0% found this document useful (0 votes)
30 views17 pages

48 - Ai&ds - Dbms Exp 4 - Aditya Shukla

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

48 - Ai&ds - Dbms Exp 4 - Aditya Shukla

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

Experiment No.

: 04

Aim: Apply Basic DDL and DML Commands to Specified System.


Tool: - SQL Server

Theory:

DDL (Data Definition Language): DDL or Data Definition Language actually 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
database.

Following are the DDL commands.


CREATE: creates structure of table.

ALTER: changes structure of table

DROP: removes data, structure of table from database

RENAME: renames table name

TRUNCATE: removes data of table but structure remains as it is.

A. CREATE command

There are two CREATE statements available in SQL:


1. CREATE DATABASE
2. CREATE TABLE

CREATE DATABASE
A Database is defined as a structured set of data. So, in SQL the very first step to store the data
in a well structured manner is to create a database. The CREATE DATABASE statement is
used to create a new database.

Syntax:
CREATE DATABASE database_name;
Here, database_name: name of the database.
Example Query:

CREATE DATABASE my_database;


Once a database is created, you can check it in the list of
databases with the following SQL command:

48_AI&DS_DBMS_4_Aditya Shukla
SHOW DATABASES;
CREATE TABLE
We have learned above about creating databases. Now to store the data we need a table to do
that. The CREATE TABLE statement is used to create a table in SQL. We know that a table
comprises of rows and columns. So while creating tables we have to provide all the information
to SQL about the names of the columns, type of data to be stored in columns, size of the data etc.
Let us now dive into details on how to use CREATE TABLE statement to create tables in SQL.
Syntax:
CREATE TABLE table_name
(
column1 data_type(size),
column2 data_type(size),
column3 data_type(size),
....
);
Here, table_name: name of the table, column1: name of the first column.
data_type: Type of data we want to store in the particular column.
For example, int for integer data.
size: Size of the data we can store in a particular column. For example if for
a column we specify the data_type as int and size as 10 then this column can store an integer
number of maximum 10 digits.

Example Query:
This query will create a table named Students with three columns, ROLL_NO, NAME and
SUBJECT.

CREATE TABLE Students


(
ROLL_NO int(3),
NAME varchar(20),
SUBJECT varchar(20),
);
This query will create a table named Students. The ROLL_NO field is of type int and can store
an integer number of size 3. The next two columns NAME and SUBJECT are of type varchar

48_AI&DS_DBMS_4_Aditya Shukla
and can store characters and the size 20 specifies that these two fields can hold maximum of 20
characters.

B. ALTER (ADD, DROP, MODIFY)

ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is also
used to add and drop various constraints on the existing table.
1. ALTER TABLE – ADD
ADD is used to add columns into the existing table. Sometimes we may require to add additional
information, in that case we do not require to create the whole database again, ADD comes to
our rescue.
Syntax:
ALTER TABLE table_name
ADD (Columnname_1 datatype,
Columnname_2 datatype,

Columnname_n datatype);

2. ALTER TABLE – DROP

48_AI&DS_DBMS_4_Aditya Shukla
DROP COLUMN is used to drop column in a table. Deleting the unwanted columns from the
table.
Syntax:
ALTER TABLE table_name
DROP COLUMN column_name;

3. ALTER TABLE-MODIFY
It is used to modify the existing columns in a table. Multiple columns can also be modified at
once.

Syntax:

ALTER TABLE table_name


ALTER COLUMN column_name column_type;

Example QUERY:

1. To ADD 2 columns AGE and COURSE to table Student.

ALTER TABLE Student


ADD (AGE number(3),COURSE varchar(40));

2. MODIFY column COURSE in table Student

ALTER TABLE Student


ALTER COLUMN COURSE varchar(20);

After running the above query maximum size of Course Column is reduced to 20 from 40.
3. DROP column COURSE in table Student.

ALTER TABLE Student


DROP COLUMN COURSE;

C. DROP

DROP is used to delete a whole database or just a table. The DROP statement destroys the
objects like an existing database, table, index, or view.

A DROP statement in SQL removes a component from a relational database management


system.

Syntax:

48_AI&DS_DBMS_4_Aditya Shukla
DROP TABLE table_name;
DROP DATABASE database_name;
Examples:

DROP DATABASE database_name;

database_name: Name of the database to be deleted.

D. RENAME

You can not use the ALTER TABLE statement in SQL Server to rename a column in a table.
However, you can use sp_rename, though Microsoft recommends that you drop and recreate the
table so that scripts and stored procedures are not broken.

Syntax
1. The syntax to rename a table in SQL Server is:
sp_rename ‘old_table_name’ , ‘new_table_name’
Example:
sp_rename 'students', 'stud';
Here, students table is renamed to new name as stud.

