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

Constraints Syntax

dbms

Uploaded by

Crazy Killer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Constraints Syntax

dbms

Uploaded by

Crazy Killer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Constraints Syntax

Module Objective
To describe the structure of Relational Model .

To understand database schema , relational model objects and characteristics of relations

To discuss different keys in relational database like Super Key , Candidate key ,Primary Key ,
Composite Key , Secondary Key or Alternate Key , Surrogate Key , Foreign and Unique Key

To explain different constraints like schema based or integrity , entity integrity or key ,
Referential integrity and domain constraints

To discuss SMS Case study with all constraints

To understand basic concepts of SQL

2
Session Plan
 Constraints
• Integrity Constraints
• Entity Integrity Constraints
• Referential Integrity Constraints
• Domain Constraints

3
Constraints
• SQL constraints are used to specify rules for data in a table.
• Constraints can be specified when the table is created with the
CREATE TABLE statement, or after the table is created with the
ALTER TABLE statement.
• They are used to limit the type of data that can go into a table.
• It can be column level or table level.

7
Categories of Constraints
• Constraints on databases are divided into three main categories:
• Inherent model-based constraints or implicit constraints
• Schema-based constraints or explicit constraints or integrity
constraints
• Application-based constraints

8
Inherent model-based constraints or implicit
constraints

The constraints that are implicit in a data model are called inherent
model-based constraints. These constraints are:
• Ordering of tuples in a relation: A relation is not sensitive to the
ordering of tuples.
• Values and NULLs in the Tuples: multi-valued attributes are not
allowed and special value, called NULL, is used in the cases
where values of attributes that may be unknown or may not apply to
a tuple

9
Schema-based constraints or explicit constraints
or integrity constraints

Integrity constraints are used to ensure accuracy and consistency of the


data in a relational database. These are further categorized as:
• Entity Integrity constraint or Key constraint
• Referential Integrity constraint
• Domain Integrity constraints
• Null constraint
• Unique constraint
• Check constraint
• Default constraint

10
Can you answer this ?
1 Which of the following is not a class of constraint in SQL Server?

A NOT NULL

B NULL

C CHECK

D UNIQUE

Difficulty Level Easy

Exam Type Sample

8
Can you answer this ?
2 To include integrity constraint in an existing relation use :

A Modify table

B Drop table

C Alter table

D Create table

Difficulty Level Easy

Exam Type Sample

9
Entity Integrity Constraint or Key
Constraint
• It ensures that there are no duplicate tuples in a relation.
• Entity integrity constraint is based on the Primary Key (PK).

• (dept_id) is the primary key of department relation therefore it will be


unique and not null for all tuples of the department relation
11
Can you answer this ?
1 Which of the constraint can be enforced one per table?

A Primary key constraint

B Not Null constraint

C Foreign Key constraint

D Check constraint

Difficulty Level Moderate

Exam Type Sample

11
Referential Integrity Constraints
• They are based on the concept of Foreign Key.
• Constraint: If a relation R2 has a foreign key attribute (FK) matching
the primary key attribute (PK) of other relation R1, then every value of
FK in R2 must either be equal to the value of PK in some tuple of R1
or the FK value must be null.

12
Can you answer this ?
1 Which of the following can be addressed by enforcing a referential integrity
constraint?

A All phone numbers must include the area code

B Certain fields are required (such as the email address, or phone number) before the
record is accepted

C Information on the customer must be known before anything can be sold to that customer

D When entering an order quantity, the user must input a number and not some text

Difficulty Level Moderate

Exam Type Sample

13
Domain constraint
• Domain constraints specify the set of possible values that may be
associated with an attribute. Some of the most commonly used domain
constraints are:
• Null constraint: It specifies whether null values are permitted for an attribute
• Unique constraint: It is a rule that forbids duplicate values in one or more
columns within a relation
• Check constraint: It is defined on a column for specifying the range of values that
can be inserted into it, using a predefined condition
• Default constraint: It is defined to provide a default value to a column if no other
value is provided while inserting a new record.

13
Null Constraint
If we specify a field in a table to be NOT NULL. Then the field will never accept null value

Syntax:

CREATE TABLE Student

ID int(6) NOT NULL,

NAME varchar(10) NOT NULL,

ADDRESS varchar(20)

);

14
Unique Constraint
This constraint helps to uniquely identify each row in the table.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL UNIQUE,
NAME varchar(10),
ADDRESS varchar(20)
);

15
Check Constraint
Using the CHECK constraint we can specify a condition for a field, which
should be satisfied at the time of entering values for this field.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int NOT NULL CHECK (AGE >= 18)
);
17
Default Constraint
This constraint is used to provide a default value for the fields.
Syntax:
CREATE TABLE Student
(
ID int(6) NOT NULL,
NAME varchar(10) NOT NULL,
AGE int DEFAULT 18
);

18
Can you answer this ?
1 Point out the correct statement.

A CHECK constraints enforce domain integrity

B UNIQUE constraints enforce the uniqueness of the values in a set of columns

C In a UNIQUE constraint, no two rows in the table can have the same value for the columns

D All

Difficulty Level Easy

Exam Type Sample

19
Can you answer this ?
1 Point out the wrong statement.

A Table constraints must be used when more than one column must be included in a
constraint

B A column constraint is specified as part of a column definition and applies only to that
column

C A table constraint is declared independently from a column definition and can apply to
more than one column in a table

D Primary keys allow for NULL as one of the unique values

Difficulty Level Moderate

Exam Type Sample

20
Review Questions
• What Are Constraints in relational model?
• What are the different levels of data integrity?
• Explain different constraints to maintain data integrity?
• If we need to restrict column "Name" to take only alphabets, what
kind of constraint we need to apply and how?

21
Thank You

22

You might also like