Assignment 3 Study of Database Constraints - ShivpriyaAmale - 181021004
Assignment 3 Study of Database Constraints - ShivpriyaAmale - 181021004
THEORY:
1. Explain what is meant by database constraints.
• Constraints are nothing but the rules on the data columns of a table.
• They enforce limits to the data or type of data that can be inserted/updated/deleted from a
table.
• Constraints could be either on a column level or a table level.
1
OPERATIONS EXECUTED: (MENTION THE LIST AS IT)
1. Create primary key while creating the table.
2. Create primary key by executing alter on a defined table.
3. Check violation for Key/entity integrity constraints
4. Drop primary key
5. Create foreign key while creating the table.
6. Create foreign key by executing alter on a defined table.
7. Check violation of referential integrity constraints
8. Drop foreign key
9. Apply default constraint on a table while creating the table
10. Apply default constraint on a table while executing alter operation on the table
11. Check violation of default constraint on table.
12. Drop default constraint.
13. Apply check constraint on a table while creating the table
14. Apply check constraint on a table while executing alter operation on the table
15. Check violation of check constraint on table.
16. Drop check constraint
17. Apply unique constraint on a table while creating the table
18. Apply unique constraint on a table while executing alter operation on the table
19. Check violation of unique constraint on table.
20. Drop unique constraint
21. Apply Not Null constraint on a table.
22. Check violation of Not Null constraint on table.
23. Use of decimal data type.
OUTPUT: (Copy Paste your output code here from your Mariadb Command Prompt, remove
error lines form code)
Enter password: *************
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.6.3-MariaDB mariadb.org binary distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| amazon |
| amazonintern |
| hsptl_staff |
| information_schema |
| microsoft |
| mysql |
| performance_schema |
| reliance2021 |
| sys |
| vjti |
+--------------------+
10 rows in set (0.562 sec)
3
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| cid | int(11) | NO | PRI | NULL | |
| cname | char(10) | YES | | NULL | |
| dop | date | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.294 sec)
5
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 |
+----------+-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------+
1 row in set (0.052 sec)
MariaDB [flipcart]> create table orders (oid int, cid int, oname char(10), primary key(oid), foreign
key(cid) references customer(cid));
Query OK, 0 rows affected (0.625 sec)
6
`cid` int(11) DEFAULT NULL,
`oname` char(10) DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `cid` (`cid`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`cid`) REFERENCES `customer` (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 |
+--------+---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------+
1 row in set (0.000 sec)
MariaDB [flipcart]> alter table orders add constraint fk foreign key(cid) references customer(cid);
Query OK, 0 rows affected (1.916 sec)
Records: 0 Duplicates: 0 Warnings: 0
7
+-------+----------+------+-----+---------+-------+
3 rows in set (0.195 sec)
8
MariaDB [flipcart]> insert into orders values(333,3,"Keyboard");
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails
(`flipcart`.`orders`, CONSTRAINT `fk` FOREIGN KEY (`cid`) REFERENCES `customer`
(`cid`))
MariaDB [flipcart]> update customer set cid=3 where cname ="RAM";
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails
(`flipcart`.`orders`, CONSTRAINT `fk` FOREIGN KEY (`cid`) REFERENCES `customer`
(`cid`))
MariaDB [flipcart]> insert into customer values(3,"VYOM","1995-5-5");
Query OK, 1 row affected (0.056 sec)
MariaDB [flipcart]> create table employees(eid int,ename varchar(20),age int default 20);
Query OK, 0 rows affected (0.860 sec)
9
MariaDB [flipcart]> select * from employees;
+------+-------+------+
| eid | ename | age |
+------+-------+------+
| 1 | RAM | 19 |
| 2 | SHYAM | 20 |
+------+-------+------+
2 rows in set (0.000 sec)
MariaDB [flipcart]> alter table employees alter age set default 18;
10
Query OK, 0 rows affected (0.618 sec)
Records: 0 Duplicates: 0 Warnings: 0
11
MariaDB [flipcart]> desc payment;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| pid | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.036 sec)
12
MariaDB [flipcart]> alter table payment1 add constraint ch check (pid>1);
Query OK, 0 rows affected (1.614 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [flipcart]> create table orders2(id int unique, oname char (20));
Query OK, 0 rows affected (0.499 sec)
13
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| oname | char(20) | YES | UNI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.070 sec)
MariaDB [flipcart]> create table orders3 (id int NOT NULL,oname char(20) NOT NULL);
Query OK, 0 rows affected (0.594 sec)
14
+-------+----------+------+-----+---------+-------+
2 rows in set (0.055 sec)
15
+--------------------+
7 rows in set (0.001 sec)
16
| 1 | 88.89 |
| 2 | 88.80 |
| 2 | 88.89 |
+------+-------+
3 rows in set (0.000 sec)
17
18
19
20
21
(Please Note: A viva will be taken in October based on your individual assignments. Please
do not copy paste the contents from others writeups.)
*******End of the Assignment*******
22