Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 05 Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Lab Manual 05 (CONSTRAINTS)

Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

1. MySQL create table with NULL CONSTRAINT


Using the default value as NOT NULL, while creating a MySQL table, it can
be enforced that a column in a table is not allowed to store NULL values.
If you want to create a table 'newauthor' where no columns are allowed to
store NULL VALUES the following statement can be used.
Example
If you want to create a table 'newauthor' where no columns are allowed to
store NULL VALUES the following statement can be used.

1. CREATE TABLE IF NOT EXISTS newauthor


2. (aut_id varchar(8) NOT NULL,
3. aut_name varchar(50) NOT NULL,
4. country varchar(25) NOT NULL,
5. home_city varchar(25) NOT NULL );

Here in the above statement the constraint 'NOT NULL' have been used to
exclude the NULL VALUE.
The following picture shows that, the columns will not accept the NULL
values.
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

2. MySQL UNIQUE CONSTRAINT (Table Level)


The UNIQUE constraint creates an index such that, all values in the index
column must be unique. An error occurs when anybody tries to add a new row
with a key value that already exists in that row.
Example
The MySQL statement stated below will create a table 'newauthor' with a
column 'aut_id' and 'aut_name' which will store unique values only since
UNIQUE (aut_id, aut_name) is used.

1. CREATE TABLE IF NOT EXISTS


2. newauthor(aut_id varchar(8) NOT NULL ,
3. aut_name varchar(50)
4. NOT NULL,
5. country varchar(25) NOT NULL,
6. home_city varchar(25)
7. NOT NULL,
8. UNIQUE (aut_id, aut_name));

3. Example of MySQL UNIQUE CONSTRAINT check unique value (Column


Level)
The MySQL statement stated below will create a table 'newauthor' with a
column 'aut_id' which is meant to store unique values only. Notice that
UNIQUE is used within the column definition

1. CREATE TABLE IF NOT EXISTS


2. newauthor(aut_id varchar(8) NOT NULL UNIQUE ,
3. aut_name varchar(50) NOT NULL,
4. country varchar(25)
5. NOT NULL,
6. home_city varchar(25) NOT NULL);
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

4. MySQL create table PRIMARY KEY CONSTRAINT on single column


In this page we have discussed how to set a PRIMARY KEY CONSTRAINT
on a column of a table.
Example
The MySQL statement stated below will create a table 'newauthor' in which
PRIMARY KEY set to the aut_id column. Notice that here PRIMARY KEY
keyword is used within the column definition.

1. CREATE TABLE IF NOT EXISTS


2. newauthor(aut_id varchar(8) NOT NULL PRIMARY KEY,
3. aut_name varchar(50) NOT NULL,
4. country varchar(25)
5. NOT NULL,
6. home_city varchar(25) NOT NULL);

5. MySQL create table PRIMARY KEY UNIQUE CONSTRAINT


In this topic we have discussed how to set PRIMARY KEY as well as UNIQUE
constraints on columns of a table while creating a table with CREATE TABLE
command.
Example
The MySQL statement stated below will create a table 'newauthor' in which
PRIMARY KEY is set to the aut_id column and UNIQUE is set to the
home_city column.

1. CREATE TABLE IF NOT EXISTS


2. newauthor(aut_id varchar(8) NOT NULL PRIMARY KEY,
3. aut_name varchar(50) NOT NULL,
4. country varchar(25) NOT NULL,
5. home_city varchar(25) NOT NULL UNIQUE);
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

6. MySQL create table PRIMARY KEY on multiple columns


MySQL allows you to set PRIMARY KEY on multiple columns of a table.
Doing this allows you to work on multiple columns as a single entity set as
PRIMARY KEY for a table.
Example
The MySQL statement stated below will create a table 'newauthor' in which
PRIMARY KEY is set with the combination of aut_id and home_city columns.

1. CREATE TABLE IF NOT EXISTS


2. newauthor(aut_id varchar(8) NOT NULL ,
3. aut_name varchar(50) NOT NULL,
4. country varchar(25) NOT NULL,
5. home_city varchar(25) NOT NULL,
6. PRIMARY KEY (aut_id, home_city));

7. MySQL create table with DEFAULT CONSTRAINT


