You are on page 1of 6

EXPERIMENT NO:2

CONSTRAINTS

Experiment Outcome
M1.02 Apply integrity constraints - PRIMARY KEY, NOT NULL, DEFAULT, CHECK, UNIQUE,
FOREIGN KEY

AIM:

To implement Data Constraints.

THEORY
SQL constraints are used to specify rules for the data in a table. Constraints are used to limit
the type of data that can go into a table. This ensures the accuracy and reliability of the data
in the table. If there is any violation between the constraint and the data action, the action is
aborted.
TYPES OF CONSTRAINTS: 1) Primary key 2) Foreign key/references 3) Check 4) Unique 5)
Not null 6) Null 7) Default

PROCEDURE

(a) The PRIMARY KEY

The PRIMARY KEY defined at column level

Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CONSTRAINT
<constraintname1> PRIMARY KEY,Columnname2 DATATYPE, columnname3
DATATYPE,.....);

The PRIMARY KEY defined at table level

Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2 DATATYPE,
columnname3 DATATYPE, PRIMARY KEY (columnname1, columnname2));

(b)CHECK CONSTRAINT

The CHECK Constraint defined at column level

Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CHECK (logical
expression), columnname2 DATATYPE, columnname3 DATATYPE,...);

The CHECK Constraint defined at table level


Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2 DATATYPE,
columnname3 DATATYPE, CHECK (logical expression1), CHECK (logical
expression2));

(c) UNIQUE CONSTRAINT

The UNIQUE Constraint defined at the column level

Syntax
CREATE TABLE tablename (Columnname1 DATATYPE UNIQUE, columnname2
DATATYPE UNIQUE, columnname3 DATATYPE ...);

The UNIQUE Constraint defined at the the table level

Syntax
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2 DATATYPE,
columnname3 DATATYPE, UNIQUE (columnname1));

(d) Not Null

Syntax
CREATE TABLE tablename(Columnname1 DATATYPE NOT NULL, columnname2
DATATYPE NOT NULL,columnname3 DATATYPE,...);

Problems

1.Create the tables described below

Table Name : PRODUCT_MASTER

Description : used to store product information

Column name Data type size


PRODUCTNO Varchar 6 'Primary key'
DESCRIPTION Varchar 15 Not Null
PROFITPERCENT DECIMAL 4,2 Not Null
UNITMEASURE Varchar 10 Not Null
QTYONHAND INT 8 Not Null
REORDERLVL INT 8 Not Null
SELLPRICE DECIMAL 8,2 Not Null can not be zero
COSTPRICE DECIMAL 8,2 Not Null can not be zero

CREATE TABLE product_master(


productno VARCHAR(6) Primary key,
description VARCHAR(15) NOT NULL,
profirpercent DECIMAL(4,2) NOT NULL,
unitmeasure VARCHAR(10) NOT NULL,
qtyonhand INT(8) NOT NULL,
reorderlvl INT(8) NOT NULL,
sellprice DECIMAL(8,2) NOT NULL CHECK (sellprice > 0),
costprice DECIMAL(8,2) NOT NULL CHECK (costprice > 0));

Table Name : CLIENT_MASTER

Description : used to store client information

Column name Data Type Size


CLIENTNO Varchar 6 Primary
key/first letter must start with ‘C’
NAME Varchar 20 Not Null

ADDRESS1 Varchar 30
ADDRESS2 Varchar 30
CITY Varchar 15
PINCODE INT 8
STATE Varchar 15
BALDUE DECIMAL 10,2

CREATE TABLE client_master(


clientno VARCHAR(6) PRIMARY KEY,
name VARCHAR(20) NOT NULL,
address1 VARCHAR(30),
address2 VARCHAR(30),
city VARCHAR(30),
pincode INT(6),
state VARCHAR(20),
baldue DECIMAL(10,2));

Table Name : SALESMAN_MASTER


Description : used to store salesman information working for the company

Column name Data Type Size


SALESMANNO Varchar 6 Primary key/first letter must
start with ‘S’
SALESMANNAME Varchar 20 Not null
ADDRESS1 Varchar 30 Not null
ADDRESS2 Varchar 30
CITY Varchar 15
PINCODE INT 8
STATE Varchar 15
SALARY DECIMAL 8,2

