Exp 3
Exp 3
Exp 3
Aim
To create an employee database to set various constraints
CONSTRAINTS:
Constraints are used to specify rules for the data in a table. If there is any violation between the
constraint and the data action, the action is aborted by the constraint. It can be specified when the
table is created (using CREATE TABLE statement) or after the table is created (using ALTER
TABLE statement).
1. NOT NULL:
When a column is defined as NOTNULL, then that column becomes a mandatory column. It
implies that a value must be entered into the column if the record is to be accepted for storage in
the table.
Syntax:
CREATE TABLE Table_Name (column_name data_type (size) NOT NULL, );
Example:
CREATE TABLE student (sno integer(3)NOT NULL, name varchar(10));
2. UNIQUE:
The purpose of a unique key is to ensure that information in the column(s) is unique i.e. a value
entered in column(s) defined in the unique constraint must not be repeated across the column(s).
A table may have many unique keys.
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) UNIQUE, ….);
Example:
CREATE TABLE student (sno integer(3) UNIQUE, name varchar(10));
3. CHECK:
Specifies a condition that each row in the table must satisfy. To satisfy the
constraint, each row in the table must make the condition either TRUE or unknown (due to a
null).
Syntax:
CREATE TABLE Table_Name(column_name data_type(size) CHECK(logical
expression), ….);
Example:
mysql> CREATE TABLE Persons (
-> ID int NOT NULL,
-> LastName varchar(255) NOT NULL,
-> FirstName varchar(255),
-> Age int,
-> CHECK (Age>=18)
-> );
Query OK, 0 rows affected (0.09 sec)
4. PRIMARY KEY:
Syntax:
Example:
5. FOREIGN KEY:
The foreign key is used to link one or more than one table together. It is also known as
the referencing key. A foreign key matches the primary key field of another table. It means a
foreign key field in one table refers to the primary key field of the other table. It identifies each
row of another table uniquely that maintains the referential integrity in MySQL.
A foreign key makes it possible to create a parent-child relationship with the tables. In this
relationship, the parent table holds the initial column values, and column values of child table
reference the parent column values. MySQL allows us to define a foreign key constraint on the
child table.
MySQL defines the foreign key in two ways:
Syntax:
[CONSTRAINT constraint_name]
FOREIGN KEY [foreign_key_name] (col_name, ...)
REFERENCES parent_tbl_name (col_name,...)
ON DELETE referenceOption
ON UPDATE referenceOption
constraint_name: It specifies the name of the foreign key constraint. If we have not provided
the constraint name, MySQL generates its name automatically.
col_name: It is the names of the column that we are going to make foreign key.
parent_tbl_name: It specifies the name of a parent table followed by column names that
reference the foreign key columns.
Reference_option: It is used to ensure how foreign key maintains referential integrity using ON
DELETE and ON UPDATE clause between parent and child table.
MySQL contains five different referential options, which are given below:
CASCADE: It is used when we delete or update any row from the parent table, the values of the
matching rows in the child table will be deleted or updated automatically.
SET NULL: It is used when we delete or update any row from the parent table, the values of the
foreign key columns in the child table are set to NULL.
RESTRICT: It is used when we delete or update any row from the parent table that has a
matching row in the reference(child) table, MySQL does not allow to delete or update rows in
the parent table.
NO ACTION: It is similar to RESTRICT. But it has one difference that it checks referential
integrity after trying to modify the table.
SET DEFAULT: The MySQL parser recognizes this action. However, the InnoDB and NDB
tables both rejected this action.
Example:
Table: customer
Table: contact
Syntax:
ALTER TABLE Table_Name ADD PRIMARY KEY (column_name);
Example:
ALTER TABLE student ADD PRIMARY KEY (sno);
(Or)
Syntax:
ALTER TABLE table_name ADD CONSTRAINT constraint_name
PRIMARY KEY(colname)
Example:
ALTER TABLE student ADD CONSTRAINT SN PRIMARY KEY(SNO)
Dropping integrity constraints in the alter table command:
Syntax:
ALTER TABLE Table_Name DROP constraint_name;
Example:
ALTER TABLE student DROP PRIMARY KEY;
(or)
Syntax:
ALTER TABLE student DROP CONSTRAINT constraint_name;
Example:
ALTER TABLE student DROP CONSTRAINT SN;
6. DEFAULT :
The DEFAULT constraint is used to insert a default value into a column. The
default value will be added to all new records, if no other value is specified.
Syntax:
Example:
CREATE TABLE Persons1 (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes'
);
Exercises-1
Name Type
---------- ----------------------
EMPNO INTEGER(6)
ENAME VARCHAR(20)
JOB VARCHAR(10)
DEPTNO INTEGER(3)
SAL INTEGER(7)
Q2. Add constraints to check, while entering the empid value (i.e) empid > 100 and
implement not null
constraints.
Thus the creation of employee database using various constraints has been executed
successfully
Creating an employee database to set various constraints and
Creation of Views Indexes, Save point.
Experiment No: 3(ii)
MySQL View
A view is a database object that has no values. Its contents are based on the base table. It
contains rowsand columns similar to the real table. In MySQL, the View is a virtual
table created by a query by joining one or more tables. It is operated similarly to the base
table but does not contain any data of its own. The View and table have one main difference
that the views are definitions built on top of other tables (or views). If any changes occur in
the underlying table, the same changes reflected in the View also.
Syntax
[WHERE conditions];
Indexes
Indexing makes columns faster to query by creating pointers to where data is
stored within a database.
Exercises-1
Exercises-2
RESULT
Thus the creation of view ,index and savepoint using employee database has been
executed successfully.