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.
|
|
|
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.
|
|
- Download the Students text file and save it to your hard drive. I will assume that you saved it in a folder as C:\Programs
- Display the MySQL prompt
- To create a new database, execute the following statement
CREATE DATABASE ROSH1; - Execute the following statement:
USE ROSH1; - Assuming that you installed the Students.sql file at C:\Programs,
execute a statement as follows (change the path to the file
accordingly):
SOURCE C:Programs\Students.sql; - Notice that a new table with values filled with values has been
created.
Type Exit and press Enter to close the SQL environment
No comments:
Post a Comment