ICT 0417 – Databases: Importing DataTopic 18 – Databases
Learning Objective
Be able to import data from existing files (including .csv and .txt) using specified field names to create tables.
1. Understanding Source Files
- CSV (Comma‑Separated \cdot alues) – plain‑text file where each record is on a new line and fields are separated by commas (or another delimiter).
- TXT (Text) – can be delimited by commas, tabs, spaces, or a custom character; sometimes used for fixed‑width fields.
- Header row – optional first line that contains field names. If absent, you must supply names during import.
2. Preparing the File Before Import
- Open the file in a plain‑text editor (e.g., Notepad) or spreadsheet program.
- Check that each record has the same number of fields.
- Ensure that text fields containing the delimiter are enclosed in quotation marks (e.g.,
"Smith, John"). - If a header row is missing, create one with the required field names, e.g.:
StudentID,FirstName,LastName,Score
- Save the file using UTF‑8 encoding to avoid character‑set problems.
3. Common Delimiters and Their Uses
| File Type | Typical Delimiter | Example Line |
|---|
| CSV | Comma (,) | 101,Emma,Watson,85 |
| TXT (tab‑delimited) | Tab (\t) | 101 Emma Watson 85 |
| TXT (pipe‑delimited) | Pipe (|) | 101|Emma|Watson|85 |
4. Import Process – General Steps (Applicable to Access, LibreOffice Base, MySQL Workbench)
- Start the Import Wizard – Choose External Data → Import or similar command.
- Select the source file – Browse to the
.csv or .txt file. - Specify the file format:
- Delimiter character (comma, tab, pipe, etc.).
- Text qualifier (usually double quotes).
- Whether the first row contains field names.
- Map fields to database columns:
- If the file has a header row, the wizard will display those names.
- Otherwise, you will be prompted to type each field name.
- Adjust names to follow database naming rules (no spaces, start with a letter, avoid reserved words).
- Define data types for each column (e.g.,
INTEGER, TEXT, DATE):- Numeric fields →
INTEGER or DECIMAL. - Date fields → specify the expected format (e.g.,
DD/MM/YYYY). - Long text →
MEMO or VARCHAR with appropriate length.
- Set primary key (optional) – Choose a field that uniquely identifies each record, or let the system create an auto‑number field.
- Review a preview – The wizard shows the first few rows as they will appear in the table.
- Complete the import – Click Finish. The new table appears in the database window.
5. Specifying Field Names During Import
If the source file does not contain a header row, you must provide field names manually. Follow these guidelines:
- Use concise, meaningful names (e.g.,
StudentID, FirstName, Score). - Do not include spaces; use camelCase or underscores.
- Avoid reserved words such as
SELECT, TABLE, DATE. - Keep the order identical to the column order in the file.
6. Common Issues and Troubleshooting
| Problem | Possible Cause | Solution |
|---|
| Extra columns appear as a single field | Delimiter not recognized (e.g., using commas in a tab‑delimited file) | Specify the correct delimiter in the import wizard. |
| Text truncation | Field length too short for the data | Increase the field size or choose a MEMO/TEXT type. |
| Date values import as text | Incorrect date format or locale mismatch | Set the expected date format (e.g., YYYY‑MM‑DD) before import. |
| Duplicate primary‑key error | File contains repeated values in the chosen key field | Either clean the source file or let the system create an auto‑number key. |
7. Example: Importing a Student‑Score CS \cdot File
Suppose we have studentscores.csv with the following content (no header row):
001,John,Doe,78002,Jane,Smith,85
003,Emily,Jones,92
Steps to import:
- Open the database program and start the Import Wizard.
- Choose the file
studentscores.csv. - Set delimiter to comma and indicate that the first row does not contain field names.
- Enter field names in order:
StudentID – INTEGERFirstName – TEXT (length 30)LastName – TEXT (length 30)Score – INTEGER
- Set
StudentID as the primary key. - Review the preview, then finish the import.
8. Summary Checklist
- File is plain text and saved in UTF‑8.
- All records have the same number of fields.
- Delimiter and text qualifier are correctly identified.
- Header row present or field names supplied.
- Data types assigned appropriately.
- Primary key chosen to avoid duplicate errors.
Suggested diagram: Flowchart of the import process from source file to final database table.