Documente Academic
Documente Profesional
Documente Cultură
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.
Cont
License#, Surname, First Name, Unit#, DOB, Mobile# Serial#, Brand, Model, Cost, Description
POSSESSION
Cont
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
1
UNIT TENANT
1
UNIT TENANT Unit#, Area, Telephone#
3
OWNERSHIP License#, Surname, First Name, Unit#, DOB, Mobile#
License#, Serial#
4 3
POSSESSION Serial#, Brand, Model, Cost, Description
2
POSSESSION Serial#, Brand, Model, Cost, Description
UNIT
TENANT
4
Unit#, Area, Telephone# License#, Surname, First Name, Unit#, DOB, Mobile#
Any other/s?
1
POSSESSION Serial#, Brand, Model, Cost, Description
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
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
10
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
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
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
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
14
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
or
16
License#, Surname, First Name, Unit#, DOB, Mobile# Serial#, Brand, Model, Cost, Description
POSSESSION
17
18
Note: When dropping a table each line is considered a complete statement and thus needs the termination character ;.
19