This document discusses SQL constraints. It defines constraints as limitations on the type of data that can go into a table. The main types of constraints covered are:
1. Not null constraints, which enforce that a column cannot be null
2. Unique constraints, which uniquely identify each record in a table
3. Primary key constraints, which uniquely identify each record and cannot be null
4. Foreign key constraints, which link to primary keys in other tables to define relationships
5. Check constraints, which define valid value ranges for a column
6. Default constraints, which provide a default value for a column if no other value is specified.
Examples are provided for each constraint type to illustrate their syntax and usage
Introduction to SQL and its constraints, presented by Israa Al-Jarrah from the CIS Department.
Explanation of SQL constraints, the meaning of constraints, and types of constraints including integrity, value, and table constraints. Constraints can be defined inline (for columns) or out-of-line (for tables) during table creation or alteration.
The UNIQUE constraint ensures each record is distinct, with examples provided for defining it within SQL tables.
Primary key constraints ensure unique and non-null values, with detailed examples of their implementation in tables.
Comparison between Unique and Primary Keys in terms of null value acceptance and usage in tables.
Foreign keys link records between tables, maintaining integrity with examples of related Persons and Orders tables.
Examples of SQL commands to create tables with Foreign Key constraints, ensuring relational integrity.
Check constraints limit allowed values in columns; Default constraints insert preset values for new records.
Example SQL statements defining primary keys composed of multiple columns and unique constraints.
Reference materials and closing remarks from the presenter, thanking the audience for their attention.
Constraints canbe specified when a table is created
(with the CREATE TABLE statement) or after the
table is created (with the ALTER TABLE statement)
Constraints are used to limit the type of data
that can go into a table.
You can defineconstraints syntactically in two ways:
1. As part of the definition of an individual column or
attribute. This is called inline specification.
2. As part of the table definition. This is called
out-of-line specification.
6.
Types of constraints:
1.Integrity constraints
2.Value constraints
Table constraint
Restricts data value with
respect to all other values in
table
Not Null
constraintenforces a column to NOT accept
NULL values. constraint enforces a column to
NOT accept NULL values.
This means that you cannot insert a new
record, or update a record without
adding a value to this field.
Expression of Not Null we use short cut NN
9.
1. Not NullExample :-
Create Table Language
(
Language_Name varchar2(30),
Language_Id number(15)
Constraint
Constraint_Language_language_name
_NN
Not Null
);
2. Unique Key
The UNIQUE constraint uniquely identifies
each record in a database table.
13.
2. Unique KeyExample
Create Table Person
(
Person_Id number(15)
Constraint
Person_person_id_UN Unique
Person_name varchar2(40),
Person_BD Date
);
14.
2. Unique KeyExample
Create Table Person
(
Person_Id number(15),
Person_name varchar2(40),
Person_BD Date
Constraint
Person_person_id_UN Unique
(Person_Id)
15.
3. Primary Key
Primary keys must contain unique values
A primary key column cannot contain NULL
values.
Each table should have a primary key, and each
table can have only ONE primary key
Unique Key
UniqueKey
accept only one null value
unique key use many time in
table.
20.
Primary Key
UniqueKey
Primary key does not accept null
value
primary key use only one time
in table.
21.
4. Foreign Key
Unique Key
A FOREIGN KEY in one table points to a PRIMARY KEY in
another table
The FOREIGN KEY constraint is used to prevent actions
that would destroy links between tables.
22.
4. Foreign KeyExample
Unique Key
The "Persons" table:
P-Id LastName FirstName Address CIty
1 Hansen Ola Timoteivn Sandnes
10
2 Svendson Tove Borgvn23 Sandnes
3 Pettersen Kari Storagt 20 Sandnes
4. Foreign KeyExample
"P_Id" column in the "Orders" table
points to the "P_Id" column in the
"Persons" table.
The "P_Id" column in the "Persons" table is
the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is
a FOREIGN KEY in the "Orders" table.
25.
4. Foreign KeyExample
First step must create “Person” Table
Create Table Person
(
P_Id number Constraint
constraint_person_person_Id_PK primary key,
LastName varcahr2(45),
FirstName varchar2(45),
Address varchar2(45),
City varchar2(45)
);
5. Check Key
The CHECK constraint is used to limit the value range
that can be placed in a column.
If you define a CHECK constraint on a single column it
allows only certain values for this column
28.
5. Check KeyExample
CREATE TABLE divisions
(
div_no NUMBER CONSTRAINT check_divno
CHECK
(div_no BETWEEN 10 AND 99),
div_name VARCHAR2(9) CONSTRAINT
check_divname_cc CHECK
(div_name = UPPER(div_name))
);
29.
5. Check Key
6. Default Key
The DEFAULT constraint is used to insert a default
value into a column
The default value will be added to all new records
if no other value is specified