You are on page 1of 4

Lab 7

Learning objectives:
 Learn to use NOT NULL constraint.
 Learn to use DEFAULT constraint.
 Learn to use UNIQUE constraint.
 Learn to use CHECK constraint.

7.0 Preliminaries

Create a new database named as “Lab7”.


CREATE DATABASE Lab7;

Select “Lab7” database for use.


USE Lab7;

7.1 NOT NULL Constraint


NOT NULL constraint ensures that a column cannot have a NULL value. This enforces a field to
always contain a value, which means that you cannot insert a new record, or update a record without
adding a value to this field.

Create a table “Customer” with attributes of customer id, name, address, and date of birth and make
sure that customer id cannot be left blank.

CREATE TABLE Customer (


CustomerID INT NOT NULL,
CustomerName VARCHAR(30),
CustomerAddress VARCHAR(50),
CustomerDateOfBirth DATE
);

Show the structure of “Customer” table.

DESCRIBE Customer;

Add NOT NULL constraint to name attribute of the ‘Customer’ table.

ALTER TABLE Customer


MODIFY CustomerName VARCHAR(30) NOT NULL;

Show the structure of “Customer” table.

DESCRIBE Customer;

Insert two new records in “Customer” table.

INSERT INTO Customer VALUES (1, ‘Carlos’, ‘Street 12


GreenVille’, ‘1985-09-29’);
INSERT INTO Customer (CustomerID, CustomerAddress) VALUES (2,
‘House Number 226 New Town’);
6.2 DEFAULT Constraint
The DEFAULT constraint provides a default value to a column when the INSERT INTO statement
does not provide a specific value.

Create a table “Product” with attributes of product id, name, and price. Set the product price to $100 if
no value is provided when a new product record is inserted.

CREATE TABLE Product (


ProductID INT,
ProductName VARCHAR(30),
ProductPrice DECIMAL(11,2) DEFAULT 100.00
);

Show the structure of “Product” table.

DESCRIBE Product;

Insert two new records in “Product” table.

INSERT INTO Product VALUES (1, ‘Apple’, 5.26);


INSERT INTO Product (ProductID, ProductName) VALUES (2,
‘Orange’);

Show all of the products.

SELECT * FROM Product;

Add DEFAULT constraint to name attribute of the ‘Customer’ table with default value of ‘John Doe’.

ALTER TABLE Customer


MODIFY CustomerName VARCHAR(30) NOT NULL DEFAULT ‘John Doe’;

Show the structure of “Customer” table.

DESCRIBE Customer;

Insert a new record in “Customer” table.

INSERT INTO Customer (CustomerID, CustomerAddress) VALUES (2,


‘House Number 226 New Town’);

Remove DEFAULT constraint from ‘Product’.

ALTER TABLE Product


ALTER COLUMN ProductPrice DROP DEFAULT;
6.3 UNIQUE Constraint
The UNIQUE Constraint prevents two records from having identical values in a particular column.

Create a table “Shipment” with attributes of shipment id, customer id, product id, quantity, and ship
date. Make sure that shipment id cannot be repeated.

CREATE TABLE Shipment (


ShipmentID INT Unique,
CustomerID INT,
ProductID INT,
Quantity INT,
ShipmentDate DATE
);

Insert new records in “Shipment” table.

INSERT INTO Shipment (ShipmentID) VALUES (1);


INSERT INTO Shipment (ShipmentID) VALUES (2);
INSERT INTO Shipment (ShipmentID) VALUES (3);
INSERT INTO Shipment (ShipmentID) VALUES (1);

To add UNIQUE constraint to an already existing table use alter table DDL.
Add UNIQUE constraint to id attribute of the ‘Customer’ table.

ALTER TABLE Customer


MODIFY CustomerID INT NOT NULL UNIQUE;

Remove UNIQUE constraint fom id attribute of the ‘Customer’ table.

ALTER TABLE Customer


DROP INDEX CustomerID;

6.4 CHECK Constraint


The CHECK Constraint enables a condition to check the value being entered into a record. If the
condition evaluates to false, the record violates the constraint and isn’t entered into the table.

Create a table “DummyCheck” with attributes of item and color. Make sure that color are only ‘blue’,
‘red’ or ‘green’.

CREATE TABLE DummyCheck (


Item VARCHAR(20),
Color VARCHAR(20) CHECK (Color IN (‘red’, ‘green’,
‘blue’))
);

Insert new records in “DummyCheck” table.

INSERT INTO DummyCheck VALUES (‘Baloon’, ‘red’);


INSERT INTO DummyCheck VALUES (‘Baloon’, ‘Blue’);
INSERT INTO DummyCheck VALUES (‘Baloon’, ‘Yellow’);
Add CHECK constraint to price attribute of the ‘Product’ table so that all prices must be less than
$500.

ALTER TABLE Product


ADD CONSTRAINT Product_Price_Check CHECK(ProductPrice < 500);

Insert new records in “Product” table.

INSERT INTO Product VALUES (3, ‘Banana’, 3.64);


INSERT INTO Product VALUES (4, ‘Apple Watch’, 699);

Remove CHECK constraint from price attribute of the ‘Product’ table.

ALTER TABLE Product


DROP CONSTRAINT Product_Price_Check;

Insert a new record in “Product” table.

INSERT INTO Product VALUES (4, ‘Apple Watch’, 699);

You might also like