Lecture 3 SQL DDL (Continued)
Lecture 3 SQL DDL (Continued)
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.
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
Example 1:
• The following SQL statement adds an “Email” column to the “Customers” table:
Example 2:
Alter Table Department
Add numberofstudents int;
9
Alter Table - Drop Column
Alter Table table_name
Example 1:
• The following SQL statement deletes the “Email” column from the “Customers” table:
Example 2:
Alter Table Student
Drop Column Major;
10
Alter Table - Modify Datatype
Alter Table table_name
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
Dropping constraints
Or
16
SQL Drop: For Deleting Tables
• Drop Table statement allows you to remove tables from your schema.
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