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

DBMS

The document discusses different types of key and integrity constraints in SQL: primary keys, foreign keys, NOT NULL constraints, and unique keys. Primary keys uniquely identify each row in a table and cannot contain null values. Foreign keys enforce referential integrity between tables by linking the values in one table's column to those in another table's primary key column. NOT NULL constraints require that a column cannot contain null values. Unique keys are similar to primary keys but can contain one null value and the values must be unique.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

DBMS

The document discusses different types of key and integrity constraints in SQL: primary keys, foreign keys, NOT NULL constraints, and unique keys. Primary keys uniquely identify each row in a table and cannot contain null values. Foreign keys enforce referential integrity between tables by linking the values in one table's column to those in another table's primary key column. NOT NULL constraints require that a column cannot contain null values. Unique keys are similar to primary keys but can contain one null value and the values must be unique.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Implementation of key and integrity constraints:

Implementation of key and integrity constraints are given below as

 SQL Primary key


 Foreign Key
 NOT NULL Constraints
 Unique key

SQL Primary key:

 A primary key is a field in a database table that uniquely identifies each row/record.

 This is also one type of Integrity Constraint. Primary keys must have distinct values.

 Null values are not allowed in a primary key column.

 A table can only have one primary key, which can be made up of one or more fields.

 It creates a composite key when several fields are used as a primary key

Syntax:

Create table table_name

Column_name1 datatype NOT NULL,

1
Column_name2 datatype,

.....

Column_namendatatype,

PRIMARY KEY (column_name1)

Code

create table student(id int not null,name varchar(20),marks int,grade varchar(5),primary


key(id));

select * from student;

The image depicted below demonstrates the table created with id, name, marks, and grade
fields.

Input:

Output:

2
Explanation:

 By using the above query, it will create the table named student with appropriate fields.

 Id belongs to the not-null constraints, the name belongs to varchar datatype, marks
belong to varchar datatype, and finally, the id field is set to primary key constraints.

Foreign key:

 Foreign keys help ensure the consistency of your data while providing some ease.

 This is also a type of integrity constraint. You are responsible for keeping track of inter-
table dependencies and preserving their consistency from within your applications if
you don't use international keys.

 In certain situations, doing so isn't even that difficult. It’s only a matter of adding a few
more delete sentences to ensure that all is in order.

Syntax

CREATE TABLE table_name1

Column_name1 datatype NOT NULL,

Column_name2 datatype ,

...

Column_nameNdatatype ,

PRIMARY KEY (Column_name1)

3
CREATE TABLE table_name2

Column_name1 datatype NOT NULL,

Column_name2 datatype NOT NULL,

Column_name3 datatype ,

....

Column_nameNdatatype ,

PRIMARY KEY (Column_name1, Column_name2),

FOREIGN KEY (Column_name1) REFERENCES table_name2 (Column_name1) ON DELETE


CASCADE

Code

CREATE TABLE studnew

stu_id INT NOT NULL,

stu_name varchar(20),

stu_class Varchar(20),

PRIMARY KEY (stu_id)

4
CREATE TABLE classnew

stu_id INT NOT NULL,

class_id INT NOT NULL,

PRIMARY KEY (stu_id, class_id),

FOREIGN KEY (stu_id) REFERENCES stud (stu_id) ON DELETE CASCADE

Select * from studnew

input:

5
OUTPUT:

NOT NULL CONSTRAINTS:

 The not null constraint tells a column that it can't have any null values in it.

 This is also a type of integrity constraint. This forces a field to always have a value,
meaning you can't create a new record or change an existing one without adding a value
to it.

Syntax

Create table table_name

Column_name1 datatype NOT NULL,

Column_name2 datatype,

......

Column_namendatatype,
6
)

Code:

create table student1(id int not null,name varchar(20),marks int,grade varchar(5));

select * from student1;

Input:

Output:

Unique Key

7
 A collection of one or more table fields/columns that uniquely identify a record in a
database table is known as a unique key.

 This is also a type of integrity constraint. It’s similar to a primary key, but it can only
accept one null value and cannot have duplicate values.

 Both the special key and the primary key ensure that a column or group of columns is
unique. A Unique key is generated automatically.

Syntax

CREATE TABLE Table_name (

Column_Name1 DataType NOT NULL UNIQUE,

Column_Name2 DataType NOT NULL,

Column_Name3 DataType,

.......

Column_NameNDataType

);

Code

CREATE TABLE Student_DB (

S_ID int NOT NULL UNIQUE,

L_Name varchar(255) NOT NULL,

F_Name varchar(255),

Age int);

8
Input:

Output:

You might also like