0% found this document useful (0 votes)
5 views

Week 4

Uploaded by

setoseto1907
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)
5 views

Week 4

Uploaded by

setoseto1907
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/ 34

Database Management Systems

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.

• Constraints can be column level or table level.


SQL Constraints
• The following constraints are commonly used in SQL:

• NOT NULL - Ensures that a column cannot have a NULL value

• UNIQUE - Ensures that all values in a column are different

• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each
row in a table

• FOREIGN KEY - Uniquely identifies a row/record in another table

• CHECK - Ensures that all values in a column satisfies a specific condition

• DEFAULT - Sets a default value for a column when no value is specified

• 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.

• A PRIMARY KEY constraint automatically has a UNIQUE constraint.

• 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:

CREATE TABLE Student (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
E-mail varchar(255), UNIQUE

);
SQL PRIMARY KEY on CREATE TABLE
• The following SQL creates a PRIMARY KEY on the "ID" column when the "
Student" table is created:

CREATE TABLE Student (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
E-mail varchar(255),
PRIMARY KEY (ID)
);
SQL FOREIGN KEY Constraint
• A FOREIGN KEY is a key used to connect two tables together.

• 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 "PeopleID" column in the "Orders" table is a FOREIGN KEY in the


"Orders" table.
SQL CHECK Constraint
• The CHECK constraint is used to control the value range that can be placed
in a column.

• If a CHECK constraint on a single column is defined, it allows only certain


values for this column.
SQL CHECK on CREATE TABLE
• The following SQL creates a CHECK constraint on the "Age" column when
the "People" table is created. The CHECK constraint ensures that the age
of a people must be 65, or older:

CREATE TABLE People (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=65)
);
SQL DEFAULT Constraint
• The DEFAULT constraint is used to provide a default value for a column.

• 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:

CREATE TABLE People (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT ‘London'
);
SQL CREATE INDEX Statement
• The CREATE INDEX statement is used to create indexes in tables.

• 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

• Creates an index on a table.


– CREATE INDEX index_name
ON table_name (column1, column2, ...);
CREATE INDEX Example
• The SQL statement below creates an index named " i_lastname" on the
"LastName" column in the

"People" table:
CREATE INDEX i_lastname
ON People (LastName);
CREATE INDEX Example
• Creation an index on a combination of columns :

CREATE INDEX i_pname


ON People (LastName, FirstName);
DROP INDEX Statement
• The DROP INDEX statement is used to delete an index in a table.
DROP INDEX index_name ON table_name;
Referential Integrity Constraints
Employee
SSN Name Dep_No

351 Ali 333

911 John 999

777 Helen 456

Department
Dep_No Dep_Name

111 Management

333 Computer Eng.

456 Customer Relations

999 Human Resources

222 Research
Referential Integrity Constraints
• Referential integrity is enforced via the primary key to foreign key match.

CREATE TABLE EMPLOYEE (


SSN INTEGER,
NAME CHAR(20),
DEP_NO INTEGER,
PRIMARY KEY (SSN),
FOREIGN KEY(DEP_NO) REFERENCES DEPARTMENT(DEP_NO)
ON DELETE CASCADE;
Types of Referential Integrity Constraints
ON DELETE CASCADE
ON DELETE SET DEFAULT
ON DELETE SET NULL
ON DELETE NO ACTION

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:

ALTER TABLE table_name


ADD column_name datatype;

• The following SQL adds an "Email" column to the "Customers" table:


ALTER TABLE Customers
ADD Email varchar(255);
ALTER TABLE - DROP COLUMN
• To delete a column in a table, use the following syntax :
ALTER TABLE table_name
DROP COLUMN column_name;

• 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:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;
SQL ALTER TABLE Example

ID LastName FirstName Address City


1 Hasan Ola Tmt 10 Salzburg
2 Smith Tove Bgv 23 Salzburg
3 Peter Kari Stg 20 Wien

Now we want to add a column named "DateOfBirth" in the "Persons" table.


We use the following SQL statement:

ALTER TABLE Persons


ADD DateOfBirth date;
SQL ALTER TABLE EXAMPLE
• The "Persons" table will now look like this:

ID LastName FirstName Address City DateOfBirth


1 Hasan Ola Tmt 10 Salzburg
2 Smith Tove Bgv 23 Salzburg
3 Peter Kari Stg 20 Wien
Change Data Type Example
• The data type of the column named "DateOfBirth" in the "Persons" table
can be changed.

• We use the following SQL statement:


ALTER TABLE Persons
ALTER COLUMN DateOfBirth year;
DROP COLUMN Example
• "DateOfBirth" in the "Persons" table can be deleted.

• Use the following SQL statement:


ALTER TABLE Persons
DROP COLUMN DateOfBirth;
DROP COLUMN Example
• The "Persons" table now look like this:

ID LastName FirstName Address City

1 Hasan Ola Tmt 10 Salzburg


2 Smith Tove Bgv 23 Salzburg
3 Peter Kari Stg 20 Wien
References
• Modern Database Management 11th Edition, Jeffrey A. Hoffer, V. Ramesh, Heikki
Topi © 2013 Pearson Education, Inc. Publishing as Prentice Hall

• https://www.tutorialspoint.com/dbms/er_diagram_representation.htm

• https://www.w3schools.com

You might also like