0% found this document useful (0 votes)
12 views5 pages

Lab1 - DDL

The document discusses SQL data definition language (DDL) statements used to define and modify database schema. It describes how to create databases, tables, constraints, and alter table structures using DDL statements like CREATE, ALTER, DROP. Examples are provided to demonstrate creating a university database with related tables, constraints and altering table structures.

Uploaded by

07dc855dbbb4
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)
12 views5 pages

Lab1 - DDL

The document discusses SQL data definition language (DDL) statements used to define and modify database schema. It describes how to create databases, tables, constraints, and alter table structures using DDL statements like CREATE, ALTER, DROP. Examples are provided to demonstrate creating a university database with related tables, constraints and altering table structures.

Uploaded by

07dc855dbbb4
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/ 5

Cairo University

Faculty of Computers and Artificial Intelligence


Information Systems Department

Database I, Year 2021/2022


Lab - 1
DDL

• SQL Server has 2 types of Databases :


1. System databases: Store Information about SQL server as a whole
2. User databases: Databses that user creates

• Installing SQL Server creates system databases as (master , model , tempdb and msdb)
and sample user databases such as:( pubs and Northwind)

1. There are 3 types of statements to use in SQL :


1. DDL Statements Create, Alter, Drop, and Truncate Tables. And Create, Drop
Views, Indices, and Constraints.
2. DML Statements: Insert , Update , Delete and Select from Tables or Views
3. DCL Statements: Create and Drop Users or Roles + Grant and Revoke Privileges
 DCL Statements is out of course

Data Definition Language(DDL) Statements:-


It is used to create and modify the structure of database objects in database. Includes statements
like:
1. CREATE: Creates a new table or any other database object
2. ALTER: Modifies an existing database object, such as a table
3. DROP: Removes an entire table or any other object in database

DDL Synatx

1. Creating A New Database:


CREATE DATABASE <Database_Name>
2. Dropping Database: Used to drop an existing database.
DROP DATABASE <database_name >
3. Creating Tables :Used to create a table with a specific structure inside a selected
database

1
CREATE TABLE <tablename>
(
<column name> <data type> [<column constraint>…]
PRIMARY KEY <column list>
FOREIGN KEY <column list> REFERENCES <table specifications>
)
Ex:
CREATE TABLE STAFF
(
STAFF_ID int Primary Key,
STAFF_NAME char(20) Not Null,
STAFF_SALARY real Null,
STAFF_PHOTO image Null
)

4. Altering Table: Used to change the table structure i.e. add column, change data type of
a column, or remove existent column.

• ALTER TABLE<table name>


ADD <column name> <data type>

• ALTER TABLE<table name>


DROP COLUMN <coulmn name>

• ALTER TABLE<table name>


ALTER COLUMN <column name> <new data type>

Ex: Add the column STAFF_EMAIL to the table STAFF

ALTER TABLE STAFF


ADD STAFF_EMAIL char(30)

5. Dropping Table: Used to drop a table structure and all the data stored in it:
DROP TABLE <table name>

Ex:
DROP TABLE STAFF

2
Common Data Types for table attributes are:
1- Numeric: integer number ( INT, SMALLINT AND BIGINT), and floating number (FLOAT, REAL).
2- Character: data types are either fixed length (CHAR (n), where n is the number of character)
or variable length (VARCHAR(n)).
3- Boolean: TRUE or FALSE.
4- Timestamp

SQL Constraint: -
Rules enforced on data columns on tables. These are used to limit the type of data that can go
into a table. This ensures the accuracy and reliability of the data in the database.

The following are commonly used constraints available in SQL:

• NOT NULL: A Constraint that ensures that a column cannot have NULL value.
• DEFAULT: A Constraint that provides a default value for a column when none is specified.
• UNIQUE: A Constraint that ensures that all values in a column are different or NULL.
• PRIMARY Key: A Constraint that uniquely identify each row/record in a database table (NOT
NULL + UNIQUE)
• FOREIGN KEY (FK): A Constraint that ensures referential integrity. A foreign key from 1 table
to another is used link a tuple in the 1st table to a unique tuple in the 2nd table.
• CHECK: A constraint that ensures that all values in a column satisfy a certain condition.
Constraints can be specified within the CREATE TABLE statement after the attributes are
declared, or they can be added later using the ALTER TABLE command.

Ex1: Consider the following schema of database “University”:


Student(SSN, Name, City, Age, Major)
Course(CrsCode, Name(
Registered(SSN, CrsCode, Semester, Year)
Department(DeptCode, Name)
• Write SQL DDL statement for declaring the University Database and relations. Specify
appropriate keys and referential integrity constraints
• Note that: you must ensure that the Age of student cannot be below 18 years.

CREATE database University

--DEPARTMENT Table
CREATE TABLE Department(
DeptCode VARCHAR(5) NOT NULL PRIMARY KEY ,

3
Name VARCHAR(15) NOT NULL,
)
--Course Table
CREATE TABLE Course
(
CrsCode SMALLINT NOT NULL,
Name VARCHAR(45) UNIQUE,
PRIMARY KEY (CrsCode)
)
--Student Table
CREATE TABLE Student
(
SSN INT NOT NULL,
Name VARCHAR(45),
Age INT CHECK (AGE >= 18),
City VARCHAR(15) DEFAULT 'CAIRO',
Major VARCHAR(5)
)
--Add PK to Student table
ALTER TABLE STUDENT
ADD PRIMARY KEY (SSN),
--OR
ALTER TABLE STUDENT
ADD CONSTRAINT PK1 PRIMARY KEY (SSN) -- constraint name is unique across database

-- Add FK Constraint to Student table


ALTER TABLE STUDENT
ADD CONSTRAINT FK_1 FOREIGN KEY (Major) REFERENCES Department (DeptCode)

--Registered Table
CREATE TABLE Registered
(
SSN INT NOT NULL,
CrsCode SMALLINT NOT NULL REFERENCES COURSE(Crscode),
Semester VARCHAR (45) NOT NULL,
[Year] VARCHAR (45)
)
-- Add pk and fk constraints to Registered Table
ALTER TABLE Registered
ADD PRIMARY KEY (SSN, CrsCode)
ALTER TABLE Registered
ADD FOREIGN KEY (SSN) REFERENCES Student (SSN)
ALTER TABLE Registered
ADD FOREIGN KEY (CrsCode) REFERENCES Course (CrsCode

4
ALTER EXAMPLES:

• Add Anew field in “Department” Table to store Number of Student for each Department.
 ALTER TABLE Department
ADD numberofstudents int

• Delete “Major” Column from “Student” Table.


 ALTER TABLE Student
DROP COLUMN Major

• Change the data type of Column “Name” in table “Student” to be VarChar(100).


 ALTER TABLE Student
ALTER COLUMN [Name] VARCHAR (100)

• Add a check constraint to make sure that "DeptCode" will only take one of these codes (IS,
CS, IT, DS).
 ALTER TABLE DEPARTMENT
ADD CONSTRAINT CK_DEPTCODE CHECK (DEPTCODE IN ('IS','CS','IT','DS'))

• Delete the constraint that you created in the previous example


 ALTER TABLE DEPARTMENT
DROP CONSTRAINT CK_DEPTCODE

You might also like