While creating a table, MySQL allows you assign DEFAULT CONSTRAINTS
to columns. DEFAULT is used to set a default value for a column and is
applied using DEFAULT default_value; where default_value is the default
value set to the column.
Example
The MySQL statement stated below will create a table 'newpublisher' with a
PRIMARY KEY on 'pub_id' column and a default value for pub_id, pub_name,
pub_city and country columns.
The MySQL statement also sets the default value white space for pub_id,
pub_name, pub_city columns and 'Pakistan' as default value for country
column.
Here is the statement below.

1. CREATE TABLE IF NOT EXISTS newpublisher


2. (pub_id varchar(8) NOT NULL UNIQUE DEFAULT '' ,
3. pub_name varchar(50) NOT NULL DEFAULT '' ,
4. pub_city varchar(25) NOT NULL DEFAULT '' ,
5. country varchar(25) NOT NULL DEFAULT 'Pakistan',
6. PRIMARY KEY (pub_id));
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

8. MySQL create table with AUTO INCREMENT


MySQL allows you to set AUTO_INCREMENT to a column. Doing so will
increase the value of that column by 1 automatically, each time a new record
is added.
Example
The MySQL statement stated below will create a table 'newauthor' with a
PRIMARY KEY on 'id' column and the 'id' column is an auto incremented field.

1. CREATE TABLE IF NOT EXISTS newauthor


2. (id int NOT NULL AUTO_INCREMENT,
3. aut_id varchar(8),
4. aut_name varchar(50),
5. country varchar(25),
6. home_city varchar(25) NOT NULL,
7. PRIMARY KEY (id));
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

8. MySQL creating table with FOREIGN KEY CONSTRAINT


While creating (or modifying) a MySQL table, you can set a FOREIGN KEY
CONSTRAINT to a column of the table. A foreign key is a column or combination
of columns which can be used to set a link between the data in two tables.
PRIMARY KEY of a table is linked to the FOREIGN KEY of another table to
enhance data integrity.
Syntax:
FOREIGN KEY [column list] REFERENCES [primary key table] ([column list]);

Arguments
Name Description

column list A list of the columns on which FOREIGN KEY is to be set.

REFERENCES Keyword.

primary key table Table name which contains the PRIMARY KEY.

column list A list of the columns on which PRIMARY KEY is set in the primary key table.

Example
If you want to do the following tasks :
A new table 'newbook_mast' will be created.
The PRIMARY KEY for that table 'newbook_mast' is 'book_id'.
The FOREIGN KEY for the table 'newbook_mast' is 'aut_id'.
The 'aut_id' is the PRIMARY KEY for the table 'newauthor'.
The FOREIGN KEY 'aut_id' for the table 'newbook_mast' points to the PRIMARY
KEY 'aut_id' of the table 'newauthor'.
That means the 'aut_id's which are present in the 'newauthor' table, only those
authors will come in the 'newbook_mast' table.
Here is the MySQL statement below for the above tasks.

1. CREATE TABLE IF NOT EXISTS newbook_mast


2. (book_id varchar(15) NOT NULL PRIMARY KEY,
3. book_name varchar(50) ,
4. isbn_no varchar(15) NOT NULL ,
5. cate_id varchar(8) ,
6. aut_id varchar(8) ,
7. pub_id varchar(8) ,
8. dt_of_pub date ,
9. pub_lang varchar(15) ,
10. no_page decimal(5,0) ,
11. book_price decimal(8,2) ,
12. FOREIGN KEY (aut_id) REFERENCES newauthor(aut_id));
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

9. MySQL create table with FOREIGN KEY CONSTRAINT on multiple


columns
MySQL allows to assign FOREIGN KEY CONSTRAINTS on multiple columns
of a table. Doing this, more than one columns of a table is set with a
FOREIGN KEY CONSTRAINT referenced PRIMARY KEYs belonging to
different tables.
Example

1. CREATE TABLE IF NOT EXISTS newpurchase


2. (invoice_no varchar(12) NOT NULL UNIQUE PRIMARY KEY,
3. invoice_dt date ,
4. ord_no varchar(25) ,
5. ord_date date ,
6. receive_dt date ,
7. book_id varchar(8) ,
8. book_name varchar(50) ,
9. pub_lang varchar(8) ,
10. cate_id varchar(8) ,
11. receive_qty int(5) ,
12. purch_price decimal(12,2) ,
13. total_cost decimal(12,2) ,
14. FOREIGN KEY(ord_no,book_id) REFERENCES neworder(ord_no,book_id));
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

10. MySQL create table with FOREIGN KEY CONSTRAINT on multiple


