SQL - CH 12 - Creating A Database
SQL - CH 12 - Creating A Database
Page 1
With this table definition, the office number and the city must be specified when you insert a new office. The
region defaults to Eastern, the sales to zero, and the target to NULL. Note that the target would default to
NULL even without the DEFAULT NULL specification.
Page 2
The FOREIGN KEY clause specifies a foreign key in a table and the relationship that it creates
with tables in the database.
The FOREIGN KEY clause specifies the table that is referenced by the foreign key. This is the
parent table in the relationship; the table being defined is the child.
It specifies how the DBMS should treat a NULL value in one or more columns of the foreign key,
when matching it against rows of the parent table.
It specifies an optional delete rule for the relationship (CASCADE, SET NULL, SET DEFAULT),
which determines the action to take when a parent row is deleted.
An optional update rule for the relationship which determines the action to take when part of the
primary key in a parent row is updated.
An optional check constraint, which restricts the data in the table so that its rows meet a
specified search condition.
Example:
Suppose we have two tables, a CUSTOMER table that includes all customer data, and an ORDERS
table that includes all customer orders. The constraint here is that all orders must be associated with
a customer that is already in the CUSTOMER table. We place a foreign key on the ORDERS table
and have it relate to the primary key of the CUSTOMER table. This way, we can ensure that all
orders in the ORDERS table are related to a customer in the CUSTOMER table. In other words, the
ORDERS table cannot contain information on a customer that is not in the CUSTOMER table.
The structure of these two tables will be as follows:
Table CUSTOMER (Parent table)
column name
Characteristic
CustID
Primary Key
LastName
FirstName
Table ORDERS (Child table)
column name
characteristic
Order_ID
Primary Key
Order_Date
CustID
Foreign Key
Amount
In the above example, the CustID column in the ORDERS table is a foreign key pointing to the
CustID column in the CUSTOMER table.
CREATE TABLE ORDERS
(
Order_ID integer PRIMARY KEY,
Order_Date datetime,
CustID integer REFERENCES CUSTOMER(CustID),
Amount double
)
Page 3
Primary
key
Foreign
key
Forn.
key
Foreign key
When the DBMS processes the CREATE TABLE statement, it checks each foreign key definition
against the definition of the table that it references (the parent table). The foreign key and the primary
key of the referenced table must agree in the number of columns they contain and their data types.
Page 4
Naming a Constraint:
We can give a name to a constraint. For example, in the above example (Ex 3), we can call the
constraint on DOB as VALID _DOB and the constraint on DOJ as VALID_DOJ. Then the above table
definition will be written as:
Page 5
Page 6
Page 7
10
What is an index? What are the advantages of an index? When should an index be avoided?
1. An index in a database is similar to an index in a book. In an index a sorted list of the data from
the table is kept along with pointers to the places where the data is found in the table.
2. When a query is run, and an indexed field is referenced in the WHERE clause, the values are
looked up in the index rather than the table itself. Then the database jumps immediately to the
location in the table where the data is stored.
3. Indexes are stored in a data structure called a B-tree which provides fast information retrieval.
4. When an index is created, all the data in the column being indexed is stored within the B-tree.
5. Whenever the table is modified (INSERT, DELETE, or UPDATE commands), the index is also
updated.
Example:
CREATE INDEX FullName
ON Employee (LName, FName)
To drop an index, we use the DROP INDEX statement
DROP INDEX FullName
Advantages of an index:
1. Data retrieval becomes fast if the table contains many records.
2. Joining two tables using indexed columns in each table can be faster than joining non-indexed
columns.
3. If the WHERE clause is used frequently in queries, the index becomes more useful.
Disadvantages of index:
1. If the table is updated frequently, the index also has to be changed frequently and this can slow
down the system.
2. Index occupies disk space and so we should restrict the number of columns on which indexes
are created.
11
Page 8
13
Page 9