CREATE TABLE salesman_master(


salesmanno VARCHAR(6) PRIMARY KEY,
salesmanname VARCHAR(30) NOT NULL,
address1 VARCHAR(30) NOT NULL,
address2 VARCHAR(30) NOT NULL,
city VARCHAR(20),
pincode INT(8),
state VARCHAR(15),
salary DECIMAL(8,2));

2. Exercise to verify the constaints


a) Try to insert two records with the same PRODUCTNO to PRODUCTMASTER Table

MariaDB [test]> insert into product_master values('900001','HDD 500


GB HDD',5,'NOs',25,5,7500,6000);
Query OK, 1 row affected (0.004 sec)

MariaDB [test]> insert into product_master values('900001','HDD 1 TB


HDD',5,'NOs',25,5,8000,6500);
ERROR 1062 (23000): Duplicate entry '900001' for key 'PRIMARY'

b) Try to insert a record without specifing the UNIT MEASURE to PRODUCTMASTER Table

MariaDB [test]> insert into product_master(productno,


description,profirpercent, qtyonhand, reorderlvl, sellprice,
costprice) values('900002','HDD 1 TB HDD',5,25,5,8000,6500);

c) Try to insert a record without specifing the SELLPRICE and COSTPRICE to


PRODUCTMASTER Table
insert into
product_master(productno,description,profirpercent,unitmeasure,qtyon
hand,reorderlvl) values('900002','HDD 1 TB HDD',5,'Nos',25,5);

d) Try to insert two records with the same CLIENTNO to CLIENTMASTER Table

MariaDB [test]> insert into client_master values(10001,'jack


danil','Jack nivas','Cochin',680432,'Kerala',34322);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
MariaDB [test]> insert into client_master values(10001,'jack
danil','Jack nivas','SKS Line','Cochin',680432,'Kerala',34322);
Query OK, 1 row affected (0.006 sec)

MariaDB [test]> insert into client_master values(10001,'Manoj


Prabakar','Sadan','Indira
Nagar','Bangaluru',680432,'Karnataka',33342);
ERROR 1062 (23000): Duplicate entry '10001' for key 'PRIMARY'

e) set 'Kerala' as the default value for STATE in SALESMAN_MASTER table

ALTER TABLE client_master MODIFY state VARCHAR(20) DEFAULT 'Kerala';


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

f) Try to insert a record without specifing the STATE to SALESMAN_MASTER Table

insert into
client_master(clientno,name,address1,address2,city,pincode,baldue)
values(10002,'Manoj Prabakar','MP Sadan','Indira
Nagar','Thrissur',680532,33342);
Query OK, 1 row affected (0.003 sec)
MariaDB [test]> select * from client_master;
+----------+----------------+------------+--------------+----------+---------+--------+----------+
| clientno | name | address1 | address2 | city | pincode | state | BALDUE |
+----------+----------------+------------+--------------+----------+---------+--------+----------+
| 10001 | jack danil | Jack nivas | SKS Line | Cochin | 680432 | Kerala | 34322.00 |
| 10002 | Manoj Prabakar | MP Sadan | Indira Nagar | Thrissur | 680532 | Kerala | 33342.00 |
+----------+----------------+------------+--------------+----------+---------+--------+----------+
2 rows in set (0.001 sec)

g) Add a field ADHARNO as UNIQUE field to SALESMAN_MASTER Table

MariaDB [test]> alter table salesman_master2 ADD adharnumber INT(12)


UNIQUE;
Query OK, 0 rows affected (0.057 sec)
Records: 0 Duplicates: 0 Warnings: 0
h) Try to add two record with same Adharnumber

MariaDB [test]> INSERT INTO salesman_master2


VALUES('500001','Raju','RR Bavan','MG Road',
'Thrissur',680020,'Kerala',342332,234523452343);
Query OK, 1 row affected, 1 warning (0.005 sec)

MariaDB [test]> INSERT INTO salesman_master2


VALUES('500002','Vinode','Vinod Bavan','MG Road',
'Thrissur',680020,'Kerala',342332,234523452343);
ERROR 1062 (23000): Duplicate entry '2147483647' for key
'adharnumber'
MariaDB [test]>

RESULT:

The DDL commands with constraints have been executed successfully

You might also like