Topics on Data Entry |
|
When performing data entry, with some fields, the records under a certain column usually have the same value. For example, for a local database with a table that includes an address, most employees live in the same state and the same country. When creating a column with a value that occurs regularly, you can specify that value as default. To specify the default value in a SQL statement, when creating the column, before the semi-colon of a column or before the closing parenthesis of the last column, type the DEFAULT keyword followed by the desired value. If the value is text-based, a date or a time value, then make sure you include it in single-quotes. If the value is a number, then simply provide it. Here are examples: CREATE TABLE Employees( EmployeeName varchar(50), State char(3) DEFAULT 'NSW', PostalCode int default 2000, Country varchar(32) default 'Australia'); After creating the table, the user doesn't have to provide a value for a column that has a default. If the user doesn't provide the value, the default would be used when the record is saved. |
If the user provides a value for a column that has a default value and then deletes the value, the default value rule would not apply anymore: The field would simply become empty |
A Column to Keep Track of Records |
When updating a record and changing a value, the user can make a mistake and change the wrong value. Consider the following table:
Imagine you ask the user to open this table and, for the video that is rated R, to change the name of the director to Jonathan Lynn. The user would be confused because there is more than one video that is rated R. This means that you should use the most restrictive criterion to locate the record. In future lessons, when we study data analysis, we will review other operators you can use, such as asking the user to locate the video whose title is "The Distinguished Gentleman" AND whose director is Jonathan Lynn. To be able to uniquely identify each record, you can create a special column and make sure that each value under that column is unique. You have two main options. You can put the responsibility on the user to always provide a unique value. For example, if the table includes records of students of a school, since each student must have a student number and that number must be unique from one student to another, you can ask the data entry person to make sure of this. What if the user forgets? What if the user cannot get that number at the time of data entry? What if that number can only be generated by the administration but only after the student has been registered? Based on this, an alternative is to ask the SQL interpreter to automatically generate a new and unique number for each record. A column whose values are automatically generated by the database engine is referred to as an identity column. An identity column can have only a numeric-based data type: bigint, decimal, int, numeric, smallint, or tinyint. To create an identity column, when creating the table, after the name of the column and before the semi-colon or the closing parenthesis of the last column, enter AUTO_INCREMENT. Although you can name an auto-incrementing column any way you like, by tradition, its name is usually made of the singular name of the table appended with Id or ID.
|
We have seen that an identity column was used to make sure that a table has a certain column that holds a unique value for each record. In some cases, you can use more than one column to uniquely identify each record. For example, on a table that holds the list of employees of a company, you can use both the employee number and the social security number to uniquely identity each record. In our description of the identity column, we saw that it applied only to one column; but we also mentioned that more than one column could be used to uniquely identity each record. The column or the combination of columns used to uniquely identity each column is called a primary key. To specify that a column is used as the primary key, when creating the table, at the end of the list of columns, type PRIMARY KEY() and, in the parentheses, type the name of the column that would be used as the primary key. |
Practical Learning: Creating an Auto-Incrementing Column |
|
Data Import |
Another technique used to perform data entry consists of importing already existing data from another database or from another recognizable data file. One way you can do this is to create a text file and you can use Notepad to do it. In the file, include all normal and valid SQL code. After creating the file, you should save it with a .sql (preferably) or a .txt extension. In MySQL, to import the contents of a file that contains all the necessary code already, at the SQL prompt, after specifying the target database, use the following formula: SOURCE FilePath; The SOURCE keyword lets the interpreter know that you are going to import the code from an external file. On the right side of the SOURCE keyword, enter the complete path to the SQL file. |
Practical Learning: Importing Data From an External Source |
|
|
||
Previous | Copyright © 2002-2005 FunctionX, Inc. | Next |
|