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

4.relational Model (RDBMS), Key, Constraints

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

4.relational Model (RDBMS), Key, Constraints

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

Relational Model

(RDBMS)
various terminologies of RDBMS
table/Relation

• rows and columns to store data.


• Each relation has a unique name
• Relation does not contain duplicate tuples.
• The tuples of a relation have no specific order.
row or record-horizontal entity in
the table
• No two tuples are identical
• All tuples of the relation have the same format and the same number
of entries.
• The order of the tuple is irrelevant.
column/attribute-vertical entity in
the table
• Every attribute of a relation must have a name.
• Default values can be specified for an attribute automatically inserted
if no other value is specified for an attribute.
• Attributes that uniquely identify each tuple of a relation are the
primary key.
Data Integrity
• Entity integrity: It specifies that there should be no duplicate rows in
a table.
• Domain integrity: It enforces valid entries for a given column by
restricting the type, the format, or the range of values.
• Referential integrity specifies that rows cannot be deleted, which are
used by other records.
• User-defined integrity: It enforces some specific business rules
defined by users. These rules are different from the entity, domain, or
referential integrity.
KEYS:
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 - 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
When can you set a constraints ?
• During Table creation
• After table creation (Alter)
Is Constraints set to one
attribute ???
• NO

• Can be set to multiple attribute also.


NOT NULL
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int);
NOT NULL on ALTER TABLE
ALTER TABLE Persons
MODIFY Age int NOT NULL;
UNIQUE Constraint
• UNIQUE constraint ensures that all values in a column
are different.
• CREATE TABLE Persons (
ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
UNIQUE constraint on multiple
columns
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255),
FirstName varchar(255) NOT NULL,
Age int,
CONSTRAINT UC_Person UNIQUE (ID,LastName)
);
UNIQUE Constraint on ALTER TABLE

• ALTER TABLE Persons


ADD UNIQUE (ID);

on multiple columns

• ALTER TABLE Persons


ADD CONSTRAINT UC_Person UNIQUE (ID,LastName);
PRIMARY KEY on CREATE TABLE
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
• CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (ID,LastNa
me)
);
PRIMARY KEY on ALTER TABLE

• ALTER TABLE Persons


ADD PRIMARY KEY (ID);

• on multiple columns

• ALTER TABLE Persons


ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastNa
me);
FOREIGN KEY
• CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(Person
ID)
);
FOREIGN KEY on ALTER TABLE

• ALTER TABLE Orders


ADD FOREIGN KEY (PersonID) REFERENCES Persons(P
ersonID);
CHECK Constraint

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
CHECK constraint on multiple
columns
• 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 AND Ci
ty='Sandnes')
);
CHECK on ALTER TABLE
• ALTER TABLE Persons
ADD CHECK (Age>=18);
DEFAULT on CREATE TABLE

• CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes‘ );
• CREATE TABLE Orders (
ID int NOT NULL,
OrderNumber int NOT NULL,
OrderDate date DEFAULT GETDATE() );
DEFAULT on ALTER TABLE
• ALTER TABLE Persons
ALTER City SET DEFAULT 'Sandnes';
AUTO INCREMENT

• CREATE TABLE Persons (


Personid int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (Personid)
)
Can a constrain be dropped ??
• Yes using “Drop” command.
• ALTER TABLE Persons
ALTER City DROP DEFAULT;

• ALTER TABLE Persons


DROP PRIMARY KEY;

You might also like