SlideShare a Scribd company logo
Database System Sunita M. Dol
Page 1
HANDOUT#07
Aim:
Write SQL queries for different integrity constraints and authorization commands.
Theory:
Integrity Constraints
Integrity constraints guard against accidental damage to the database, by ensuring that authorized
changes to the database do not result in a loss of data consistency. There are following integrity
constraints that can be applied on CREATE command
• Primary key constraints
• Not null constraints
• Check constraints
• Foreign key constraints
• Unique
• Default
Primary key constraints
The PRIMARY KEY constraint uniquely identifies each record in a database table. The primary
key attributes are required to be notnull and unique; that is, no tuple can have a null value for a
primary-key attribute, and no two tuples in the relation can be equal on all the primary-key
attributes. Although the primary-key specification is optional, it is generally a good idea to
specify a primary key for each relation. A table can have only one primary key, which may
consist of single or multiple fields.
The syntax is
primary key (A1, A2, . . . , An )
The primary-key specification says that attributes A1, A2, . . . , An form the primary key for the
relation.
Example: Declare branch_name as the primary key for branch Query using CREATES
command is
create table branch
(branch_name char(15),
branch_city char(30),
assets integer,
primary key (branch_name))
Here the primary key constraint on the branch_name attribute of the branch relation uniquely
identifies each tuple of the table branch.
Database System Sunita M. Dol
Page 2
NOT NULL Constraints
The not null constraint on an attribute specifies that the null value is not allowed for that
attribute; in other words, the constraint excludes the null value from the domain of that attribute.
It means that a field should always contain a value and you cannot insert a new record, or update
a record without adding a value to this field.
Example: Declare branch_city column to NOT accept NULL values. Here the query is
create table branch
(branch_name char(15),
branch_city char(30) not null,
assets integer,
primary key (branch_name))
The not null constraint on the branch_city attribute of the branch relation ensures that the
branch city cannot be null
CHECK Constraints
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 single column it allows only certain values for this column. The
check clause specifies a predicate P that must be satisfied by every tuple in the relation.
Example: Declare assets value of the branch relation >0 on the relation branch
create table branch
(branch_name char(15),
branch_city char(15),
assets numeric(16,2),
primary key(branch_name),
check(assets>0));
The check constraint on the assets attribute of the branch relation ensures that the assets value is
greater than zero.
FOREIGN Key Constraints
A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or
collection of fields) in one table that refers to the PRIMARY KEY in another table
To explain this integrity constraints foreign key on CREATE command; consider there are two
relation r1 and r2. A relation schema, say r2, may include among its attributes the primary key of
another relation schema, say r1. This attribute is called a foreign key from r2, referencing r1. The
relation r2 is also called the referencing relation of the foreign key dependency, and r1 is called
the referenced relation of the foreign key.
foreign key (A1 , A2, . . . , An ) references s: The foreign key specification says that the values
of attributes (A1 , A2, . . . , An ) for any tuple in the relation must correspond to values of the
Database System Sunita M. Dol
Page 3
primary key attributes of some tuple in relation s. Here s is called the referenced relation of the
foreign key
Consider the example as shown in figure. There are two tables branch and account.
create table branch
(branch_name char(15),
branch_city char(15),
assets numeric(16,2),
primary key(branch_name),
check(assets>=0));
create table account
(account_number char(10),
branch_name char(15),
balance numeric(12,2),
primary key(account_number),
foreign key(branch_name)references branch,
check(balance>=0));
The definition of the account table has a declaration “foreign key (branch_name) references
branch”. This foreign-key declaration specifies that for each account tuple, the branch_name
specified in the tuple must exist in the primary key attribute (brach_name) of the branch relation.
Without this constraint, it is possible for a account relation to specify a nonexistent branch name.
UNIQUE Constraints
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.
Example: Declare branch_city as not null and unique on the relation branch
create table branch
(branch_name char(15) not null unique,
branch_city char(15),
assets numeric(16,2),
check(assets>=0));
DEFAULT Constraints
The DEFAULT constraint is used to provide a default value for a column. The default value will
be added to all new records if no other value is specified
Example: Declare default value ‘Brooklyn’ for the field branch_city on the relation branch
While creating the table the default value is specified for the field branch_city that is ‘Brooklyn’.
Database System Sunita M. Dol
Page 4
create table branch
(branch_name char(15),
branch_city char(15) default ‘Brooklyn’,
assets numeric(16,2),
primary key(branch_name),
check(assets>=0));
Authorization Commands
Data Control Language is used to control access to data stored in a database (Authorization). In
particular, it is a component of Structured Query Language (SQL). Examples of DCL commands
include GRANT and REVOKE.
GRANT Command
GRANT command is used to allow specified users to perform specified tasks. The syntax for
granting privileges on a table in SQL Server is:
GRANT privileges_name ON object TO user;
privileges_name are the privileges granted to the user.
(SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER, ALL)
SELECT Ability to perform SELECT statements on the table.
INSERT Ability to perform INSERT statements on the table.
UPDATE Ability to perform UPDATE statements on the table.
DELETE Ability to perform DELETE statements on the table.
REFERENCES Ability to create a constraint that refers to the table.
ALTER Ability to perform ALTER TABLE statements to change the table
definition.
ALL ALL does not grant all permissions for the table. Rather, it grants the
permissions which are SELECT, INSERT, UPDATE, DELETE, and
REFERENCES.
object is the name of the database object to which permissions are being granted.
user is the name of the user to whom the privileges would be granted.
If you wanted to grant SELECT on a table called student to a user name sunita, then run the
following GRANT statement:
GRANT SELECT ON students TO sunita;
If you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a table
called student to a user name sunita, then run the following GRANT statement:
GRANT SELECT, INSERT, UPDATE, DELETE ON students TO sunita;
Database System Sunita M. Dol
Page 5
You can also use the ALL keyword to indicate that you wish to grant the permissions (i.e.:
SELECT, INSERT, UPDATE, DELETE, and REFERENCES) to a user named Sunita is written
as:
GRANT ALL ON students TO sunita;
If you wanted to grant only SELECT access on the student table to all users, then grant the
privileges to the public role as given here
GRANT SELECT ON employees TO public;
REVOKE Command
REVOKE command is used to cancel previously granted or denied permissions. The syntax for
granting privileges on a table in SQL Server is:
GRANT privileges ON object TO user;
privileges_name are the privileges granted to the user.
(SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER, ALL)
SELECT Ability to perform SELECT statements on the table.
INSERT Ability to perform INSERT statements on the table.
UPDATE Ability to perform UPDATE statements on the table.
DELETE Ability to perform DELETE statements on the table.
REFERENCES Ability to create a constraint that refers to the table.
ALTER Ability to perform ALTER TABLE statements to change the table
definition.
ALL ALL does not grant all permissions for the table. Rather, it grants the
permissions which are SELECT, INSERT, UPDATE, DELETE, and
REFERENCES.
object is the name of the database object to which permissions are being granted.
user is the name of the user to whom the privileges would be granted.
If you wanted to grant SELECT on a table called student to a user name sunita, then run the
following GRANT statement:
GRANT SELECT ON students TO sunita;
If you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a table
called student to a user name sunita, then run the following GRANT statement:
GRANT SELECT, INSERT, UPDATE, DELETE ON students TO sunita;
You can also use the ALL keyword to indicate that you wish to grant the permissions (i.e.:
SELECT, INSERT, UPDATE, DELETE, and REFERENCES) to a user named Sunita is written
as:
GRANT ALL ON students TO sunita;
If you wanted to grant only SELECT access on the student table to all users, then grant the
privileges to the public role as given here
GRANT SELECT ON employees TO public;
Database System Sunita M. Dol
Page 6
Queries and Output:
Conclusion:
Thus we studies SQL queries for different integrity constraints like
• Primary Key
• Foreign Key
• NOT NULL
• CHECK
• UNIQUE
• DEFAULT
and authorization commands like
• GRANT and
• REVOKE
References:
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) sixth edition.
• Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan
(McGraw Hill International Edition) fifth edition.
• http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/
• http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/

More Related Content

What's hot

STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
VENNILAV6
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
Smriti Jain
 
SQL commands
SQL commandsSQL commands
SQL commands
GirdharRatne
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
Nazir Ahmed
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
Ashwin Dinoriya
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
Ram Kedem
 
SQL
SQLSQL
SQL Basics
SQL BasicsSQL Basics
SQL Basics
Hammad Rasheed
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
Dr. C.V. Suresh Babu
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
Ashish Gaurkhede
 
Database queries
Database queriesDatabase queries
Database queries
IIUM
 
SQL Views
SQL ViewsSQL Views
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
1keydata
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
Ram Sagar Mourya
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
TechandMate
 

What's hot (20)

STRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIESSTRUCTURE OF SQL QUERIES
STRUCTURE OF SQL QUERIES
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
SQL commands
SQL commandsSQL commands
SQL commands
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Chapter 07 ddl_sql
Chapter 07 ddl_sqlChapter 07 ddl_sql
Chapter 07 ddl_sql
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Advanced SQL Webinar
Advanced SQL WebinarAdvanced SQL Webinar
Advanced SQL Webinar
 
SQL
SQLSQL
SQL
 
SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Commands of DML in SQL
Commands of DML in SQLCommands of DML in SQL
Commands of DML in SQL
 
