Dbms Constraint Example
Dbms Constraint Example
CONSTRAINTS
Experiment Outcome
M1.02 Apply integrity constraints - PRIMARY KEY, NOT NULL, DEFAULT, CHECK, UNIQUE,
FOREIGN KEY
AIM:
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
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CONSTRAINT
<constraintname1> PRIMARY KEY,Columnname2 DATATYPE, columnname3
DATATYPE,.....);
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2 DATATYPE,
columnname3 DATATYPE, PRIMARY KEY (columnname1, columnname2));
(b)CHECK CONSTRAINT
Syntax:
CREATE TABLE tablename (Columnname1 DATATYPE CHECK (logical
expression), columnname2 DATATYPE, columnname3 DATATYPE,...);
Syntax
CREATE TABLE tablename (Columnname1 DATATYPE UNIQUE, columnname2
DATATYPE UNIQUE, columnname3 DATATYPE ...);
Syntax
CREATE TABLE tablename (Columnname1 DATATYPE, columnname2 DATATYPE,
columnname3 DATATYPE, UNIQUE (columnname1));
Syntax
CREATE TABLE tablename(Columnname1 DATATYPE NOT NULL, columnname2
DATATYPE NOT NULL,columnname3 DATATYPE,...);
Problems
ADDRESS1 Varchar 30
ADDRESS2 Varchar 30
CITY Varchar 15
PINCODE INT 8
STATE Varchar 15
BALDUE DECIMAL 10,2
b) Try to insert a record without specifing the UNIT MEASURE to PRODUCTMASTER Table
d) Try to insert two records with the same CLIENTNO to CLIENTMASTER 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)
RESULT: