My DBMS Lab 9
My DBMS Lab 9
LAB EXPERIMENT # 09
SQL Constraints
OBJECTIVES:
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
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
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: