SlideShare a Scribd company logo
STRUCTURED QUERY
LANGUAGE
CONSTRAINTS, COMMIT AND ROLLBACK
CONSTRAINTS
Constraints are the rules which ensures the validity of data which is being
entered into the table.
CONSTRAIN
T
PURPOSE
PRIMARY
KEY
Sets a column or a group of columns as the Primary
Key of a table. Therefore, NULLs and Duplicate values
in this
column are not accepted.
NOT NULL Makes sure that NULLs are not accepted in the
specified column.
PRIMARY KEY CONSTRAINT
This constraint can be added at the time of creating a table:
Using Create Table Command:
Q. Create a table item having fields item no, name, price, quantity with item no as the Primary key.
Ans. Create table item ( itemno integer primary key, name char(20), price decimal(5,2), quantity integer);
OR
Create table item ( itemno integer , name char(20), price float(5,2), quantity integer, itemno primary key);
Q. Create a table item having fields item no, name, price, quantity with item no and name together as the
Primary key.
Ans. Create table item ( itemno integer , name char(20), price float(5,2), quantity integer, primary key(itemno,
name));
Note: Alter table command can also be used to add primary key constraint if the table is already created and we forgot to add this constraint at
the time of creating a table. This is discussed further in this presentation.
NOT NULL CONSTRAINT
NULL constraint when added to a column does not allow that particular column to
accept NULL values.
Q. Create a table item having fields item no, name, price, quantity with item no as the
Primary key and price should be NOT NULL.
Ans. Create table item ( itemno integer primary key, name char(20), price decimal(5,2)
NOT NULL, quantity integer);
VIEWING CONSTRAINT
DESC command is used to view the entire structure of the table along with the
constraints associated with its columns.
Q. WAC to display the structure of the table emp along with its constraints.
Ans. Desc item;
ADD, MODIFY AND REMOVE CONSTRAINTS
ALTER TABLE command is used to add, modify and delete constraint.
Q. WAC to add constraint PRIMARY KEY to the column item number of the table item;
Ans. Alter table item add primary key (itemno);
Q. WAC to make item number and name as the PRIMARY KEY of the table item.
Ans. Alter table item add primary key (itemno, name);
Q. WAC to delete the primary key constraint from the table item.
Ans. Alter table item drop primary key;
ADD, MODIFY AND REMOVE CONSTRAINT Contd….
The NOT NULL constraint can be added and removed by using MODIFY option of the
ALTER TABLE command.
Q. WAC to add a NOT NULL constraint to price column of the table item.
Ans. Alter table item modify price decimal(5,2) not NULL;
Q. WAC to remove a NOT NULL constraint from price column of the table item.
Ans. Alter table item modify price decimal(5,2) NULL;
ADVANCED RDBMS CONCEPTS
TRANSACTION: A transaction is a unit of work that must be done in logical
order and successfully as a group.
The statements which help to manage transaction are:
START TRANSACTION statement
COMMIT statement
ROLLBACK statement
START TRANSACTION
START TRANSACTION statement commits the current transaction and
starts a new transaction. It tells MySQL that the new transaction is
beginning and the statements that follow should be treated as a unit,
until this transaction ends.
SYNTAX:
START TRANSACTION;
Note: Start transaction statement does not take any clauses.
COMMIT
The COMMIT statement is used to save all changes made to the database during the
transaction to the database. Commit statement is issued at a time when the transaction is
Complete ie all the changes made to the database have been successful and the changes
should be saved to the database. COMMIT ends the current transaction.
SYNTAX:
COMMIT;
OR
COMMIT WORK;
Here WORK is a keyword and is optional.
INSERTING SAVEPOINT
The SAVEPOINT statement defines a book mark in a transaction. These book marks are useful in
rolling back a transaction till the book mark.
SYNTAX:
SAVEPOINT <savepoint name>;
Example:
SAVEPOINT Mark1;
In the above statement a save point with the name Mark1 is defined. It becomes a bookmark in
the transaction. Now the following statement will rollback the transaction till the bookmark named
Mark1.
AUTO COMMIT
 By default, Autocommit mode is on in MySQL. It means that MySQL does a COMMIT after
every SQL statement that does not return an error.
 When Autocommit is off then we have to issue COMMIT statement explicitly to save
changes made to the database.
 The following statement sets the autocommit mode to off. It also starts a new transaction
SET AUTOCOMMIT=0;
 The following statement sets the autocommit mode to ON. It also commits and terminates
