Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamental of Database Systems

Similar presentations


Presentation on theme: "Fundamental of Database Systems"— Presentation transcript:

1 Fundamental of Database Systems 1401312-3
By Dr Abdullah Alzahrani د. عبدالله الزهراني SQL Data Definition

2 Reference الكتاب المرجع
Fundamentals of Database Systems, 5th ed., by Elmasri and Navathe, Pearson International Edition, Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

3 SQL DD DD (Data Definition) CREATE TABLE SQL Constraints ALTER TABLE
NOT NULL Constraint DEFAULT Constraint PRIMARY KEY Constraint FOREIGN KEY Constraint UNIQUE Constraint CHECK Constraint ALTER TABLE DROP TABLE Some of the materials and examples are taken from: and the reference book Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

4 SQL DD The CREATE TABLE Command
CREATE TABLE Command is used to specify a new relation by giving it a name and specifying its attributes and initial constraints. The attributes are specified first. Each attribute is given a name, a data type to specify its domain of values. The key, entity integrity, and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared, or they can be added later using the ALTER TABLE Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

5 SQL DD Important terms Domain Constraints: Domain constraints specify that within each tuple, the value of each attribute must be of correct data type Key constraint: defines a superkey of a relation so any two distinct tuples in any state of the relation cannot have identical values. This is to specify a uniqueness constraint. Key constraints and entity integrity constraints are specified on individual relations Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

6 SQL DD Important terms Entity integrity constraint: states that no primary key value can be NULL. This is because the primary key value is used to identify individual tuples in a relation. Having NULL values for the primary key implies that we cannot identify some tuples. For example, if two or more tuples had NULL for their primary keys, we may not be able to distinguish them if we try to reference them from other relations. Referential integrity: The referential integrity constraint is specified between two relations and is used to maintain the consistency among tuples in the two relations. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

7 SQL DD we can specify that every student record must be related to a department record. This is known as a referential integrity constraint. Another type of constraint specifies uniqueness on data item values, such as every student record must have a unique value for student_id. This is known as a key or uniqueness constraint. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

8 SQL DD In SQL, we have the following constraints:
NOT NULL - Indicates that a column cannot store NULL value UNIQUE - Ensures that each row for a column must have a unique value PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table CHECK - Ensures that the value in a column meets a specific condition DEFAULT - Specifies a default value for a column Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

9 SQL DD The CREATE TABLE Command FORM
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

10 SQL DD The CREATE TABLE examples
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

11 SQL DD The CREATE TABLE examples
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

12 SQL DD The CREATE TABLE examples
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

13 SQL DD The CREATE TABLE examples
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

14 SQL DD The ALTER TABLE Command
The definition of a table can be changed by using the ALTER command. ALTER TABLE actions include: Adding or dropping a column (attribute); Changing a column definition; Adding or dropping table constraints. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

15 SQL DD The ALTER TABLE Command FORMs
ALTER TABLE table_name ADD column_name datatype Ex: ALTER TABLE student add COLUMN student_name varchar(100) ALTER TABLE table_name DROP COLUMN column_name Ex: ALTER TABLE student drop COLUMN student_name; ALTER TABLE table_name MODIFY COLUMN column_name datatype Ex: ALTER TABLE student MODIFY COLUMN student_name varchar(100); Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

16 SQL DD The ALTER TABLE Command FORMs (cont.)
ALTER TABLE table_name ADD constraint … ALTER TABLE table_name DROP constraint … Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

17 SQL DD The ALTER TABLE Command FORMs ALTER TABLE and PRIMARY KEY
Ex: ALTER TABLE student ADD PRIMARY KEY (student_id); Ex: ALTER TABLE student ADD CONSTRAINT pk_student_ID PRIMARY KEY (student_Id, student_Name) Ex: ALTER TABLE student DROP PRIMARY KEY ALTER TABLE and FOREIGN KEY Ex: ALTER TABLE student ADD FOREIGN KEY (DEP_Id) REFERENCES DEPARTMENT(DEPARTMENT_ID) Ex:ALTER TABLE student ADD CONSTRAINT fk_student FOREIGN KEY (DEP_Id, speciality_id) REFERENCES DEPARTMENT(DEPARTMENT_ID, DEPARTMENT_ID) Ex: ALTER TABLE student DROP FOREIGN KEY fk_student Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

18 SQL DD The ALTER TABLE Command FORMs
ALTER TABLE and DEFAULT constraint Ex: ALTER TABLE student ALTER COLUMN student_name set default 's1' ALTER TABLE student ALTER student_name DROP DEFAULT ALTER TABLE and CHECK constraint Ex: ALTER TABLE student ADD CONSTRAINT con438 CHECK (student_Id>438000); Ex: ALTER TABLE student DROP con438 Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

19 SQL DD The ALTER TABLE Command FORMs ALTER TABLE and UNIQUE constraint
Ex: ALTER TABLE student ADD CONSTRAINT studentUNIQ UNIQUE (student_Id, student_Name) ALTER TABLE student DROP CONSTRAINT  studentUNIQ ALTER TABLE and NOT NULL constraint Ex: ALTER TABLE student MODIFY COLUMN varchar(100) NOT NULL; ALTER TABLE student MODIFY COLUMN varchar(100); Just remove the NOT NULL from the alter to allow NULL values. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

20 SQL DD The DROP TABLE Command
The DROP command can be used to drop named schema elements, such as tables, domains, or constraints; The relation and its definition can be deleted by using the DROP TABLE command Notice that the DROP TABLE command not only deletes all the records in the table if successful, but also removes the table definition Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

21 SQL DD The DROP TABLE Command There are two drop behaviour options:
CASCADE. RESTRICT. Example: DROP TABLE DEPENDENT CASCADE If the RESTRICT option is chosen instead of CASCADE, a table is dropped only if it is not referenced in any constraints. With the CASCADE option, all such constraints, views, and other elements that reference the table being dropped are also dropped automatically from the schema, along with the table itself. Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

22 SQL DD The DROP TABLE Command FORM
Dr Abdullah Alzahrani. Fundamental of Database Systems 9/20/2016

23 End SQL Queries Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016
Fundamental of Database Systems 9/20/2016


Download ppt "Fundamental of Database Systems"

Similar presentations


Ads by Google