|
Topics on Data Entry |
During data entry, users of your database will face columns that expect data. Sometimes, for one reason or another, data will not be available for a particular field. An example would be an MI (middle initial) field: some people have a middle initial, some others either don't have it or would not provide it to the user. This aspect can occur for any field of your table. Therefore, you should think of a way to deal with it. A field is referred to as null when no data is available for it:
A field is referred to as null if there is no way of determining the value of its content or its value is simply unknown. As you can see, it is not a good idea to have a null field in your table. As a database developer, it is your responsibility to always know with certainty the value held by each field of your table. Remember that even if a field is empty, you should know what value it is holding because being empty could certainly mean that the field has a value. To show that a column would allow null values, it can be marked with the NULL keyword. If a column must always require a value, it must be marked as NOT NULL. If you are creating a new table, to indicate that a column would allow null values, type NULL before the comma that separates it from the next column. If it is the last column, enter NULL before the closing parenthesis of the CREATE TABLE statement. This is the same way you would use NOT NULL. If a table has already been created and you want to mark one of its columns as NULL or NOT NULL, if you are using MSDE, you can modify the column using the ALTER COLUMN expression. Here is an example: ALTER TABLE Contractors ALTER COLUMN LastName varchar(20) NOT NULL GO This would apply the NOT NULL property to the LastName column of the Contractors table.
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.
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.
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.
|
|
|
||
| Previous | Copyright © 2007, Yevol.com | Next |
|
|
||