Database queries
Database queriesDatabase queries
Database queries
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 

Similar to Assignment#07

3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
Sunita Milind Dol
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
Dev Chauhan
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
Sherif Gad
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
amitabros
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
prathap kumar
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
SakkaravarthiS1
 
Sql
SqlSql
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
Vivek Singh
 
Sql commands
Sql commandsSql commands
Sql commands
Pooja Dixit
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
Ehtisham Ali
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
TarunKumar893717
 
Ankit
AnkitAnkit
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
Abhishek590097
 
SQL Query
SQL QuerySQL Query
SQL Query
Imam340267
 
12 SQL
12 SQL12 SQL
12 SQL
12 SQL12 SQL
Database models and DBMS languages
Database models and DBMS languagesDatabase models and DBMS languages
Database models and DBMS languages
DivyaKS12
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
Iblesoft
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
Prosanta Ghosh
 
Module 3
Module 3Module 3
Module 3
cs19club
 

Similar to Assignment#07 (20)

3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQLDATABASE MANAGMENT SYSTEM (DBMS) AND SQL
DATABASE MANAGMENT SYSTEM (DBMS) AND SQL
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Sql
SqlSql
Sql
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql server ___________session_15(data integrity)
Sql server  ___________session_15(data integrity)Sql server  ___________session_15(data integrity)
Sql server ___________session_15(data integrity)
 
Interview Questions.pdf
Interview Questions.pdfInterview Questions.pdf
Interview Questions.pdf
 
Ankit
AnkitAnkit
Ankit
 
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQueryPPT  of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
PPT of Common Table Expression (CTE), Window Functions, JOINS, SubQuery
 
SQL Query
SQL QuerySQL Query
SQL Query
 
12 SQL
12 SQL12 SQL
12 SQL
 
12 SQL
12 SQL12 SQL
12 SQL
 
Database models and DBMS languages
Database models and DBMS languagesDatabase models and DBMS languages
Database models and DBMS languages
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013Dbms ii mca-ch4-relational model-2013
Dbms ii mca-ch4-relational model-2013
 
Module 3
Module 3Module 3
Module 3
 

More from Sunita Milind Dol

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
Sunita Milind Dol
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
Sunita Milind Dol
 
9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
Sunita Milind Dol
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
Sunita Milind Dol
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
Sunita Milind Dol
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
Sunita Milind Dol
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
Sunita Milind Dol
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
Sunita Milind Dol
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
Sunita Milind Dol
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
Sunita Milind Dol
 
Assignment12
Assignment12Assignment12
Assignment12
Sunita Milind Dol
 
Assignment11
Assignment11Assignment11
Assignment11
Sunita Milind Dol
 

More from Sunita Milind Dol (20)

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
 
9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment11
Assignment11Assignment11
Assignment11
 

Recently uploaded

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
abbyasa1014
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
Mahmoud Morsy
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
gowrishankartb2005
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
MiscAnnoy1
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 

Recently uploaded (20)

The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Engineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdfEngineering Drawings Lecture Detail Drawings 2014.pdf
Engineering Drawings Lecture Detail Drawings 2014.pdf
 
Certificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi AhmedCertificates - Mahmoud Mohamed Moursi Ahmed
Certificates - Mahmoud Mohamed Moursi Ahmed
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Material for memory and display system h
Material for memory and display system hMaterial for memory and display system h
Material for memory and display system h
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
Introduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptxIntroduction to AI Safety (public presentation).pptx
Introduction to AI Safety (public presentation).pptx
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 

Assignment#07

  • 1. Database System Sunita M. Dol Page 1 HANDOUT#07 Aim: Write SQL queries for different integrity constraints and authorization commands. Theory: Integrity Constraints Integrity constraints guard against accidental damage to the database, by ensuring that authorized changes to the database do not result in a loss of data consistency. There are following integrity constraints that can be applied on CREATE command • Primary key constraints • Not null constraints • Check constraints • Foreign key constraints • Unique • Default Primary key constraints The PRIMARY KEY constraint uniquely identifies each record in a database table. The primary key attributes are required to be notnull and unique; that is, no tuple can have a null value for a primary-key attribute, and no two tuples in the relation can be equal on all the primary-key attributes. Although the primary-key specification is optional, it is generally a good idea to specify a primary key for each relation. A table can have only one primary key, which may consist of single or multiple fields. The syntax is primary key (A1, A2, . . . , An ) The primary-key specification says that attributes A1, A2, . . . , An form the primary key for the relation. Example: Declare branch_name as the primary key for branch Query using CREATES command is create table branch (branch_name char(15), branch_city char(30), assets integer, primary key (branch_name)) Here the primary key constraint on the branch_name attribute of the branch relation uniquely identifies each tuple of the table branch.
  • 2. Database System Sunita M. Dol Page 2 NOT NULL Constraints The not null constraint on an attribute specifies that the null value is not allowed for that attribute; in other words, the constraint excludes the null value from the domain of that attribute. It means that a field should always contain a value and you cannot insert a new record, or update a record without adding a value to this field. Example: Declare branch_city column to NOT accept NULL values. Here the query is create table branch (branch_name char(15), branch_city char(30) not null, assets integer, primary key (branch_name)) The not null constraint on the branch_city attribute of the branch relation ensures that the branch city cannot be null CHECK Constraints 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 single column it allows only certain values for this column. The check clause specifies a predicate P that must be satisfied by every tuple in the relation. Example: Declare assets value of the branch relation >0 on the relation branch create table branch (branch_name char(15), branch_city char(15), assets numeric(16,2), primary key(branch_name), check(assets>0)); The check constraint on the assets attribute of the branch relation ensures that the assets value is greater than zero. FOREIGN Key Constraints A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table To explain this integrity constraints foreign key on CREATE command; consider there are two relation r1 and r2. A relation schema, say r2, may include among its attributes the primary key of another relation schema, say r1. This attribute is called a foreign key from r2, referencing r1. The relation r2 is also called the referencing relation of the foreign key dependency, and r1 is called the referenced relation of the foreign key. foreign key (A1 , A2, . . . , An ) references s: The foreign key specification says that the values of attributes (A1 , A2, . . . , An ) for any tuple in the relation must correspond to values of the
  • 3. Database System Sunita M. Dol Page 3 primary key attributes of some tuple in relation s. Here s is called the referenced relation of the foreign key Consider the example as shown in figure. There are two tables branch and account. create table branch (branch_name char(15), branch_city char(15), assets numeric(16,2), primary key(branch_name), check(assets>=0)); create table account (account_number char(10), branch_name char(15), balance numeric(12,2), primary key(account_number), foreign key(branch_name)references branch, check(balance>=0)); The definition of the account table has a declaration “foreign key (branch_name) references branch”. This foreign-key declaration specifies that for each account tuple, the branch_name specified in the tuple must exist in the primary key attribute (brach_name) of the branch relation. Without this constraint, it is possible for a account relation to specify a nonexistent branch name. UNIQUE Constraints 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. Example: Declare branch_city as not null and unique on the relation branch create table branch (branch_name char(15) not null unique, branch_city char(15), assets numeric(16,2), check(assets>=0)); DEFAULT Constraints The DEFAULT constraint is used to provide a default value for a column. The default value will be added to all new records if no other value is specified Example: Declare default value ‘Brooklyn’ for the field branch_city on the relation branch While creating the table the default value is specified for the field branch_city that is ‘Brooklyn’.
  • 4. Database System Sunita M. Dol Page 4 create table branch (branch_name char(15), branch_city char(15) default ‘Brooklyn’, assets numeric(16,2), primary key(branch_name), check(assets>=0)); Authorization Commands Data Control Language is used to control access to data stored in a database (Authorization). In particular, it is a component of Structured Query Language (SQL). Examples of DCL commands include GRANT and REVOKE. GRANT Command GRANT command is used to allow specified users to perform specified tasks. The syntax for granting privileges on a table in SQL Server is: GRANT privileges_name ON object TO user; privileges_name are the privileges granted to the user. (SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER, ALL) SELECT Ability to perform SELECT statements on the table. INSERT Ability to perform INSERT statements on the table. UPDATE Ability to perform UPDATE statements on the table. DELETE Ability to perform DELETE statements on the table. REFERENCES Ability to create a constraint that refers to the table. ALTER Ability to perform ALTER TABLE statements to change the table definition. ALL ALL does not grant all permissions for the table. Rather, it grants the permissions which are SELECT, INSERT, UPDATE, DELETE, and REFERENCES. object is the name of the database object to which permissions are being granted. user is the name of the user to whom the privileges would be granted. If you wanted to grant SELECT on a table called student to a user name sunita, then run the following GRANT statement: GRANT SELECT ON students TO sunita; If you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a table called student to a user name sunita, then run the following GRANT statement: GRANT SELECT, INSERT, UPDATE, DELETE ON students TO sunita;
  • 5. Database System Sunita M. Dol Page 5 You can also use the ALL keyword to indicate that you wish to grant the permissions (i.e.: SELECT, INSERT, UPDATE, DELETE, and REFERENCES) to a user named Sunita is written as: GRANT ALL ON students TO sunita; If you wanted to grant only SELECT access on the student table to all users, then grant the privileges to the public role as given here GRANT SELECT ON employees TO public; REVOKE Command REVOKE command is used to cancel previously granted or denied permissions. The syntax for granting privileges on a table in SQL Server is: GRANT privileges ON object TO user; privileges_name are the privileges granted to the user. (SELECT, INSERT, UPDATE, DELETE, REFERENCES, ALTER, ALL) SELECT Ability to perform SELECT statements on the table. INSERT Ability to perform INSERT statements on the table. UPDATE Ability to perform UPDATE statements on the table. DELETE Ability to perform DELETE statements on the table. REFERENCES Ability to create a constraint that refers to the table. ALTER Ability to perform ALTER TABLE statements to change the table definition. ALL ALL does not grant all permissions for the table. Rather, it grants the permissions which are SELECT, INSERT, UPDATE, DELETE, and REFERENCES. object is the name of the database object to which permissions are being granted. user is the name of the user to whom the privileges would be granted. If you wanted to grant SELECT on a table called student to a user name sunita, then run the following GRANT statement: GRANT SELECT ON students TO sunita; If you wanted to grant SELECT, INSERT, UPDATE, and DELETE privileges on a table called student to a user name sunita, then run the following GRANT statement: GRANT SELECT, INSERT, UPDATE, DELETE ON students TO sunita; You can also use the ALL keyword to indicate that you wish to grant the permissions (i.e.: SELECT, INSERT, UPDATE, DELETE, and REFERENCES) to a user named Sunita is written as: GRANT ALL ON students TO sunita; If you wanted to grant only SELECT access on the student table to all users, then grant the privileges to the public role as given here GRANT SELECT ON employees TO public;
  • 6. Database System Sunita M. Dol Page 6 Queries and Output: Conclusion: Thus we studies SQL queries for different integrity constraints like • Primary Key • Foreign Key • NOT NULL • CHECK • UNIQUE • DEFAULT and authorization commands like • GRANT and • REVOKE References: • Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) sixth edition. • Database system concepts by Abraham Silberschatz, Henry F. Korth, S. Sudarshan (McGraw Hill International Edition) fifth edition. • http://codex.cs.yale.edu/avi/db-book/db4/slide-dir/ • http://codex.cs.yale.edu/avi/db-book/db5/slide-dir/ • http://codex.cs.yale.edu/avi/db-book/db6/slide-dir/