SlideShare a Scribd company logo
1 of 13
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

More Related Content

What's hot

T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .NetKathiK58
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql newSANTOSH RATH
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statementVivek Singh
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle DatabaseChhom Karath
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioningOpenSourceIndia
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)Huda Alameen
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Finalmukesh24pandey
 
Built-in Functions in SQL | Numeric Functions
Built-in Functions in SQL | Numeric FunctionsBuilt-in Functions in SQL | Numeric Functions
Built-in Functions in SQL | Numeric FunctionsRaj vardhan
 

What's hot (20)

Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
T sql denali code Day of .Net
T sql denali code Day of .NetT sql denali code Day of .Net
T sql denali code Day of .Net
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek SharmaIntroduction to Oracle Functions--(SQL)--Abhishek Sharma
Introduction to Oracle Functions--(SQL)--Abhishek Sharma
 
Les11
Les11Les11
Les11
 
Sql insert statement
Sql insert statementSql insert statement
Sql insert statement
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
 
Chetan postgresql partitioning
Chetan postgresql partitioningChetan postgresql partitioning
Chetan postgresql partitioning
 
Les02
Les02Les02
Les02
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Alter table command
Alter table commandAlter table command
Alter table command
 
Sql functions
Sql functionsSql functions
Sql functions
 
Aggregate Functions,Final
Aggregate Functions,FinalAggregate Functions,Final
Aggregate Functions,Final
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Built-in Functions in SQL | Numeric Functions
Built-in Functions in SQL | Numeric FunctionsBuilt-in Functions in SQL | Numeric Functions
Built-in Functions in SQL | Numeric Functions
 
Clauses
ClausesClauses
Clauses
 
Les17
Les17Les17
Les17
 
Oracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guideOracle Sql & PLSQL Complete guide
Oracle Sql & PLSQL Complete guide
 

Viewers also liked

Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1Vineeta Garg
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3Shaili Choudhary
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructorsVineeta Garg
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patilwidespreadpromotion
 
Working with Cookies in NodeJS
Working with Cookies in NodeJSWorking with Cookies in NodeJS
Working with Cookies in NodeJSJay Dihenkar
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Data Structure
Data StructureData Structure
Data Structuresheraz1
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++Vineeta Garg
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structurefaran nawaz
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarSivakumar R D .
 

Viewers also liked (20)

SQL
SQLSQL
SQL
 
Classes and objects1
Classes and objects1Classes and objects1
Classes and objects1
 
Advanced data structures using c++ 3
Advanced data structures using c++ 3Advanced data structures using c++ 3
Advanced data structures using c++ 3
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
Pp
PpPp
Pp
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Working with Cookies in NodeJS
Working with Cookies in NodeJSWorking with Cookies in NodeJS
Working with Cookies in NodeJS
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
C++ data types
C++ data typesC++ data types
C++ data types
 
Data Structure
Data StructureData Structure
Data Structure
 
Data file handling in c++
Data file handling in c++Data file handling in c++
Data file handling in c++
 
10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil10. Search Tree - Data Structures using C++ by Varsha Patil
10. Search Tree - Data Structures using C++ by Varsha Patil
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Queues in C++
Queues in C++Queues in C++
Queues in C++
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 

Similar to Structured query language constraints

Similar to Structured query language constraints (20)

Intro to tsql unit 9
Intro to tsql   unit 9Intro to tsql   unit 9
Intro to tsql unit 9
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
Sql modifying data - MYSQL part I
Sql modifying data - MYSQL part ISql modifying data - MYSQL part I
Sql modifying data - MYSQL part I
 
Les08
Les08Les08
Les08
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Les08
Les08Les08
Les08
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Mysql
MysqlMysql
Mysql
 
Data integrity
Data integrityData integrity
Data integrity
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14Ppt INFORMATIVE PRACTICES for class 11th chapter 14
Ppt INFORMATIVE PRACTICES for class 11th chapter 14
 
Crosstab query techniques
Crosstab query techniquesCrosstab query techniques
Crosstab query techniques
 
dbms lab manual
dbms lab manualdbms lab manual
dbms lab manual
 
dbms.pdf
dbms.pdfdbms.pdf
dbms.pdf
 
Manipulating data
Manipulating dataManipulating data
Manipulating data
 
Group-2.pdf
Group-2.pdfGroup-2.pdf
Group-2.pdf
 
Les02
Les02Les02
Les02
 
DOODB_LAB.pptx
DOODB_LAB.pptxDOODB_LAB.pptx
DOODB_LAB.pptx
 

Recently uploaded

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 

Recently uploaded (20)

Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 

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