My DBMS Lab 9

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

The Islamia University of Bahawalpur

University College of Engineering & Technology


Department of Cyber Security and Digital Forensics
LAB MANUAL Database Systems 5 TH Semester

LAB EXPERIMENT # 09
SQL Constraints

Student Name: Ukasha Awan Roll No: 05

Lab Instructor Signatures: Date:

OBJECTIVES:

1. SQL CHECK Constraint

The following SQL creates a CHECK constraint on the "quantity" column when the
"Persons" table is created. The CHECK constraint specifies that the column "quantity" must
only include integers greater than 0.

Now, a false input is being entered by inserting 0 in the second row, in order to make sure
that the constraint is doing it’s work. It is obviously violating the check constraint that is
why error are showing in the output.

Here we have correct the above mistake and the command is completed successfully
- Alter table to add check constraint

Creates a CHECK constraint ‘price_check’ on the "price" . The CHECK constraint specifies
that the column "price" must only include integers greater than 0.0

2. DROP a CHECK Constraint

In the following a check constraint ‘product_check’ is added on the ‘p_id’ while creating
the table ‘orders’. It specifies that ‘p_id’ must be one on these: '10023', '10024', '10025'

Now, the following is dropping the constraint that we have created earlier

3. SQL DEFAULT Constraint

The following SQL creates a DEFAULT constraint on the "Planet" column when the "users"
table is created. It will set the planet name as Earth for every row that does not specifies
its value

To check the above default constraint we have inserted two rows, one of which does not
insert the ‘planet’. For that particular row the default value is showed in the output.
Following are the results:

- Default functions:

The DEFAULT constraint can also be used to insert system values, by using functions like
the one used in the following table:

In the following result the column is auto-filled because of the default value set in the
above code:

You might also like