2. The syntax to rename a column in an existing table in SQL Server (Transact-SQL) is:
sp_rename ‘table_name.old_column_name’ , ‘new_column_name’ ,’COLUMN’
Example:
sp_rename 'student.last_name', 'lname', 'COLUMN';
Here, last_name column of student table is renamed to new name as lname.

E. TRUNCATE

TRUNCATE statement is a Data Definition Language (DDL) operation that is used to mark the
extents of a table for deallocation (empty for reuse). The result of this operation quickly removes
all data from a table, typically bypassing a number of integrity enforcing mechanisms. It was
officially introduced in the SQL:2008 standard.
The TRUNCATE TABLE mytable statement is logically (though not physically) equivalent to

48_AI&DS_DBMS_4_Aditya Shukla
the DELETE FROM mytable statement (without a WHERE clause).

Syntax:
TRUNCATE TABLE table_name;

Here, table_name: Name of the table to be truncated.

Example:
TRUNCATE TABLE Student;
After running the above query Student table will be truncated, i.e, the data will be deleted but the
structure will remain in the memory for further operations.

DML (Data Manipulation Language) : The SQL commands that deals with the manipulation
of data present in database belong to DML or Data Manipulation Languages.

Examples of DML:

• SELECT – is used to retrieve data from the database.


• INSERT – is used to insert data into a table.
• UPDATE – is used to update existing data within a table.
• DELETE – is used to delete records from a database table.

Empty Table: Student

Id Name Age
1. Insert Statement

The INSERT statement of SQL is used to insert a new row in a table. There are two ways of
using INSERT INTO statement for inserting rows:
Only values: First method is to specify only the value of data to be inserted without the column
names.
Syntax:

INSERT INTO table_name VALUES (value1, value2, value3,...);


table_name: name of the table.
value1, value2,.. : value of first column, second column,... for the new record

Column names and values both: In the second method we will specify both the columns which
we want to fill and their corresponding values as shown below:
Syntax:

48_AI&DS_DBMS_4_Aditya Shukla
INSERT INTO table_name (column1, column2, column3,..) VALUES ( value1, value2,
value3,..);
table_name: name of the table.
column1: name of first column, second column ...
value1, value2, value3: value of first column, second column,... for the new record

Queries:
Method 1 (Inserting only values):
INSERT INTO Student VALUES ('1','HARSH','19');
Method 2 (Inserting values in only specified columns):
INSERT INTO Student (ID, NAME, Age) VALUES ('2','PRATIK','19');
Id Name Age

1 HARSH 19

2 PRATIK 19

Using SELECT in INSERT Statement


We can use the SELECT statement with INSERT INTO statement to copy rows from one table
and insert them into another table. The use of this statement is similar to that of INSERT INTO
statement. The difference is that the SELECT statement is used here to select data from a
different table. The different ways of using INSERT INTO SELECT statement are shown below:
Inserting all columns of a table: We can copy all the data of a table and insert into in a different
table.
Syntax:

INSERT INTO first_table SELECT * FROM second_table;


first_table: name of first table.
second_table: name of second table.

We have used the SELECT statement to copy the data from one table and INSERT INTO
statement to insert in a different table.
Inserting specific columns of a table: We can copy only those columns of a table which we
want to insert into in a different table.
Syntax:

INSERT INTO first_table(names_of_columns1) SELECT names_of_columns2 FROM


second_table;

first_table: name of first table.


second_table: name of second table.

48_AI&DS_DBMS_4_Aditya Shukla
names of columns1: name of columns separated by comma(,) for table 1.
names of columns2: name of columns separated by comma(,) for table 2.

We have used the SELECT statement to copy the data of the selected columns only from
the second table and INSERT INTO statement to insert in first table.
Copying specific rows from a table: We can copy specific rows from a table to insert into
another table by using WHERE clause with the SELECT statement. We have to provide
appropriate condition in the WHERE clause to select specific rows.

Syntax:
INSERT INTO table1 SELECT * FROM table2 WHERE condition;
first_table: name of first table.
second_table: name of second table.
condition: condition to select specific rows.

Queries:
Method 1(Inserting all rows and columns):

INSERT INTO Student SELECT * FROM ExternalStudent;


Method 2(Inserting specific columns):
INSERT INTO Student (ROLL_NO,NAME,Age) SELECT ROLL_NO, NAME, Age

FROM ExternalStudent;
Output:
This query will insert the data in the columns ROLL_NO, NAME and Age of the table
LateralStudent in the table Student and the remaining columns in the Student table will be
filled by null which is the default value of the remaining columns.

Select specific rows to insert:


INSERT INTO Student SELECT *
FROM ExternalStudent WHERE Age = 18;
2. Update Statement
The UPDATE statement in SQL is used to update the data of an existing table in database. We
can update single columns as well as multiple columns using UPDATE statement as per our
requirement.
Basic Syntax

UPDATE table_name SET column1 = value1, column2 = value2,...


WHERE condition;

48_AI&DS_DBMS_4_Aditya Shukla
table_name: name of the table
column1: name of first , second, third column....
value1: new value for first, second, third column....
condition: condition to select the rows for which the values of columns needs to be updated.

NOTE: In the above query the SET statement is used to set new values to the particular column
and the WHERE clause is used to select the rows for which the columns are needed to be
updated. If we have not used the WHERE clause then the columns in all the rows will be
updated. So the WHERE clause is used to choose the particular rows.

Id Name Age

1 HARSH 19

2 PRATIK 19

3 DEEP 20

Example Queries

Updating single column: Update the column NAME and set the value to ‘PRATIK’ in all the
rows where Age is 20.
UPDATE Student SET NAME = 'PRATIK' WHERE Age = 20;
Updating multiple columns: Update the columns NAME to ‘PRATIK’ and AGE to 18 where
ID is 1.
UPDATE Student SET NAME = 'PRATIK', AGE= 18 WHERE ID = 1;
Note: For updating multiple columns we have used comma(,) to separate the names and values
of two columns.

Omitting WHERE clause: If we omit the WHERE clause from the update query then all of the
rows will get updated.
UPDATE Student SET NAME = 'PRATIK';
3. Delete Statement

The DELETE Statement in SQL is used to delete existing records from a table. We can delete a
single record or multiple records depending on the condition we specify in the WHERE clause.

Basic Syntax:
DELETE FROM table_name WHERE some_condition;
table_name: name of the table
some_condition: condition to choose particular record.

48_AI&DS_DBMS_4_Aditya Shukla
Note: We can delete single as well as multiple records depending on the condition we provide in
WHERE clause. If we omit the WHERE clause then all of the records will be deleted and the
table will be empty.

Example Queries:
Deleting single record: Delete the rows where name is ‘Ram’. This will delete only one row.
DELETE FROM Student WHERE NAME = 'Deep';
Delete all of the records: There are two queries to do this as shown below,
query1: "DELETE FROM Student";
query2: "DELETE * FROM Student";
4. Select Statement
Select is the most commonly used statement in SQL. The SELECT Statement in SQL is used to
retrieve or fetch data from a database. We can fetch either the entire table or according to some
specified rules. The data returned is stored in a result table. This result table is also called result-
set.
With the SELECT clause of a SELECT command statement, we specify the columns that we
want to be displayed in the query result and, optionally, which column headings we prefer to see
above the result table.
The select clause is the first clause and is one of the last clauses of the select statement that the
database server evaluates. The reason for this is that before we can determine what to include in
the final result set, we need to know all of the possible columns that could be included in the
final result set.
Basic Syntax:

SELECT column1, column2


FROM table_name
Here, column1, column2: names of the fields of the table
table_name: from where we want to fetch

This query will return all the rows in the table with fields column1 , column2.
Example Queries:

1. To fetch the entire table or all the fields in the table:


SELECT * FROM table_name;
2. Query to fetch the fields ROLL_NO, NAME, AGE from the table Student:
SELECT ID, NAME, AGE FROM Student;
3. To fetch all the fields from the table Student:
SELECT * FROM Student;
4. To fetch the students where age is more than 18.

48_AI&DS_DBMS_4_Aditya Shukla
SELECT * FROM Student WHERE age>1
5. To fetch name of students where age is more than 18.
SELECT name FROM Student WHERE age>18

Result and Discussion:

48_AI&DS_DBMS_4_Aditya Shukla
48_AI&DS_DBMS_4_Aditya Shukla
48_AI&DS_DBMS_4_Aditya Shukla
48_AI&DS_DBMS_4_Aditya Shukla
48_AI&DS_DBMS_4_Aditya Shukla
Learning Outcomes: Students should have the ability to
LO 4.1: Develop programming logic for basic SQL commands.
LO 4.2: Compute the output of commands to create and edit the table.
Course Outcomes: Upon completion of the course students will be able to Solve and apply SQL
Queries on given Data.

Conclusion:
Thus I have successfully implemented various DDL and DML commands of DBMS on [Name of
the Project].

48_AI&DS_DBMS_4_Aditya Shukla
For Faculty Use

Correction Formative Timely completion Attendance /


Parameters Assessment of Practical [ 40%] Learning
[40%] Attitude [20%]

48_AI&DS_DBMS_4_Aditya Shukla

You might also like