Sie sind auf Seite 1von 25

Database Presentation

Prepared By
Aaditya Manandhar
BIM 4th semester
Section A Roll no 1

Database Presentation 7/19/2018 1


 Introduction to constrains
 Syntax
 Constraintsused in SQL
 Use of Constraints
 About Integrity Constraints and its Type
 About Domain Constraints and its Type

Database Presentation 7/19/2018


2
 Rules enforced on data columns on table.
 Purpose is to maintain data integrity
 Used to limit the type of data that goes into a table
 Other words, specifies the rules for the data in table
 Any violation between constraints and data it prevents it

 Syntax of SQL constraints while creating Table is shown


in next slide

Database Presentation 7/19/2018


3
 CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);

Database Presentation 7/19/2018


4
 NOT NULL- Indicates that a column cannot store NULL value

 UNIQUE – Ensures that each row for a column must have a unique
value

 PRIMARY KEY- Uniquely Identify the tuple

 FOREIGN KEY- referential Integrity of data in one table to match


value in next table

 DEFAULT- Specifies the default value for column

Database Presentation 7/19/2018


5
 Protect the integrity of the database

 To tell DBMS about the data

Database Presentation 7/19/2018


6
 Also known as Referential constraints
 Ensures changes made to database do not result loss of data
consistency
 Impose restriction on allowable data in database

Database Presentation 7/19/2018


7
1.Entity Integrity Constraints

2. Referential Integrity Constraints

Database Presentation 7/19/2018


8
 Refers about Primary key in database
 Unique key to identify the tuple
 States that no attribute of primary key is null
 i.e. primary key cannot be a null value
 States that every relation must have a primary
key

Database Presentation 7/19/2018


9
Database Presentation 7/19/2018
10
 every value of one column of relation to exist as
value of another column in different
table(relation)
 States about the foreign key
 Defines the relation between the tables
 Foreign key must reference a valid primary
key(Parent table)

Database Presentation 7/19/2018


11
Database Presentation 7/19/2018
12
 Specifies what set of values an attribute can take
 User defined constraints

 They are elementary form of integrity constraints


 Test values inserted in the database and test queries to
ensure that comparison makes sense

 Data types associated are integer, character, string,


date, time etc
 Example: Age cannot be negative value or alphabets

Database Presentation 7/19/2018


13
Database Presentation 7/19/2018
14
 NOT NULL Constraints
 UNIQUE Constraints
 Default Constraints

Database Presentation 7/19/2018


15
 Strictthe column not to accept null value
 Enforces a field to contain value
 Cannot modify record without adding
value

Database Presentation 7/19/2018


16
 CREATE TABLE PersonsNotNull
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

 Here P_id must have some value which is of integer type and
 LatName should have character type value

Database Presentation 7/19/2018


17
 Works as like primary key
 Uniquely identify each record in relation
 Many UNIQUE per table but only one
primary key

Database Presentation 7/19/2018


18
 CREATE TABLE Persons
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

 Here P_id must be a unique integer data type

Database Presentation 7/19/2018


19
Database Presentation 7/19/2018
20
 Assign default value to field
 Value will only be assignesd when no
other value is specifeid
 Written as:
 City varchar(255) DEFAULT 'Sandnes'

Database Presentation 7/19/2018


21
 CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT ‘Kathmandu'
);

 Value ‘Kathmandu’ will be assigned if no value is given


in City column

Database Presentation 7/19/2018


22
 Constraints defines way to insert data in
relation
 Helps us to know what type of data we
insert in relation
 Uniquely identifying records
 Create relation between tables

Database Presentation 7/19/2018


23
 www.tutorialspoint.com/sql/sql-constraints.htm

 beginnersbook.com/2015/04/constraints-in-dbms/

 www.w3schools.com/sql/sql_constraints.asp

 http://www.edugrabs.com/types-of-constraints-in-dbms-or-
relational-constraints/

 https://www.youtube.com/watch?v=zvYU9vlvDNY

 https://en.wikipedia.org/wiki/Entity_integrity

Database Presentation 7/19/2018


24
Database Presentation 7/19/2018
25

Das könnte Ihnen auch gefallen