the current transaction.
SET AUTOCOMMIT=1;
EXAMPLE
 mysql> SET AUTOCOMMIT = 0;
 mysql> INSERT INTO ITEM VALUES(103,'COFFEE TABLE',340);
 mysql> SELECT * FROM ITEM;
 mysql> ROLLBACK;
 mysql> SELECT * FROM ITEM;
 mysql> START TRANSACTION;
 mysql> UPDATE ITEM SET IPRICE = IPRICE +200;
 mysql> SAVEPOINT S1;
 mysql> UPDATE ITEM SET IPRICE = IPRICE +400;
 mysql> SELECT * FROM ITEM;
 mysql> ROLLBACK TO S1;
 mysql> SELECT * FROM ITEM;
 Mysql>SET AUTOCOMMIT ON;
 MYSQL> DELETE FROM ITEM WHERE IPRICE<200;
 Mysql> rollback;
Inserts a new record in the table item
Auto commit is disabled/off
Rolls back(undo) the insert command
Start transaction sets Auto commit off.
Increase the item price by Rs 200
Increase the item price by Rs 400
Sets the save point S1
Increase the item price by Rs 400, command will
be roll backed
Auto commit is set to on
Records with price>200 are deleted
Rollback cannot be done as auto commit
is on
Ad

Recommended

PPTX
Structured query language functions
Vineeta Garg
 
PPT
Single row functions
Balqees Al.Mubarak
 
PPT
Les10
arnold 7490
 
PPT
Les03
arnold 7490
 
PPT
Les01
arnold 7490
 
PPT
Single row functions
Soumyajit Dutta
 
PPT
Les09
arnold 7490
 
DOC
Sql functions
G C Reddy Technologies
 
PDF
T sql denali code Day of .Net
KathiK58
 
PPTX
Introduction to sql new
SANTOSH RATH
 
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
अभिषेक शर्मा
 
PPT
Les11
arnold 7490
 
PDF
Sql insert statement
Vivek Singh
 
PPTX
Interacting with Oracle Database
Chhom Karath
 
ODP
Chetan postgresql partitioning
OpenSourceIndia
 
PPT
Les02
arnold 7490
 
PDF
Structured query language(sql)
Huda Alameen
 
PDF
Alter table command
ravikhandelwal41
 
RTF
Sql functions
ilias ahmed
 
PPT
Aggregate Functions,Final
mukesh24pandey
 
DOCX
Built-in Functions in SQL | Numeric Functions
Raj vardhan
 
PPT
Les17
Vijay Kumar
 
PPT
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
PPTX
SQL
Vineeta Garg
 
PPTX
Classes and objects1
Vineeta Garg
 

More Related Content

What's hot (20)

PDF
T sql denali code Day of .Net
KathiK58
 
PPTX
Introduction to sql new
SANTOSH RATH
 
PPT
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
अभिषेक शर्मा
 
PPT
Les11
arnold 7490
 
PDF
Sql insert statement
Vivek Singh
 
PPTX
Interacting with Oracle Database
Chhom Karath
 
ODP
Chetan postgresql partitioning
OpenSourceIndia
 
PPT
Les02
arnold 7490
 
PDF
Structured query language(sql)
Huda Alameen
 
PDF
Alter table command
ravikhandelwal41
 
RTF
Sql functions
ilias ahmed
 
PPT
Aggregate Functions,Final
mukesh24pandey
 
DOCX
Built-in Functions in SQL | Numeric Functions
Raj vardhan
 
PPT
Les17
Vijay Kumar
 
PPT
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 
T sql denali code Day of .Net
KathiK58
 
Introduction to sql new
SANTOSH RATH
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
अभिषेक शर्मा
 
Sql insert statement
Vivek Singh
 
Interacting with Oracle Database
Chhom Karath
 
Chetan postgresql partitioning
OpenSourceIndia
 
Structured query language(sql)
Huda Alameen
 
Alter table command
ravikhandelwal41
 
Sql functions
ilias ahmed
 
Aggregate Functions,Final
mukesh24pandey
 
Built-in Functions in SQL | Numeric Functions
Raj vardhan
 
Oracle Sql & PLSQL Complete guide
Raviteja Chowdary Adusumalli
 

Viewers also liked (20)

PPTX
SQL
Vineeta Garg
 
PPTX
Classes and objects1
Vineeta Garg
 
DOCX
Advanced data structures using c++ 3
Shaili Choudhary
 
PPTX
Constructors and destructors
Vineeta Garg
 
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPS
Pp
sheraz1
 
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
PPTX
Stacks in c++
Vineeta Garg
 
PDF
Working with Cookies in NodeJS
Jay Dihenkar
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
C++ data types
pratikborsadiya
 
PPS
Data Structure
sheraz1
 
PPTX
Data file handling in c++
Vineeta Garg
 
PPTX
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
Stacks in algorithems & data structure
faran nawaz
 
PPT
stacks in algorithems and data structure
faran nawaz
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Queues in C++
Vineeta Garg
 
