Lecture7
Lecture7
Database System
Lecture7
SQL (Structured Query Language)
Data Definition Language DDL
Lecture7 – Fall2024 1
SSMS
Lecture7 – Fall2024
SQL Command
SQL Command
Lecture7 – Fall2024
SQL CREATE / DROP DATABASE Statement
2. Using a Query
Lecture7 – Fall2024
SQL CREATE / DROP DATABASE Statement
❑ Syntax :
CREATE DATABASE databasename;
CREATE DATABASE University;
❑ Syntax :
USE University
GO
❑ Syntax :
DROP DATABASE databasename;
DROP DATABASE University;
Lecture7 – Fall2024
SQL CREATE / DROP DATABASE Statement
❑ Syntax :
ALTER DATABASE OldNamedatabase MODIFY
NAME=NewNameDatabase ;
Lecture7 – Fall2024
SQL CREATE / DROP DATABASE Statement
❑ Syntax :
CREATE DATABASE Sales
ON
(
NAME = Sales_data ,
FILENAME =‘C:\Program Files\Micrsoft SQL
Server\MSSQL15.MSSQLSERVER\MYSQL\DATA\saledata.mdf’,
SIZE = 10,
MAXSIZE = 50 ,
FILEGROWTH=5)
LOG ON
(
NAME = Sales_LOG ,
FILENAME =‘C:\Program Files\Micrsoft SQL
Server\MSSQL15.MSSQLSERVER\MYSQL\DATA\saledata.idf’,
SIZE = 5MB,
MAXSIZE = 25MB ,
FILEGROWTH=5MB)
Lecture7 – Fall2024
CREATE Schema & DROP Schema
Lecture7 – Fall2024
SQL CREATE TABLE Statement
❑ Syntax :
CREATE TABLE [database_name.][schema_name.] table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
▪ The column parameters specify the names of the columns of the
table.
▪ The datatype parameter specifies the type of data the column can
hold (e.g. varchar, integer, date, etc.).
SQL CREATE TABLE Example
Lecture7 – Fall2024
SQL Data Types for SQL Server
Lecture7 – Fall2024
SQL Statement
Lecture7 – Fall2024
SQL Statement
Lecture7 – Fall2024
SQL Date Data Types
SQL Server comes with the following data types for storing a date or
a date/time value in the database:
Lecture7 – Fall2024
SQL TRUNCATE TABLE
❑ Syntax :
TRUNCATE TABLE table_name;
Lecture7 – Fall2024
SQL Server Describe Table
❑ Syntax :
EXEC SP_HELP TableName;
❑ Syntax :
DROP TABLE table_name;
Lecture7 – Fall2024
SQL ALTER TABLE
Alter Table
Columns Constraints
Lecture7 – Fall2024
SQL ALTER TABLE Statement
• The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
Lecture7 – Fall2024
ALTER TABLE - ADD Column
Lecture7 – Fall2024
ALTER TABLE - ADD Column
➢ To add more than on new columns to the table
Lecture7 – Fall2024
ALTER TABLE - ALTER/MODIFY Column
❑ Rename Table
❑ Rename Column
EXEC SP_RENAME Name table .Old Column, New Column
);
SQL Constraints
ProductName VARCHAR(50),
Age int CHECK (Age BETWEEN 18 AND 21),
Gender CHAR(1)
);
Lecture7 – Fall2024
ADD Constraints
ALTER TABLE Persons
ADD CONSTRAINT Persons_ProductName_UQ UNIQUE (ProductName);
ALTER TABLE Persons
ADD CONSTRAINT Persons_PK PRIMARY KEY (ID);
ALTER TABLE Persons
ADD CONSTRAINT Persons_FK FOREIGN KEY(ID ,Name) REFERENCES
Person(personID , personName)
ADD CONSTRAINT Persons_FK FOREIGN KEY(ID) REFERENCES
Person(personID)
ALTER TABLE Persons
ADD CONSTRAINT Persons_Gender_Default DEFAULT 3 FOR Gender
Lecture7 – Fall2024
ADD Constraints
Lecture7 – Fall2024
ADD Constraints
ALTER TABLE Persons
Lecture7 – Fall2024
DROP Constraints
Lecture7 – Fall2024
SQL FOREIGN KEY Constraint
▪ The table containing the foreign key is called the child table, and
the table containing the candidate key is called the referenced or
parent table.
Lecture7 – Fall2024
SQL FOREIGN KEY Constraint
ON DELETE { NO ACTION | CASCADE | SET NULL | SET DEFAULT }
Specifies what action happens to rows in the table created, if those rows have a referential
relationship and the referenced row is deleted from the parent table. The default is NO
ACTION.
❑ NO ACTION
The Database Engine raises an error and the delete action on the row in the parent table is
rolled back.
❑ CASCADE
Corresponding rows are deleted from the referencing table if that row is deleted from the parent
table.
❑ SET NULL
All the values that make up the foreign key are set to NULL if the corresponding row in the
parent table is deleted. For this constraint to execute, the foreign key columns must be nullable.
❑ SET DEFAULT
All the values that make up the foreign key are set to their default values if the corresponding
row in the parent table is deleted. For this constraint to execute, all foreign key columns must
have default definitions. If a column is nullable, and there is no explicit default value set,
NULL becomes the implicit default value of the column.
SQL FOREIGN KEY Constraint
ON UPDATE { NO ACTION | CASCADE \ SET NULL | SET DEFAULT }
Specifies what action happens to rows in the table altered when those rows have a referential
relationship and the referenced row is updated in the parent table. The default is NO ACTION.
❑ NO ACTION
The Database Engine raises an error, and the update action on the row in the parent table is
rolled back.
❑ CASCADE
Corresponding rows are updated in the referencing table when that row is updated in the parent
table.
❑ SET NULL
All the values that make up the foreign key are set to NULL when the corresponding row in the
parent table is updated. For this constraint to execute, the foreign key columns must be
nullable.
❑ SET DEFAULT
All the values that make up the foreign key are set to their default values when the
corresponding row in the parent table is updated. For this constraint to execute, all foreign key
columns must have default definitions. If a column is nullable, and there is no explicit default
value set, NULL becomes the implicit default value of the column.
SQL FOREIGN KEY Constraint
CREATE TABLE table_name
(col_name col_type
[NOT NULL| NULL]
[IDENTITY [ (seed , increment) ]]
[DEFAULT initial-value]
[CONSTRAINT const_name]{
[CHECK logical_expression]
[PRIMARY KEY col_name]
[FOREIGN KEY fore_name REFERENCES ref_tab_nam
[(par_col_name)]
[ON DELETE { NO ACTION | CASCADE | SET NULL | SET
DEFAULT}]
[ON UPDATE { NO ACTION | CASCADE | SET NULL | SET
DEFAULT} ]
}); Lecture7 – Fall2024
Example
Phone M N
Employee Work_on Project
Employee
ENO INT,
Hour TIME ,
PRIMARY KEY(PID,ENO) );
Lecture7 – Fall2024
❑ Database Schema (Relationship Schema)
In the relationship diagram, the tables are linked to each other through
the foreign key, and to create a database relationship diagram, we
expand the database that was created, right-click on the Database
Diagram, and create a New Database Diagram as in the following
figure:
Lecture7 – Fall2024
❑ Database Schema (Relationship Schema)
Lecture7 – Fall2024
❑ Database Schema (Relationship Schema)
We add the tables by pressing Add to show the following figure:
To save the chart by saving from the toolbar and choosing the name of
the chart, and to delete a relationship between two tables, we right-
click on the relationship, and the following figure appears, so we
choose Delete Relationship Database, and thus we have deleted the
relationship between the two tables as in the following figure:
❑ The Entity – Relationship (ER) Model
❑ Database Schema
ExpiredDate
❑ CREATE DATABASE SuperMarket
❑ USE SuperMarket
);
❑ CREATE TABLE BillProducts (
BillID INT REFERENCES Bill(BillID),
ProductID INT REFERENCES Product(ProductID),
Quantity FLOAT CHECK (Quantity > 0),
PRIMARY KEY (BillID , ProductID));
Database Diagram