tables
MySQL allows to assign FOREIGN KEY CONSTRAINTS on multiple tables.
Doing this, more than one columns of a table is set with a FOREIGN KEY
CONSTRAINT referenced to PRIMARY KEYs belonging to different tables.
Example

1. CREATE TABLE IF NOT EXISTS


2. newbook_mast (book_id varchar(15) NOT NULL PRIMARY KEY,
3. book_name varchar(50) ,
4. isbn_no varchar(15) NOT NULL ,
5. cate_id varchar(8),
6. aut_id varchar(8) ,
7. pub_id varchar(8) ,
8. dt_of_pub date ,
9. pub_lang varchar(15) ,
10. no_page decimal(5,0) ,
11. book_price decimal(8,2) ,
12. FOREIGN KEY(aut_id) REFERENCES newauthor(aut_id),
13. FOREIGN KEY(pub_id) REFERENCES newpublisher(pub_id) );
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

11. MySQL create table with CASCADE and RESTRICT


MySQL allows to create a table with CASCADE and RESTRICT options.
CASCADE option deletes or updates the row from the parent table
(containing PRIMARY KEYs), and automatically delete or update the
matching rows in the child table (containing FOREIGN KEYs).
RESTRICT option bars the removal (i.e. using delete) or modification (i..e
using update) of rows from the parent table.
Example

1. CREATE TABLE IF NOT EXISTS newpurchase


2. (invoice_no varchar(12) NOT NULL UNIQUE PRIMARY KEY,
3. invoice_dt date ,
4. ord_no varchar(25) ,
5. ord_date date , ca
6. receive_dt date ,
7. book_id varchar(8) ,
8. book_name varchar(50) ,
9. pub_lang varchar(8) ,
10. cate_id varchar(8) ,
11. receive_qty int(5) ,
12. purch_price decimal(12,2) ,
13. total_cost decimal(12,2) ,
14. FOREIGN KEY(ord_no,book_id) REFERENCES
15. neworder(ord_no,book_id)
16. ON UPDATE CASCADE ON DELETE RESTRICT,
17. FOREIGN KEY(cate_id) REFERENCES category(cate_id))
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

12. MySQL create table with SET NULL


MySQL allows you to create table with SET NULL option. Doing so will delete
or update the row from the parent table, and set the foreign key column or
columns in the child table to NULL.
You can use SET NULL for DELETE as well as UPDATE.
If SET NULL is used, you should not set NOT NULL options to the columns of
the child table (containing FOREIGN KEYS).
Example

1. CREATE TABLE IF NOT EXISTS newpurchase


2. (invoice_no varchar(12) NOT NULL UNIQUE PRIMARY KEY,
3. invoice_dt date ,
4. ord_no varchar(25) ,
5. ord_date date ,
6. receive_dt date ,
7. book_id varchar(8) ,
8. book_name varchar(50) ,
9. pub_lang varchar(8) ,
10. cate_id varchar(8) ,
11. receive_qty int(5) ,
12. purch_price decimal(12,2) ,
13. total_cost decimal(12,2) ,
14. FOREIGN KEY(ord_no,book_id) REFERENCES neworder
15. (ord_no,book_id)
16. ON UPDATE CASCADE ON DELETE
17. SET NULL,
18. FOREIGN KEY(cate_id) REFERENCES category(cate_id));
Lab Manual 05 (CONSTRAINTS)
Source: http://www.w3resource.com/mysql/creating-table-advance/constraint.php

13. MySQL create table with NO ACTION


NO ACTION option in MySQL is equivalent to RESTRICT.
Example

1. CREATE TABLE IF NOT EXISTS


2. newpurchase (invoice_no varchar(12) NOT NULL UNIQUE PRIMARY KEY,

3. invoice_dt date ,
4. ord_no varchar(25) ,
5. ord_date date ,
6. receive_dt date ,
7. book_id varchar(8) ,
8. book_name varchar(50) ,
9. pub_lang varchar(8) ,
10. cate_id varchar(8) ,
11. receive_qty int(5) ,
12. purch_price decimal(12,2) ,
13. total_cost decimal(12,2) ,
14. FOREIGN KEY(ord_no,book_id) REFERENCES
15. neworder(ord_no,book_id)
16. ON UPDATE CASCADE ON DELETE NO ACTION,
17. FOREIGN KEY(cate_id) REFERENCES category(cate_id));

You might also like