0% found this document useful (0 votes)
34 views

Assignment 3 Study of Database Constraints - ShivpriyaAmale - 181021004

Uploaded by

Shivpriya Amale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment 3 Study of Database Constraints - ShivpriyaAmale - 181021004

Uploaded by

Shivpriya Amale
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

LAB ASSIGNMENT 3(SUBJECT: DBMS)

(Please use this format for doing assignments)


SUBMISSION DATE: 14-09-2021
NAME OF STUDENT: SHIVPRIYA AMALE
BRANCH: MECHANICAL
ID: 181021004

AIM: Study of Database Constraints using SQL.


TOOL: MariaDB (If any other tool, please mention the name)
PROGRAMMING LANGUAGE: Structured Query language (SQL)

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.

2. Explain types of database constraints with an example.


a) Internal/Implied/Implicit constraint (Add a short description)
• Constraints that are applied in the data model is called Implicit constraints.
• They are inherent in the data models.
• E.g., duplicate tuples are not allowed in a relation
b) External/Schema based constraint (Add a short description)
• Constraints that are directly applied in the schemas of the data model using DDL
commands are called as schema-based constraints or Explicit constraints.
• Domain constraints, key constraints, Entity integrity constraints and Referential integrity
constraints are some of the sub-types of schema based constraints.
• E.g. Defining datatype of a column: Customer id can only be an integer (Domain
constraint)
c) Application level constraint (Add a short description)
• Constraints which cannot be applied using schema based constraints or are not inherent in
the database model are called application level constraints
• Programming languages are used to apply these constraints
• E.g.
1. Providing discounts to customers
2. Giving bonuses to employees (where the amount has some limits)

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

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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)

MariaDB [(none)]> create database flipcart;


Query OK, 1 row affected (0.273 sec)

MariaDB [(none)]> use flipcart;


Database changed

MariaDB [flipcart]> create table customer(cid int,cname char(10),dop date,primary key(cid));


Query OK, 0 rows affected (1.569 sec)

MariaDB [flipcart]> desc customer;

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)

MariaDB [flipcart]> insert into customer values(1,"RAM","1991-5-5");


Query OK, 1 row affected (0.174 sec)

MariaDB [flipcart]> select * from customer;


+-----+-------+------------+
| cid | cname | dop |
+-----+-------+------------+
| 1 | RAM | 1991-05-05 |
+-----+-------+------------+
1 row in set (0.001 sec)

MariaDB [flipcart]> insert into customer values (1,"SHYAM","1992-5-5");


ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
MariaDB [flipcart]> insert into customer values (NULL,"SHYAM",1992-5-5);
ERROR 1048 (23000): Column 'cid' cannot be null
MariaDB [flipcart]> alter table customer drop primary key(cid);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near '(cid)' at line 1
MariaDB [flipcart]> alter table customer drop primary key;
Query OK, 1 row affected (1.501 sec)
4
Records: 1 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> desc customer;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| cid | int(11) | NO | | NULL | |
| cname | char(10) | YES | | NULL | |
| dop | date | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.307 sec)

MariaDB [flipcart]> alter table customer add primary key(cid);


Query OK, 0 rows affected (1.716 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> alter table customer add constraint pk primary key(cid);


ERROR 1068 (42000): Multiple primary key defined
MariaDB [flipcart]> show create table customer;
+----------+-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------+
| Table | Create Table
|
+----------+-------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------+
| customer | CREATE TABLE `customer` (
`cid` int(11) NOT NULL,
`cname` char(10) DEFAULT NULL,
`dop` date DEFAULT NULL,

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)

MariaDB [flipcart]> desc orders;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| oid | int(11) | NO | PRI | NULL | |
| cid | int(11) | YES | MUL | NULL | |
| oname | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.072 sec)

MariaDB [flipcart]> show create table orders;


