0% found this document useful (0 votes)
8 views6 pages

Exp 3 Wup

Uploaded by

satyamthesupreme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Exp 3 Wup

Uploaded by

satyamthesupreme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT NO:-03

Aim: Create a database using Data Definition Language (DDL) and apply
integrity constraints for the specified system

Theory:

Purpose of DDL:
Data Definition Language (DDL) allows users to:

● Define the structure of a database table.


● Specify relationships between different types of data.
● Implement integrity constraints to maintain data accuracy.
● Apply value constraints for data consistency.

Data Types in Database Tables:


When defining columns in a database table, various data types are employed, such as:

● Integer (int)
● Varchar (variable-length character string)
● Date
● Decimal (fixed-point decimal numbers)
● Boolean
● and more...

DDL Commands:
DDL commands facilitate the management of database structure. The primary DDL
commands include:

CREATE:
● Purpose: Create a new table or a new database.
● Example: CREATE TABLE TableName (column1 datatype, column2
datatype, ...);
ALTER:
● Purpose: Modify or change the structure of a database table.
● Example: ALTER TABLE TableName ADD COLUMN newColumn datatype;
DROP:
● Purpose: Delete a table, index, or view from the database.
● Example: DROP TABLE TableName;
TRUNCATE:
● Purpose: Delete records/data from a table, while retaining its structure.
● Example: TRUNCATE TABLE TableName;
RENAME:
● Purpose: Rename an object (e.g., table) in the database.
● Example: ALTER TABLE OldTableName RENAME TO NewTableName;

Create Database:
The CREATE DATABASE statement is used to create a new SQL database.

● Example: CREATE DATABASE NewDatabase;

These DDL commands provide users with powerful tools to shape and manage the
structure of their databases, enabling effective data organization and maintenance.

Data types
When a table is created, each column in the table is assigned a data type.
Some of the important data types are as follows

● Varchar2
● Char
● Number

• Entity Integrity constraint:


• The term Data integrity refers to the correctness and completeness of the data in a
database.
• Entity Integrity constraint are the part of the table definition that are used to limit
the values entered into the columns.
• There are Six types of Entity Integrity constraint.

Sr.no Entity Purpose


. Integrity
Constraint

1 NOT This constraint specifies that the column cannot have NULL or empty
NULL values. The below statement creates a table with NOT NULL constraint.
2 UNIQUE This constraint ensures that all values inserted into the column will be
unique. It means a column cannot stores duplicate values. MySQL allows
us to use more than one column with UNIQUE constraint in a table.

3 CHECK It controls the value in a particular column. It ensures that the inserted
value in a column must be satisfied with the given condition.

4 DEFAULT This constraint is used to set the default value for the particular column
where we have not specified any value.

5 PRIMARY This constraint is used to identify each record in a table uniquely. If the
KEY column contains primary key constraints, then it cannot be null or empty.

6 Foreign This constraint is used to link two tables together. It is also known as the
Key referencing key. A foreign key column matches the primary key field of
another table. It means a foreign key field in one table refers to the
primary key field of another table.

DDL Commands with example:


Let’s see each DDL command with an example.
Create
It is used to create a new table or a new database.
An example of the create command is as follows
create table student(stdname varchar(20) , branch varchar(20),college varchar(20), age number,
telephone number, address varchar(20));

A student table is created with the fields given below


Stdname Branch College Age Telephone Address

Alter
It is used to alter or change the structure of the database table
An example of the alter command is as follows
ALTER TABLE student ADD birthdate DATETIME

Drop
It is used to delete a table, index, or views from the database.
An example of the drop command is as follows
DROP TABLE student

Application of DDL commands on Specified Case study with Input & Output:

• Creating Table using constraint: EMP

create table emp

( eno int PRIMARY KEY,


ename varchar(20) UNIQUE,
job char(10) NOT NULL,
mgr int DEFAULT 1,
hiredate date,
sal float(9,2) CHECK(sal > 0),
comm float(9,2),
dno int,
FOREIGN KEY (dno) REFERENCES dept(dno)
);

• Creating Table using constraint: DEPT

create table dept


(
dno int primary key,
dname varchar(20),
loc varchar(10)
);
• Alter Table
• Is used to change definition of a table
• Example

• mysql> alter table emp
add grade char(2);
mysql>alter table emp
modify grade char(4);

• mysql> alter table emp


drop grade;

• Drop Table:
• Drop Table statement to delete the existing table. This statement removes the complete data
of a table along with the whole structure or definition permanently from the database.
• Example : mysql> drop table emp;
• Truncate table : TRUNCATE statement removes the complete data without removing its
structure.
• Example : mysql>truncate table emp;
• Rename table : to rename existing table name with new table name.

• Example: mysql>rename table emp to dmce;

You might also like