Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Constraints

Avatar for Prachi Langde Prachi Langde
September 16, 2026
1

 Constraints

SQl

Avatar for Prachi Langde

Prachi Langde

September 16, 2026

Transcript

  1. SQL Constraints • SQL constraints are used to specify rules

    for data in a table. • Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. • If there is any violation between the constraint and the data action, the action is aborted. • Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.
  2. • 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 - Prevents actions that would destroy links between tables • CHECK - Ensures that the values in a column satisfies a specific condition • DEFAULT - Sets a default value for a column if no value is specified • CREATE INDEX - Used to create and retrieve data from the database very quickly
  3. SQL NOT NULL Constraint • By default, a column can

    hold NULL values. • The NOT NULL constraint enforces a column to NOT accept NULL values. • This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.
  4. SQL NOT NULL on CREATE TABLE • The following SQL

    ensures that the "ID", "LastName", and "FirstName" columns will NOT accept NULL values when the "Persons" table is created: • Example • CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255) NOT NULL, Age int );
  5. SQL NOT NULL on ALTER TABLE • To create a

    NOT NULL constraint on the "Age" column when the "Persons" table is already created, use the following SQL: • ALTER TABLE Persons MODIFY Age int NOT NULL;
  6. SQL PRIMARY KEY Constraint • The PRIMARY KEY constraint uniquely

    identifies each record in a table. • Primary keys must contain UNIQUE values, and cannot contain NULL values. • A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).
  7. SQL PRIMARY KEY on CREATE TABLE • The following SQL

    creates a PRIMARY KEY on the "ID" column when the "Persons" table is created: • CREATE TABLE Persons ( ID int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int );
  8. SQL PRIMARY KEY on ALTER TABLE • To create a

    PRIMARY KEY constraint on the "ID" column when the table is already created, use the following SQL: • ALTER TABLE Persons ADD CONSTRAINT PK_Person PRIMARY KEY (ID);
  9. DROP a PRIMARY KEY Constraint • To drop a PRIMARY

    KEY constraint, use the following SQL: • ALTER TABLE Persons DROP CONSTRAINT PK_Person;
  10. SQL UNIQUE Constraint • The UNIQUE constraint ensures 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.
  11. SQL UNIQUE Constraint on CREATE TABLE • The following SQL

    creates a UNIQUE constraint on the "ID" column when the "Persons" table is created: • CREATE TABLE Persons ( ID int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int );
  12. DROP a UNIQUE Constraint • To drop a UNIQUE constraint,

    use the following SQL: • ALTER TABLE Persons DROP CONSTRAINT UC_Person;
  13. SQL UNIQUE Constraint on ALTER TABLE • To create a

    UNIQUE constraint on the "ID" column when the table is already created, use the following SQL: • ALTER TABLE Persons ADD UNIQUE (ID); • To name a UNIQUE constraint, and to define a UNIQUE constraint on multiple columns, use the following SQL syntax: • ALTER TABLE Persons ADD CONSTRAINT UC_Person UNIQUE (ID,LastName );
  14. SQL FOREIGN KEY Constraint • The FOREIGN KEY constraint is

    used to prevent actions that would destroy links between tables. • A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. • The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.
  15. Persons Table PersonID LastName FirstName Age 1 Hansen Ola 30

    2 Svendson Tove 23 3 Pettersen Kari 20 Orders Table OrderID Order Number PersonID 1 77895 3 2 44678 3 3 22456 2 4 24562 1
  16. • Notice that the "PersonID" column in the "Orders" table

    points to the "PersonID" column in the "Persons" table. • The "PersonID" column in the "Persons" table is the PRIMARY KEY in the "Persons" table. • The "PersonID" column in the "Orders" table is a FOREIGN KEY in the "Orders" table. • The FOREIGN KEY constraint prevents invalid data from being inserted into the foreign key column, because it has to be one of the values contained in the parent table.
  17. SQL FOREIGN KEY on CREATE TABLE • The following SQL

    creates a FOREIGN KEY on the "PersonID" column when the "Orders" table is created: • CREATE TABLE Orders ( OrderID int NOT NULL PRIMARY KEY, OrderNumber int NOT NULL, PersonID int FOREIGN KEY REFERENCES Persons(PersonID) );
  18. SQL FOREIGN KEY on ALTER TABLE • To create a

    FOREIGN KEY constraint on the "PersonID" column when the "Orders" table is already created, use the following SQL: • ALTER TABLE Orders ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
  19. DROP a FOREIGN KEY Constraint • To drop a FOREIGN

    KEY constraint, use the following SQL: • ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
  20. SQL CHECK Constraint • 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 column it will allow only certain values for this column. • If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
  21. SQL CHECK on CREATE TABLE • The following SQL creates

    a CHECK constraint on the "Age" column when the "Persons" table is created. The CHECK constraint ensures that the age of a person must be 18, or older: • CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int CHECK (Age>=18) );
  22. • To allow naming of a CHECK constraint, and for

    defining a CHECK constraint on multiple columns , use the following SQL syntax: • CREATE TABLE Persons ( ID int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int, City varchar(255), CONSTRAINT CHK_Person CHECK (Age>=18 A ND City='Sandnes') );
  23. SQL CHECK on ALTER TABLE • To create a CHECK

    constraint on the "Age" column when the table is already created, use the following SQL: • ALTER TABLE Persons ADD CHECK (Age>=18);
  24. • To allow naming of a CHECK constraint, and for

    defining a CHECK constraint on multiple columns, use the following SQL syntax: • ALTER TABLE Persons ADD CONSTRAINT CHK_PersonAge CHECK ( Age>=18 AND City='Sandnes');
  25. DROP a CHECK Constraint • To drop a CHECK constraint,

    use the following SQL: • ALTER TABLE Persons DROP CONSTRAINT CHK_PersonAge;
  26. Entity Set in DBMS• An entity refers to any object

    having• Either a physical existence such as a particular person, office, house or car. • Or a conceptual existence such as a school, a university, a company or a job.
  27. 1. Strong Entity Set• A strong entity set is an

    entity set that contains sufficient attributes to uniquely identify all its entities. • In other words, a primary key exists for a strong entity set. • Primary key of a strong entity set is represented by underlining it.
  28. • Symbols Used• A single rectangle is used for representing

    a strong entity set. • A diamond symbol is used for representing the relationship that exists between two strong entity sets. • A single line is used for representing the connection of the strong entity set with the relationship set. • A double line is used for representing the total participation of an entity set with the relationship set. • Total participation may or may not exist in the relationship.
  29. • In this ER diagram, • Two strong entity sets

    “Student” and “Course” are related to each other. • Student ID and Student name are the attributes of entity set “Student”. • Student ID is the primary key using which any student can be identified uniquely. • Course ID and Course name are the attributes of entity set “Course”. • Course ID is the primary key using which any course can be identified uniquely. • Double line between Student and relationship set signifies total participation. • It suggests that each student must be enrolled in at least one course. • Single line between Course and relationship set signifies partial participation. • It suggests that there might exist some courses for which no enrollments are made.
  30. Weak Entity Set• A weak entity set is an entity

    set that does not contain sufficient attributes to uniquely identify its entities. • In other words, a primary key does not exist for a weak entity set. • However, it contains a partial key called as a discriminator. • Discriminator can identify a group of entities from the entity set. • Discriminator is represented by underlining with a dashed line.
  31. • The combination of discriminator and primary key of the

    strong entity set makes it possible to uniquely identify all entities of the weak entity set. • Thus, this combination serves as a primary key for the weak entity set. • Clearly, this primary key is not formed by the weak entity set completely.
  32. Symbols Used• A double rectangle is used for representing a

    weak entity set. • A double diamond symbol is used for representing the relationship that exists between the strong and weak entity sets and this relationship is known as identifying relationship. • A double line is used for representing the connection of the weak entity set with the relationship set. • Total participation always exists in the identifying relationship.
  33. • In this ER diagram, • One strong entity set

    “Building” and one weak entity set “Apartment” are related to each other. • Strong entity set “Building” has building number as its primary key. • Door number is the discriminator of the weak entity set “Apartment”. • This is because door number alone can not identify an apartment uniquely as there may be several other buildings having the same door number. • Double line between Apartment and relationship set signifies total participation. • It suggests that each apartment must be present in at least one building. • Single line between Building and relationship set signifies partial participation. • It suggests that there might exist some buildings which has no apartment.
  34. • To uniquely Identify any apartment, • First, building number

    is required to identify the particular building. • Secondly, door number of the apartment is required to uniquely identify the apartment. • Thus, • Primary key of Apartment • = Primary key of Building + Its own discriminator
  35. Extended ER Features • Extended ER is a high-level data

    model that incorporates the extensions to the original ER model. Enhanced ER models are high level models that represent the requirements and complexities of complex databases. • The extended Entity Relationship (ER) models are three types as given below − • Aggregation • Specialization • Generalization
  36. Specialization • The process of designing sub groupings within an

    entity set is called specialization. It is a top-down process. If an entity set is given with all the attributes in which the instances of the entity set are differentiated according to the given attribute value, then that subclasses or the sub-entity sets can be formed from the given attribute. • Example • Specialization of a person allows us to distinguish a person according to whether they are employees or customers. Specialization of account creates two entity sets: savings account and current account. • In the E-R diagram specialization is represented by triangle components labeled ISA. The ISA relationship is referred as superclass- subclass relationship as shown below −
  37. Generalization • It is the reverse process of specialization. It

    is a bottom-up approach. • It converts subclasses to superclasses. This process combines a number of entity sets that share the same features into higher-level entity sets. • If the sub-class information is given for the given entity set then, ISA relationship type will be used to represent the connectivity between the subclass and superclass as shown below −
  38. Aggregation • It is an abstraction in which relationship sets

    are treated as higher level entity sets and can participate in relationships. Aggregation allows us to indicate that a relationship set participates in another relationship set. • Aggregation is used to simplify the details of a given database where ternary relationships will be changed into binary relationships. Ternary relation is only one type of relationship which is working between three entities.