+--------+---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------+
| Table | Create Table
|
+--------+---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------+
| orders | CREATE TABLE `orders` (
`oid` int(11) NOT NULL,

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 drop foreign key;


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near '' at line 1
MariaDB [flipcart]> alter table orders drop constraint orders_ibfk_1;
Query OK, 0 rows affected (0.564 sec)
Records: 0 Duplicates: 0 Warnings: 0

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

MariaDB [flipcart]> desc orders;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| oid | int(11) | NO | PRI | NULL | |
| cid | int(11) | YES | MUL | NULL | |
| oname | char(10) | YES | | NULL | |

7
+-------+----------+------+-----+---------+-------+
3 rows in set (0.195 sec)

MariaDB [flipcart]> select * from customer;


+-----+-------+------------+
| cid | cname | dop |
+-----+-------+------------+
| 1 | RAM | 1991-05-05 |
+-----+-------+------------+
1 row in set (0.001 sec)

MariaDB [flipcart]> insert into customer values(2,"SHYAM","1995-5-5");


Query OK, 1 row affected (0.081 sec)

MariaDB [flipcart]> insert into orders values(111,1,"MobilePhon");


Query OK, 1 row affected (0.079 sec)

MariaDB [flipcart]> insert into orders values(222,2,"Pendrive");


Query OK, 1 row affected (0.076 sec)

MariaDB [flipcart]> select * from orders;


+-----+------+------------+
| oid | cid | oname |
+-----+------+------------+
| 111 | 1 | MobilePhon |
| 222 | 2 | Pendrive |
+-----+------+------------+
2 rows in set (0.000 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)

MariaDB [flipcart]> insert into employees values (1,"RAM",19);


Query OK, 1 row affected (0.093 sec)

MariaDB [flipcart]> select * from employees;


+------+-------+------+
| eid | ename | age |
+------+-------+------+
| 1 | RAM | 19 |
+------+-------+------+
1 row in set (0.000 sec)

MariaDB [flipcart]> insert into employees values(2,"SHYAM");


ERROR 1136 (21S01): Column count doesn't match value count at row 1
MariaDB [flipcart]> insert into employees(eid,ename) values(2,"SHYAM");
Query OK, 1 row affected (0.093 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]> insert into employees(eid,ename) values(3,"VYOM");


Query OK, 1 row affected (0.075 sec)

MariaDB [flipcart]> select * from employees;


+------+-------+------+
| eid | ename | age |
+------+-------+------+
| 1 | RAM | 19 |
| 2 | SHYAM | 20 |
| 3 | VYOM | 20 |
+------+-------+------+
3 rows in set (0.000 sec)

MariaDB [flipcart]> alter table employees alter age drop default;


Query OK, 0 rows affected (0.357 sec)
Records: 0 Duplicates: 0 Warnings: 0

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

MariaDB [flipcart]> desc employees;


+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| eid | int(11) | YES | | NULL | |
| ename | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | 18 | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.170 sec)

MariaDB [flipcart]> create table payment(pid int check(pid>1));


Query OK, 0 rows affected (1.017 sec)

MariaDB [flipcart]> insert into payment values(1);


ERROR 4025 (23000): CONSTRAINT `payment.pid` failed for `flipcart`.`payment`
MariaDB [flipcart]> insert into payment values(2);
Query OK, 1 row affected (0.093 sec)

MariaDB [flipcart]> select * from payment;


+------+
| pid |
+------+
| 2|
+------+
1 row in set (0.001 sec)

11
MariaDB [flipcart]> desc payment;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| pid | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.036 sec)

MariaDB [flipcart]> alter table payment modify pid int;


Query OK, 0 rows affected (0.826 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> insert into payment values(1);


Query OK, 1 row affected (0.036 sec)

MariaDB [flipcart]> select * from payment;


+------+
| pid |
+------+
| 2|
| 1|
+------+
2 rows in set (0.001 sec)

MariaDB [flipcart]> create table payment1(pid int);


Query OK, 0 rows affected (0.752 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)

MariaDB [flipcart]> desc orders2;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| oname | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.109 sec)

MariaDB [flipcart]> insert into orders2 values(1,"AAA"),(2,"BBB");


Query OK, 2 rows affected (0.089 sec)
Records: 2 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> alter table orders2 add constraint cu unique(oname);


Query OK, 0 rows affected (1.693 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> desc orders2;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |

13
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| oname | char(20) | YES | UNI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.070 sec)

MariaDB [flipcart]> alter table orders2 drop index cu;


Query OK, 0 rows affected (0.443 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> desc orders2;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| oname | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.056 sec)

MariaDB [flipcart]> create table orders3 (id int NOT NULL,oname char(20) NOT NULL);
Query OK, 0 rows affected (0.594 sec)

MariaDB [flipcart]> desc orders3;


+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| oname | char(20) | NO | | NULL | |

14
+-------+----------+------+-----+---------+-------+
2 rows in set (0.055 sec)

MariaDB [flipcart]> alter table orders3 drop oname;


Query OK, 0 rows affected (0.611 sec)
Records: 0 Duplicates: 0 Warnings: 0

MariaDB [flipcart]> desc orders3;


+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.039 sec)

MariaDB [flipcart]> insert into orders3 values(NULL);


ERROR 1048 (23000): Column 'id' cannot be null
MariaDB [flipcart]> show tables;
+--------------------+
| Tables_in_flipcart |
+--------------------+
| customer |
| employees |
| orders |
| orders2 |
| orders3 |
| payment |
| payment1 |

15
+--------------------+
7 rows in set (0.001 sec)

MariaDB [flipcart]> create table student(id int, marks decimal(4,2));


Query OK, 0 rows affected (0.620 sec)

MariaDB [flipcart]> desc student;


+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| marks | decimal(4,2) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.113 sec)

MariaDB [flipcart]> insert into student values(1,88.89);


Query OK, 1 row affected (0.050 sec)

MariaDB [flipcart]> insert into student values(2,88.8);


Query OK, 1 row affected (0.118 sec)

MariaDB [flipcart]> insert into student values(2,88.889);


Query OK, 1 row affected, 1 warning (0.059 sec)

MariaDB [flipcart]> select * from student;


+------+-------+
| id | marks |
+------+-------+

16
| 1 | 88.89 |
| 2 | 88.80 |
| 2 | 88.89 |
+------+-------+
3 rows in set (0.000 sec)

SNAPSHOTS OF YOUR CODE:

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

You might also like