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