0% found this document useful (0 votes)
4 views19 pages

Lecture 3 SQL DDL (Continued)

The document provides an overview of SQL Data Definition Language (DDL) focusing on creating and modifying database tables. It includes exercises on creating tables with unique and foreign key constraints, using auto-increment fields, and altering tables to add or drop columns and constraints. Additionally, it explains the use of reserved words in SQL and concludes with practical exercises for applying the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views19 pages

Lecture 3 SQL DDL (Continued)

The document provides an overview of SQL Data Definition Language (DDL) focusing on creating and modifying database tables. It includes exercises on creating tables with unique and foreign key constraints, using auto-increment fields, and altering tables to add or drop columns and constraints. Additionally, it explains the use of reserved words in SQL and concludes with practical exercises for applying the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Course: Introduction to Database Systems

Course Code: IS211

SQL
DDL (Continued)
Presented by:
Dr. Dina Ezzat
1
Exercise 1
• Write a SQL statement to create table Colleges with attributes college_id,
college_code and college_country given the following information:
• The value of the college_code column must be unique. Similarly, the value of
college_id must be unique as well as it can not store NULL values. Also the value
of the college_country column should be Egypt if no value is entered.

CREATE TABLE Colleges (


college_id INT Primary Key,
college_code VARCHAR(20) Unique,
college_country VARCHAR(20) DEFAULT ‘Egypt’
);

2
Exercise 2
• Consider the following three relation instances: table1, table2, and table3. The attributes that
make up primary keys are underlined. In table2, attribute A is a foreign key to attribute A in
table1; and attributes {C,D} in table2 form a foreign key to attributes {C,D} in table3.

1. Write CREATE TABLE statements to create the three tables. Infer suitable data types from
the example instances. Include definitions of primary keys and foreign keys.

3
Exercise 2 (Cont.)
2. Does the order of the CREATE TABLE statements matter in your solution?

Yes, the order does matter, table2 can only be created after table1 and table3 were
created, because the definition of table2 references the two other tables in its foreign
key constraints.

4
SQL Auto Increment Field
• Auto-increment allows a unique number to be generated automatically when a
new record is inserted into a table.

• Often this is the primary key field that we would like to be created automatically
every time a new record is inserted.

Examples?

5
Reserved Words in SQL
• In SQL, certain words are reserved. These are called keywords or reserved words. These words can not
be used as identifiers i.e., as column names in SQL.
• To use these reserved words, write the column names inside the square brackets ‘[]’ so that they are not
read as reserved words.
• Example:
• Year
• Registered(SSN, CrsCode, Semester, Year)
Create Table Registered
(
SSN int Foreign Key References Student(SSN),
CrsCode int Foreign Key References Course(Crscode),
Semester varchar (45) NOT NULL,
[Year] varchar (45);
Primary Key (SSN, CrsCode)
);
6
SQL Alter Table Statement

7
SQL Alter Table Statement (Cont.)

The Alter Table statement is used to add, delete or modify (change data type)
columns in an existing table.

8
Alter Table - Add Column
Alter Table table_name

Add column_name datatype;

Example 1:
• The following SQL statement adds an “Email” column to the “Customers” table:

Alter Table Customers


Add Email varchar (100);

Example 2:
Alter Table Department
Add numberofstudents int;
9
Alter Table - Drop Column
Alter Table table_name

Drop Column column_name;

Example 1:
• The following SQL statement deletes the “Email” column from the “Customers” table:

Alter Table Customers


Drop Column Email;

Example 2:
Alter Table Student
Drop Column Major;
10
Alter Table - Modify Datatype
Alter Table table_name

Alter Column column_name datatype; New data type

Example 1:
Alter Table Student Can I change from any data type
Alter Column StudentName varchar (100);
to any data type?
Example 2:
Alter Table Customer
Alter Column Address char (100);
11
SQL Alter Table Statement (Cont.)

The Alter Table statement is also used to add and drop various constraints
on an existing table.

12
Alter Table: Constraints
Adding constraints

Alter Table table_name


Add Constraint constraint syntax;

Alter Table table_name


Add Constraint constraint_name constraint syntax;

Dropping constraints

Alter Table table_name


Drop Constraint constraint_name;
13
Alter Table: Constraints Examples
Alter Table Registered

Add Constraint Primary Key (SSN, CrsCode);

Or

Alter Table Registered

Add Constraint PK1 Primary Key (SSN, CrsCode);

Constraint name is unique across database.


14
Alter Table: Constraints Examples (Cont.)
Alter Table Student
Add Constraint UQ_Std_Name unique (Name);

Alter Table Student


Drop Constraint UQ_Std_Name;

Alter Table Department


Add Constraint CK_DeptCode Check (DeptCode in (‘IS’, ‘CS’, ‘IT’, ‘DS’));

Alter Table Department


Drop Constraint CK_ DeptCode; 15
Alter Table: Constraints Examples (Cont.)
Alter Table Persons
Alter Column City Drop Default;

Alter Table STUDENT


Add Constraint FK_1 Foreign Key (Major) References Department (DeptCode);

16
SQL Drop: For Deleting Tables
• Drop Table statement allows you to remove tables from your schema.

Drop Table table_name;

Example:
Drop Table Student;

17
Exercises
Write SQL statements for the following questions:
• Add a new attribute called “CustomerAddress” to the table “Customer”.
Alter Table Customer
Add CustomerAddress char (25);
• Change the data type of the column “CustomerAddress” in the table “Customer” to Varchar (100).
Alter Table Customer
Alter Column CustomerAddress varchar (100);
• Remove/Delete the table Customer.
Drop Table Customer;
• Add a foreign key constraint to the table “Orders” on the attribute “PersonID” which refers to the attribute
“PersonID” in the table “Persons”.
Alter Table Orders
Add Constraint FK_PersonOrder Foreign Key (PersonID) References Persons(PersonID);

18
Any Questions?

19

You might also like