10
MySQL Importing and creating a database

MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Embed Size (px)

Citation preview

Page 1: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

MySQL

Importing and creating a database

Page 2: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

CSV (Comma Separated Values) file• CSV = Comma Separated Values – they are simple text files

containing data which can be imported into a database or spreadsheet

• A CSV can be created using a spreadsheet in Excel (Save as CSV)

• In the CSV file, the data is delimited by a comma. Delimiters separate the data.

• Other types of delimiters include a semi-colon or tab

Spreadsheet (Excel)

CSV file (opened in Notepad)

Page 3: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

MySQL Import Functioon• CSV files can be imported into a database, spreadsheet, etc. (Excel

files may also be imported, but CSV files are simpler and more universal)

Create spreadsheet

Save as CSV file

Import using PHPMyAdmin

Page 4: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 1 - Create CSV FileCreate the table containing the data:1. Copy a table with data from any web page (e.g. flight info)2. Paste into Excel or spreadsheet (paste without formatting)3. Make sure the table has a row of column headers, e.g. departing

from, airline, flight no., etc.4. Save as a CSV file

Page 5: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 2- Import CSV File to PHPMyAdminExercise (continued)5. Open PHPMyAdmin on screen, open/create a database file6. Click Import7. Select the CSV file8. Make sure “Use header” is selected (see below) so the first row of

the table does not get accepted as part of the data. (it will use this as field names instead)

9. When the CSV file imports, a table is created with a default name, e.g. table 3

Page 6: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 3 – Rename the TableContinued …• Select and rename the table (use Operations tab):

Table renamed, e.g. flight_departures

Page 7: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 4 – Add Primary KeyContinued …• To change and edit the structure of the table, it needs a unique

column identifier (primary key)• Open the table structure. Add a column at the beginning of the

table

• Use a name such as id, with type INT (length 3 digits), primary and A_I for auto increment

• Save

Page 8: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 5 - continued• Each record now has a unique field (id) to identify it

• The PRIMARY KEY uniquely identifies each record in a database table.

• A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can have only ONE primary key. In this example the primary key is called id

Page 9: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Step 6 (final) – Edit Structure• You can then edit the structure and field names and data type (int

for id, var char for basic information, and time for flight times

• Here is some example data inside the table

Page 10: MySQL Importing and creating a database. CSV (Comma Separated Values) file CSV = Comma Separated Values – they are simple text files containing data which

Summary

• Data can be created in a different format and imported into MySQL

• MySQL is a fast database system enabling interaction with web pages.

Further exercises (if time)• A database can be exported to a file, then it becomes portable.

You could use the file to transfer the database to a different PHPMyAdmin

• The exported database can be re-imported on a different computer.

1. Export the database in CSV formatand save to a folder.

2. Import CSV file in Excel.