Вы находитесь на странице: 1из 19

The Structured Query Language (SQL)

Table Creation

Once you have created the ER model the next step is to implement your design into a functional database. Before you may start creating your tables and actually writing any code you must first work out the order of table creation.

Order of Table Creation


The base rule when selecting table creation order is that: the one side of a one-to-many relationship will ALWAYS occur before the many side, e.g.
STUDENT ENROLMENT UNIT

Student#, Student Name

Student#, Unit Code

Unit Code, Unit Name

Creation order: STUDENT->UNIT->ENROLMENT, or UNIT-> STUDENT-> ENROLMENT


3

Order of Table Creation

Cont

Now you have a go at:


License#, Serial# UNIT TENANT OWNERSHIP

Unit#, Area, Telephone#

License#, Surname, First Name, Unit#, DOB, Mobile# Serial#, Brand, Model, Cost, Description

POSSESSION

Order of Table Creation

Cont

In this model you may discern:


The Unit and Possession are both valid start points. Unit must be created before Tenant Possession and Tenant must be created before Ownership Unit Possession Tenant Ownership Possession Unit Tenant Ownership Unit Tenant Possession Ownership
5

This gives us several acceptable creation orders:


Order of Table Creation

1
UNIT TENANT

Cont Possible orders of table creation


2
License#, Serial# OWNERSHIP

1
UNIT TENANT Unit#, Area, Telephone#

3
OWNERSHIP License#, Surname, First Name, Unit#, DOB, Mobile#

License#, Serial#

Unit#, Area, Telephone#

License#, Surname, First Name, Unit#, DOB, Mobile#

4 3
POSSESSION Serial#, Brand, Model, Cost, Description

2
POSSESSION Serial#, Brand, Model, Cost, Description

UNIT

TENANT

License#, Serial# OWNERSHIP

4
Unit#, Area, Telephone# License#, Surname, First Name, Unit#, DOB, Mobile#

Any other/s?

1
POSSESSION Serial#, Brand, Model, Cost, Description

SQL Create Table Statements

A table creation statement as used in SQL consists of several basic elements:


The words create table The name of the table An opening parenthesis Column definitions (separated by a comma) Constraint definitions (separated by a comma) A closing parenthesis A SQL terminator (;) (could be go in Transac-SQL)
7

SQL Create Table Statements


An example of this would be a create table statement for the following basic entity:
STUDENT Student#, Surname, First_Names, Date_Of_Birth, Home_Phone, Mobile_Phone, Height_In_Meters

CREATE TABLE STUDENT ( Student# VARCHAR(9) NOT NULL, Surname VARCHAR(20) NOT NULL, First_Names VARCHAR(15) NOT NULL, Date_Of_Birth DATE NOT NULL, Home_Phone VARCHAR(11), Mobile_Phone VARCHAR(10), Height_In_Meters NUMBER(3,2), CONSTRAINT STUDENT_PK PRIMARY KEY(Student#) );
8

SQL - Constraints

The database uses constraints to prevent invalid data entry into tables. You may thus use constraints to do the following:

Enforce rules at the table level whenever a row is inserted, updated, or deleted from that table. The constraint must be satisfied for the operation to succeed. Prevent the deletion of a table if there are dependencies from other tables. Provide rules for Oracle tools, such as Oracle Developer.
9

SQL Types of Constraints


Constraint NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK Description Specifies that this column may not contain a null value Specifies a column or combination of columns whose values must be unique for all rows in the table Uniquely identifies each row of the table Establishes and enforces a foreign key relationship between the column and a column of the referenced table Specifies a condition that must be true

10

SQL Primary Keys

A primary key constraint creates a primary key for the table. Only one primary key can be created for each table. The primary key constraint enforces uniqueness of the column or column combination Ensures that no column that is part of the primary key can contain a null value.
11

SQL Primary Keys

For example:
CREATE TABLE STUDENT ( Student# VARCHAR(9) NOT NULL, Surname VARCHAR(20) NOT NULL, First_Names VARCHAR(15) NOT NULL, Date_Of_Birth DATETIME NOT NULL, Home_Phone VARCHAR(11), Mobile_Phone VARCHAR(10), Height_In_Meters NUMBER(3,2), CONSTRAINT STUDENT_PK PRIMARY KEY(Student#) );

Notes: Some SQL version doesnt allow # to be part of column name (e.g., in MySQL), in this case, use Student_Num instead of Student#.
12

SQL Primary Keys


For Example: (A compound Key)

cont

CREATE TABLE Compact_Disc ( CD_Name VARCHAR(100) NOT NULL, Artist VARCHAR(100) NOT NULL, No_Of_Tracks Number, CONSTRAINT CD_PK PRIMARY KEY(CD_Name,Artist) );

13

SQL Foreign Keys

The foreign key, or referential integrity constraint, designates a column or combination of columns as a foreign key and establishes a relationship between a primary key or a unique key in the same table or a different table. For example if we were to write the create table scripts for the simple enrolment model:
STUDENT ENROLMENT UNIT

Student#, Student Name

Student#, Unit Code

Unit Code, Unit Name

14

SQL Foreign Keys


CREATE TABLE STUDENT ( Student# VARCHAR(9) NOT NULL, Student_Name VARCHAR(150) NOT NULL, CONSTRAINT STUDENT_PK PRIMARY KEY(Student#) ); CREATE TABLE UNIT ( Unit_Code VARCHAR(7) NOT NULL, Unit_Name VARCHAR(150) NOT NULL, CONSTRAINT UNIT_PK PRIMARY KEY(Unit_Code) ); CREATE TABLE ENROLEMENT ( Student# VARCHAR(9) NOT NULL, Unit_Code VARCHAR(7) NOT NULL, CONSTRAINT ENR_STU_FK FOREIGN KEY (Student#) REFERENCES STUDENT(Student#), CONSTRAINT ENR_UNIT_FK FOREIGN KEY (Unit_Code) REFERENCES UNIT(Unit_Code), CONSTRAINT ENROL_PK PRIMARY KEY(Student#,Unit_Code) ); 15

Drop Order

The drop order requires that all tables with foreign keys be dropped before the table they reference is dropped, thus the drop order is always a reverse of a valid table create order. For example with the simple enrolment E-R model the valid creation orders were:

Student Unit Enrolment Unit Student Enrolment Enrolment Unit Student Enrolment Student Unit

or

Thus the valid drop orders would be:


or

16

Drop Order cont


Now try writing all possible drop orders:
License#, Serial# UNIT TENANT OWNERSHIP

Unit#, Area, Telephone#

License#, Surname, First Name, Unit#, DOB, Mobile# Serial#, Brand, Model, Cost, Description

POSSESSION

17

Drop Order cont

relationship must be dropped before the one side.

Rule: Many side of the one-to-many

Your answer should something like:


Ownership->Tenant >Possession->Unit Ownership->Tenant ->Unit >Possession Ownership->Possession->Tenant >Unit

18

Drop Order cont


Once the drop order is determined the tables may be dropped in the order using DROP TABLE statement as (e.g., for the last one):

DROP DROP DROP DROP

TABLE TABLE TABLE TABLE

OWNERSHIP; POSSESSION; TENANT; UNIT;

Note: When dropping a table each line is considered a complete statement and thus needs the termination character ;.
19