Week 4
Week 4
SQL II
Pınar Yıldırım
SQL Table Constraints
• SQL constraints are used to define rules for data in a table.
• Constraints can be defined
– when the table is created with the CREATE TABLE statement,
– or after the table is created with the ALTER TABLE statement.
Syntax
CREATE TABLE table_name (
column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);
SQL Constraints
• Constraints are used to control the type of data that can go into a table.
They are important for the accuracy and reliability of the data in the table.
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table
• INDEX - Used to create and retrieve data from the database very quickly
SQL NOT NULL Constraint
• By default, a column can hold NULL values.
• The NOT NULL constraint enforces a column to NOT accept NULL values.
SQL NOT NULL on CREATE TABLE
• The following SQL ensures that the "ID", "LastName", and "FirstName"
columns will NOT accept NULL values when the " student" table is created:
Example
CREATE TABLE Student (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
E-mail varchar(255)
);
SQL UNIQUE Constraint
• The UNIQUE constraint provides that all values in a column are different.
• Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
• However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
SQL UNIQUE Constraint on CREATE TABLE
• The following SQL creates a UNIQUE constraint on the "ID" column when
the " Student" table is created:
);
SQL PRIMARY KEY on CREATE TABLE
• The following SQL creates a PRIMARY KEY on the "ID" column when the "
Student" table is created:
• A FOREIGN KEY is a field (or collection of fields) in one table that refers to
the PRIMARY KEY in another table.
SQL FOREIGN KEY Constraint
• Look at the following two tables:
"People" table:
PeopleID LastName FirstName Age
1 Hasan Ola 30
2 Smith Tove 23
Foreign key
3 Peter Kari 20
• Orders table:
OrderID OrderNumber PeopleID
1 77895 3
2 44678 3
3 22456 2
4 24562 1
SQL FOREIGN KEY Constraint
• The "PeopleID" column in the "Orders" table points to the "PeopleID"
column in the "Persons" table.
• The "PeopleID" column in the "People" table is the PRIMARY KEY in the
"People" table.
• The default value will be added to all new records IF there is no other
value.
SQL DEFAULT on CREATE TABLE
• The following SQL sets a DEFAULT value for the "City" column when the "
People" table is created:
• Indexes are used to retrieve data from the database more quickly than
otherwise. The users cannot see the indexes, they are just used to speed
up searches/queries.
CREATE INDEX Syntax
"People" table:
CREATE INDEX i_lastname
ON People (LastName);
CREATE INDEX Example
• Creation an index on a combination of columns :
Department
Dep_No Dep_Name
111 Management
222 Research
Referential Integrity Constraints
• Referential integrity is enforced via the primary key to foreign key match.
ON UPDATE CASCADE
ON UPDATE SET DEFAULT
ON UPDATE SET NULL
ON UPDATE NO ACTION
SQL ALTER TABLE Statement
• The ALTER TABLE statement is used to add, delete, or modify columns in
an existing table.
• The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
ALTER TABLE - ADD Column
• To add a column in a table, use the following syntax:
• The following SQL deletes the "Email" column from the "Customers"
table:
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE - ALTER/MODIFY COLUMN
• To change the data type of a column in a table, use the following syntax:
• https://www.tutorialspoint.com/dbms/er_diagram_representation.htm
• https://www.w3schools.com