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

My DBMS Lab 9

This document describes several SQL constraints: 1. A CHECK constraint is created on the "quantity" column of the "Persons" table to only allow integer values greater than 0. An error is generated when trying to insert a 0 value. 2. The ALTER command is used to add a CHECK constraint 'price_check' to the "price" column to only allow values greater than 0.0. 3. A CHECK constraint is added to the 'p_id' column of the 'orders' table to only allow certain values, and then this constraint is dropped. 4. A DEFAULT constraint is created on the "Planet" column of the "users" table to set

Uploaded by

Malik Mohsin
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

My DBMS Lab 9

This document describes several SQL constraints: 1. A CHECK constraint is created on the "quantity" column of the "Persons" table to only allow integer values greater than 0. An error is generated when trying to insert a 0 value. 2. The ALTER command is used to add a CHECK constraint 'price_check' to the "price" column to only allow values greater than 0.0. 3. A CHECK constraint is added to the 'p_id' column of the 'orders' table to only allow certain values, and then this constraint is dropped. 4. A DEFAULT constraint is created on the "Planet" column of the "users" table to set

Uploaded by

Malik Mohsin
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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