PPT
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Classes and objects1
Vineeta Garg
 
Advanced data structures using c++ 3
Shaili Choudhary
 
Constructors and destructors
Vineeta Garg
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
Stacks in c++
Vineeta Garg
 
Working with Cookies in NodeJS
Jay Dihenkar
 
Data types in c++
Venkata.Manish Reddy
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
C++ data types
pratikborsadiya
 
Data Structure
sheraz1
 
Data file handling in c++
Vineeta Garg
 
10. Search Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Stacks in algorithems & data structure
faran nawaz
 
stacks in algorithems and data structure
faran nawaz
 
Inheritance in c++
Vineeta Garg
 
Queues in C++
Vineeta Garg
 
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Ad

Similar to Structured query language constraints (20)

PPTX
Sql basics
Aman Lalpuria
 
PPTX
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
PDF
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
PPTX
Data base.ppt
TeklayBirhane
 
PPTX
Microsoft SQL 000000000000000000001.pptx
HaribabuKonakanchi1
 
PPTX
Creating database using sql commands
Belle Wx
 
PPTX
Advanced_SQL_Presentation_Template.pptx
SARASCHANDRA MAVALLAPALLI
 
PPT
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
PPTX
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
PPTX
Data Definition Language Commands in DBMS
agrawalmonikacomp
 
PPTX
Vansh Goel ISM (1).pptx
Akshit0808
 
PDF
MySQL notes - Basic Commands and Definitions
DeepakDeedarSingla
 
PPTX
SQL: Data Definition Language(DDL) command
sonali sonavane
 
PPTX
Unit1_3_Database Constraints.pptx igt is about DBMS
NishthaShah16
 
DOCX
SQL & PLSQL
Prakash Poudel
 
PDF
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
PDF
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
PDF
Relational database management system
Praveen Soni
 
PDF
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
PDF
Ijetr012023
ER Publication.org
 
Sql basics
Aman Lalpuria
 
2. DBMS Experiment - Lab 2 Made in SQL Used
TheVerse1
 
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
Data base.ppt
TeklayBirhane
 
Microsoft SQL 000000000000000000001.pptx
HaribabuKonakanchi1
 
Creating database using sql commands
Belle Wx
 
Advanced_SQL_Presentation_Template.pptx
SARASCHANDRA MAVALLAPALLI
 
CSE311_IAH_Slide07_SQL Advanced Quries.ppt
noshinnawar31
 
Database Akjljljlkjlkjkljlkjldiministration.pptx
EliasPetros
 
Data Definition Language Commands in DBMS
agrawalmonikacomp
 
Vansh Goel ISM (1).pptx
Akshit0808
 
MySQL notes - Basic Commands and Definitions
DeepakDeedarSingla
 
SQL: Data Definition Language(DDL) command
sonali sonavane
 
Unit1_3_Database Constraints.pptx igt is about DBMS
NishthaShah16
 
SQL & PLSQL
Prakash Poudel
 
ADBMS unit 1.pdfsdgdsgdsgdsgdsgdsgdsgdsg
zmulani8
 
Class XII-UNIT III - SQL and MySQL Notes_0.pdf
rohithlingineni1
 
Relational database management system
Praveen Soni
 
Chapter8 my sql revision tour
KV(AFS) Utarlai, Barmer (Rajasthan)
 
Ijetr012023
ER Publication.org
 
Ad

Recently uploaded (20)

PPTX
How to use _name_search() method in Odoo 18
Celine George
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
PDF
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
PPTX
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
PPTX
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PPTX
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PDF
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
PPTX
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PDF
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
How to use _name_search() method in Odoo 18
Celine George
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
How payment terms are configured in Odoo 18
Celine George
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
Great Governors' Send-Off Quiz 2025 Prelims IIT KGP
IIT Kharagpur Quiz Club
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
How to use search fetch method in Odoo 18
Celine George
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
Wage and Salary Computation.ppt.......,x
JosalitoPalacio
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 

Structured query language constraints

  • 2. CONSTRAINTS Constraints are the rules which ensures the validity of data which is being entered into the table. CONSTRAIN T PURPOSE PRIMARY KEY Sets a column or a group of columns as the Primary Key of a table. Therefore, NULLs and Duplicate values in this column are not accepted. NOT NULL Makes sure that NULLs are not accepted in the specified column.
  • 3. PRIMARY KEY CONSTRAINT This constraint can be added at the time of creating a table: Using Create Table Command: Q. Create a table item having fields item no, name, price, quantity with item no as the Primary key. Ans. Create table item ( itemno integer primary key, name char(20), price decimal(5,2), quantity integer); OR Create table item ( itemno integer , name char(20), price float(5,2), quantity integer, itemno primary key); Q. Create a table item having fields item no, name, price, quantity with item no and name together as the Primary key. Ans. Create table item ( itemno integer , name char(20), price float(5,2), quantity integer, primary key(itemno, name)); Note: Alter table command can also be used to add primary key constraint if the table is already created and we forgot to add this constraint at the time of creating a table. This is discussed further in this presentation.
  • 4. NOT NULL CONSTRAINT NULL constraint when added to a column does not allow that particular column to accept NULL values. Q. Create a table item having fields item no, name, price, quantity with item no as the Primary key and price should be NOT NULL. Ans. Create table item ( itemno integer primary key, name char(20), price decimal(5,2) NOT NULL, quantity integer);
  • 5. VIEWING CONSTRAINT DESC command is used to view the entire structure of the table along with the constraints associated with its columns. Q. WAC to display the structure of the table emp along with its constraints. Ans. Desc item;
  • 6. ADD, MODIFY AND REMOVE CONSTRAINTS ALTER TABLE command is used to add, modify and delete constraint. Q. WAC to add constraint PRIMARY KEY to the column item number of the table item; Ans. Alter table item add primary key (itemno); Q. WAC to make item number and name as the PRIMARY KEY of the table item. Ans. Alter table item add primary key (itemno, name); Q. WAC to delete the primary key constraint from the table item. Ans. Alter table item drop primary key;
  • 7. ADD, MODIFY AND REMOVE CONSTRAINT Contd…. The NOT NULL constraint can be added and removed by using MODIFY option of the ALTER TABLE command. Q. WAC to add a NOT NULL constraint to price column of the table item. Ans. Alter table item modify price decimal(5,2) not NULL; Q. WAC to remove a NOT NULL constraint from price column of the table item. Ans. Alter table item modify price decimal(5,2) NULL;
  • 8. ADVANCED RDBMS CONCEPTS TRANSACTION: A transaction is a unit of work that must be done in logical order and successfully as a group. The statements which help to manage transaction are: START TRANSACTION statement COMMIT statement ROLLBACK statement
  • 9. START TRANSACTION START TRANSACTION statement commits the current transaction and starts a new transaction. It tells MySQL that the new transaction is beginning and the statements that follow should be treated as a unit, until this transaction ends. SYNTAX: START TRANSACTION; Note: Start transaction statement does not take any clauses.
  • 10. COMMIT The COMMIT statement is used to save all changes made to the database during the transaction to the database. Commit statement is issued at a time when the transaction is Complete ie all the changes made to the database have been successful and the changes should be saved to the database. COMMIT ends the current transaction. SYNTAX: COMMIT; OR COMMIT WORK; Here WORK is a keyword and is optional.
  • 11. INSERTING SAVEPOINT The SAVEPOINT statement defines a book mark in a transaction. These book marks are useful in rolling back a transaction till the book mark. SYNTAX: SAVEPOINT <savepoint name>; Example: SAVEPOINT Mark1; In the above statement a save point with the name Mark1 is defined. It becomes a bookmark in the transaction. Now the following statement will rollback the transaction till the bookmark named Mark1.
  • 12. AUTO COMMIT  By default, Autocommit mode is on in MySQL. It means that MySQL does a COMMIT after every SQL statement that does not return an error.  When Autocommit is off then we have to issue COMMIT statement explicitly to save changes made to the database.  The following statement sets the autocommit mode to off. It also starts a new transaction SET AUTOCOMMIT=0;  The following statement sets the autocommit mode to ON. It also commits and terminates the current transaction. SET AUTOCOMMIT=1;
  • 13. EXAMPLE  mysql> SET AUTOCOMMIT = 0;  mysql> INSERT INTO ITEM VALUES(103,'COFFEE TABLE',340);  mysql> SELECT * FROM ITEM;  mysql> ROLLBACK;  mysql> SELECT * FROM ITEM;  mysql> START TRANSACTION;  mysql> UPDATE ITEM SET IPRICE = IPRICE +200;  mysql> SAVEPOINT S1;  mysql> UPDATE ITEM SET IPRICE = IPRICE +400;  mysql> SELECT * FROM ITEM;  mysql> ROLLBACK TO S1;  mysql> SELECT * FROM ITEM;  Mysql>SET AUTOCOMMIT ON;  MYSQL> DELETE FROM ITEM WHERE IPRICE<200;  Mysql> rollback; Inserts a new record in the table item Auto commit is disabled/off Rolls back(undo) the insert command Start transaction sets Auto commit off. Increase the item price by Rs 200 Increase the item price by Rs 400 Sets the save point S1 Increase the item price by Rs 400, command will be roll backed Auto commit is set to on Records with price>200 are deleted Rollback cannot be done as auto commit is on