You are on page 1of 13

Department of Computer Engineering

Academic Year 2021-2022

Jay Patel: 60004210114 SE

Experiment No-3

Aim: Create and populate database using Data Definition Language (DDL) and DML
Commands

Theory:

Structured Query Language (SQL) as we all know is the database language using which we can
perform certain operations on the existing database and, we can use this language to create a database.
SQL uses certain commands like Create, Drop, Insert, etc. to carry out the required tasks.

These SQL commands are categorized into four categories out of which two are detailed in this
experiment:

1. DDL – Data Definition Language


2. DML – Data Manipulation Language
Data Definition Language:
Data Definition Language or DDL consists of the SQL commands that can be used to define the
database schema. It simply deals with descriptions of the database schema and is used to create and
modify the structure of database objects in the database. DDL is a set of SQL commands used to
create, modify, and delete database structures but not data. These commands are normally not used by
a general user, who should be accessing the database via an application.

List of DDL commands:

• CREATE: This command is used to create the database or its objects (like table, index,
function, views, store procedure, and triggers).
• DROP: This command is used to delete objects from the database.
• ALTER: This is used to alter the structure of the database.
• TRUNCATE: This is used to remove all records from a table, including all spaces allocated
for the records are removed.
• COMMENT: This is used to add comments to the data dictionary.
• RENAME: This is used to rename an object existing in the database.
• DESCRIBE: This is used to describe information about table structure.

1
Department of Computer Engineering
Academic Year 2021-2022

1) Create Command

 Creating Database

Syntax: create database collegesystem;

 Creating table:

The create table command defines each column of the table uniquely. Each column has minimum of
three attributes:

- Name
- Datatype
- Size (column width)
Each table column definition is a single clause in the create table syntax. Each table column
definition is separated from the other by a comma. Finally, the SQL statement is terminated
with a semicolon.

SYNTAX:

CREATE TABLE tablename ( column1


datatype [NULL|NOT NULL],
column2 datatype [NULL|NOT NULL],

...

constraints

);

2
Department of Computer Engineering
Academic Year 2021-2022

Creating tables

mysql> create table departments(

-> d_id int NOT NULL,

-> d_name varchar(100) NOT NULL

-> );

mysql> create table college(

-> college_name varchar(100) primary key

-> );

3
Department of Computer Engineering
Academic Year 2021-2022

mysql> create table canteen(

-> Bill_no int primary key,

-> menu varchar(100) not null,

-> price int

-> );

mysql> create table student

-> (

-> Student_id int primary key,

-> Name varchar(110) not null,

-> DOB varchar(100) not null

-> );

4
Department of Computer Engineering
Academic Year 2021-2022

mysql> create table exam(

-> course_no int primary key,

-> subject varchar(100) not null,

-> Marks int not null);

mysql> create table library(

-> book_id int primary key,

-> Library_Incharge varchar(100) not


null,

-> no_ofbooks int not null);

mysql> create table staff(

-> staff_no int primary key,

-> staff_name varchar(100) not null,

-> salary int);

mysql> create table principal(

-> age int not null,

-> qualification varchar(100),

-> name varchar(100));

5
Department of Computer Engineering
Academic Year 2021-2022

2) ALTER

Using ALTER TABLE Command, we can modify our exiting table.

Adding New Column:

Syntax:

ALTER TABLE tablename

ADD (NewColumnName DataType(SIZE),...n);

E.g.

mysql> alter table exam

-> add column date varchar(100);

Dropping Column from a Table:

Syntax:

ALTER TABLE tablename

DROP COLUMN columnname;

E.g.

mysql> alter table staff

-> drop column salary;

6
Department of Computer Engineering
Academic Year 2021-2022

Modify existing Column:

Syntax:

ALTER TABLE tablename

MODIFY columnname newdatatype(SIZE);

E.g.

mysql> alter table exam

-> modify date


date;

7
Department of Computer Engineering
Academic Year 2021-2022

3) RENAME

Syntax:

RENAME TABLE tablename to newtablename;

E.g.
mysql> rename table departments to college_departments;

4) DROP:

Syntax:

DROP TABLE tablename;

E.g.

mysql> drop table worksin;

8
Department of Computer Engineering
Academic Year 2021-2022

5) TRUNCATE:

Get rid of all rows.

Syntax:

TRUNCATE TABLE tablename;

E.g.

mysql> truncate table

college_departments;

9
Department of Computer Engineering
Academic Year 2021-2022

6) SHOW:

To check existing databases and tables.

Syntax:

SHOW [DATABASES|TABLES];

E.g.

mysql> show

databases;

7) DESC:

It provides description of mentioned table.

Syntax: DESC
tablename
E.g.

mysql> desc departments;

10
Department of Computer Engineering
Academic Year 2021-2022

Data Modification Language (DML):

A data manipulation language (DML) is a family of computer languages including commands


permitting users to manipulate data in a database. This manipulation involves inserting data into
database tables, retrieving existing data, deleting data from existing tables and modifying existing
data. DML is mostly incorporated in SQL databases.

1) INSERT:

This command adds one or more records to a database table.

Syntax:

INSERT INTO tablename(col1, col2, …) VALUES(val1, val2,…);

E.g.:

mysql> insert into exam values(839 , 'Maths' , 100,'2022-01-12');

mysql> insert into exam values(256 , 'English' , 70,'2022-04-09');

2)

SELECT:

The SELECT statement is used to select data from a database.

Syntax:

SELECT col1, col2... FROM table1, table2,…;

E.g.

mysql> select * from exam;

3) UPDATE:

11
Department of Computer Engineering
Academic Year 2021-2022

The UPDATE statement is used to update existing records in a table.

Syntax:

UPDATE tablename SET

col1=val1, col2=val2,…

WHERE condition;

E.g.

mysql> update exam set

-> marks = 80

-> where subject = 'english';

4) DELETE:

This command removes one or more records from a table according to specified conditions.

Syntax:

DELETE FROM tablename

WHERE condition;

E.g.

mysql> delete from exam

-> where course_no = 256;

12
Department of Computer Engineering
Academic Year 2021-2022

Conclusion: Hence, we created the tables previously defined using DDL commands. We also
performed basic manipulation of data using DML commands.

13

You might also like