You are on page 1of 32

DEPARTMENT OF INFORMATION TECHNOLOGY

DEPARTMENT OF INFORMATION TECHNOLOGY


Practical File

B. Tech (IT) 3rd Year – 5thSemester


Subject: DBMS Lab (BTIT 505 -18)

CHANDIGARH ENGINEERING COLLEGE


(LANDRAN)

Faculty: Submitted by:


Dr. Amanpreet Kaur Hritik Sharma
Associate Professor-IT 1902629

Hritik Sharma 1902629 1


DEPARTMENT OF INFORMATION TECHNOLOGY

INDEX
Sr No. Title Date Page No.

1. To study and execute Data Definition 03-08-2021 3-6


Language (DDL) command

2. Data Definition Language Commands 7-10


(DDL) –RENAME Table,
CONSTRAINTS on data.

3. Adding Constraint to the attributes of a 11-18


relation and Data Manipulation
Commands (DML): Insert, Update.

4. Data Manipulation Commands (DML): 19-21


DELETE, SELECT

5 SQL using- where; OR, AND and OR 22-32


operators

10

Hritik Sharma 1902629 2


DEPARTMENT OF INFORMATION TECHNOLOGY

Experiment No.-1

Title: To study and execute Data Definition Language (DDL) command

Objective: 1. To study DDL command in oracle SQL.

• Create table

• Alter Table

o Add column

o Drop Column

o Alter / Modify Column

• Drop table

Theory:

DDL – (DATA DEFINITION LANGUAGE): The SQL sentences that are used to

create these objects are called DDL’s or Data Definition Language. The sql provides various

commands for defining relation schemas, deleting relations, creating indexes and modify relation

schemas. DDL is part of sql which helps a user in defining the data structures into the database.

Following are the various DDL commands are :

• Alter table & Create table & drop table

• Create index & drop index

• Create view & drop view

1. CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns.

To create a table we will name the table and the columns of the table. We follow the rules to

Hritik Sharma 1902629 3


DEPARTMENT OF INFORMATION TECHNOLOGY

name tables and columns:-

• It must begin with a letter and can be up to 30 characters long.

• It must not be duplicate and not any reserved word.

SYNTAX to create a table is:

CREATE TABLE tablename (column_name1 datatype (size), column_name2 datatype (size) …);

Example is:

CREATE TABLE Hritiksharma_1902629 (

Rollno int,

Name varchar,

Contactno int

);

2. ALTER TABLE: After creating a table one may have need to change the table either by add

new columns or by modify existing columns. One can do so by using alter table command.

SYNTAX to add a column is:

ALTER TABLE tablename ADD(col1 datatype,col2 datatype);

Example is:

ALTER TABLE Hritiksharma_1902629

ADD (email varchar2, address varchar2);

SYNTAX to modify a column is:

ALTER TABLE tablename MODIFY(col1 datatype,col2 datatype);

Example is:

ALTER TABLE Hritiksharma_1902629

MODIFY(Contactno int);

Hritik Sharma 1902629 4


DEPARTMENT OF INFORMATION TECHNOLOGY

SYNTAX to Drop Column a column is:

ALTER TABLE tablename

DROP COLUMN Column_name;

Example is:

ALTER TABLE Hritiksharma_1902629

DROP COLUMN email;

3. DROP TABLE: To remove the definition of oracle table, the drop table statement is used.

SYNTAX to drop table is:

DROP TABLE tablename ;

Example is:

DROP TABLE Hritiksharma_1902629;

Output:

Hritik Sharma 1902629 5


DEPARTMENT OF INFORMATION TECHNOLOGY

Hritik Sharma 1902629 6


DEPARTMENT OF INFORMATION TECHNOLOGY

EXPERIMENT NO. – 2

AIM: Data Definition Language Commands (DDL) –RENAME Table, CONSTRAINTS on data.

OBJECTIVE: To study and execute the following DDL commands in SQL.

• • RENAME Table

• • Apply Constraints on Table Attributes

• 1. Primary key

• 2. Not null

THEORY: Structured Query Language (SQL) is the database language, which performs certain

operations on the existing database and also this language is used to create database.

The SQL commands are categorized into four categories:

1. DDL – Data Definition Language

2. DQL – Data Query Language

3. DML – Data Manipulation Language

4. DCL – Data control Language.

DDL (Data Definition Language): It consists of the SQL commands that can define the database

schema. It deals with descriptions of the database schema and is used to create and modify the

structure of the database objects in the database.

Commands that can be performed in DDL,

• RENAME TABLE – It is used to rename an existing table in the database.

Syntax of RENAME TABLE Command:

Hritik Sharma 1902629 7


DEPARTMENT OF INFORMATION TECHNOLOGY

Rename Old Table Name to New Table Name;

Example:

Rename Hritik_1902629_1 to Hritik_1902629_2;

Screen Shot:

To

Hritik Sharma 1902629 8


DEPARTMENT OF INFORMATION TECHNOLOGY

CONSTRAINTS:

1. Primary Key:

• The Primary Key constraint uniquely identifies each record in a table.

• Primary Key must contain UNIQUE values, and cannot contain NULL values.

• A table can have only one Primary Key.

Syntax:

Alter Table TableName ADD Primary Key (Attribute);

Or

Primary Key(attribute);

Example:

Alter table Hritik_1902629_2

Add Primary Key(RollNo);

SCREEN SHOT:

Hritik Sharma 1902629 9


DEPARTMENT OF INFORMATION TECHNOLOGY

2. Not Null:

• By default, a column can hold NULL values.

• The NOT NULL constraint enforces a column to NOT accept NULL values.

Syntax:

Alter Table TableName

Modify Attribute Not Null;

Or

Attribute NOT NULL;

Example:

Alter table Hritik_1902629_2

Modify Name Not Null;

Alter table Hritik_1902629_2

Modify City Not Null;

Screen Shot:

Hritik Sharma 1902629 10


DEPARTMENT OF INFORMATION TECHNOLOGY

Experiment No:3

Aim: Adding Constraint to the attributes of a relation and Data Manipulation


Commands (DML): Insert, Update.

Objective: 1. To add the following Constraints to a column in newly created table.

• Primary Key
• Not Null
• Unique
• Check

2. To Study and Execute following DML commands in SQL

• INSERT INTO
• UPDATE

Theory:

Data Manipulation Language: 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.

Hritik Sharma 1902629 11


DEPARTMENT OF INFORMATION TECHNOLOGY

Following are the Commands in DML:

• Select: It is used to retrieve data from database.


• Insert: It is used to insert data in database.
• Update: It is used to update existing data within the database.
• Delete: It is used to delete all the records from the table.

Rename Table:- RENAME TABLE is used to change the name of a table. Sometimes,
we choose non-meaningful name for the table. So it is required to be changed.

Syntax to Rename Table:


Rename old_table_name to new_table_name;

Example:
Rename Hritik_1902629_1 to Hritik_1902629_2

Output:

To

Hritik Sharma 1902629 12


DEPARTMENT OF INFORMATION TECHNOLOGY

1. Adding Constraints to Columns

• Primary Key: A Combination of Unique and not Not Null. Uniquely identifies
each row.

Syntax:
Alter Table Table_Name
ADD Primary Key (Attribute);

Example:

Alter table Hritik_1902629_2


Add primary key(RollNo);

Output:

Hritik Sharma 1902629 13


DEPARTMENT OF INFORMATION TECHNOLOGY

• Not Null: Ensures that the column cannot have a Null value
Syntax:
Alter table table_name
Modify attribute Not Null;
Example:
Alter table Hritik_1902629_2
Modify Name Not Null;
Alter table Hritik_1902629_2
Modify City Not Null;
Output:

• Unique: Ensures all the values in column are different.

Hritik Sharma 1902629 14


DEPARTMENT OF INFORMATION TECHNOLOGY

Syntax:
Alter table table_name
Modify attribute Unique;

Example:
Alter table Hritik_1902629_2
Modify Contact Unique;

Output:

• Check: Ensures that the values in the column satisfies a specific condition.

Syntax:
Alter table table_name
Modify attribute check(constraint);

Example:
Alter table Hritik_1902629_2
Modify Age check(Age>16);

Hritik Sharma 1902629 15


DEPARTMENT OF INFORMATION TECHNOLOGY

Output:

2. Executing DML Commands:


• Insert Into: It is used to add new rows of data to a table in the database.
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Or
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Example:
Insert Into Hritik_1902629_2(RollNo, Name, City, Age, Contact)
Values(1, 'Rahul', 'Ludhiana',17,12345);
Insert Into Hritik_1902629_2
Values (2, 'Rajan', 'Delhi',18,123456);
Insert Into Hritik_1902629_2
Values(3, 'Rohit', 'Dadri',19,1234567);
Insert Into Hritik_1902629_2
Values(4, 'Shweta', 'Mumbai',20,12345678);
Insert Into Hritik_1902629_2
Values(5, 'Rajat', 'Kanpur',21,123456789);
Output:

Hritik Sharma 1902629 16


DEPARTMENT OF INFORMATION TECHNOLOGY

• Update: It is used to modify the existing records in a table. You can use the
WHERE clause with the UPDATE query to update the selected rows, otherwise all
the rows would be affected.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
Update Hritik_1902629_2
Set City='Jaipur'

Hritik Sharma 1902629 17


DEPARTMENT OF INFORMATION TECHNOLOGY

where Name='Rajan';
Output:

Hritik Sharma 1902629 18


DEPARTMENT OF INFORMATION TECHNOLOGY

Experiment No. - 4

Aim: Data Manipulation Commands (DML): DELETE, SELECT


Objectives : To study and execute following DML commands in SQL.
1. DELETE
2. SELECT

Theory:

Data Manipulation Language: 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.

Following are the Commands in DML:

• Select: It is used to retrieve data from database.


• Insert: It is used to insert data in database.
• Update: It is used to update existing data within the database.
• Delete: It is used to delete all the records from the table.

1. Delete: It is used to delete all the records from the table.

Syntax to Delete Table:


DELETE FROM table_name WHERE condition;

Example:
DELETE FROM Hritik_1902629 WHERE Rollno= 1902643;

Output:

Hritik Sharma 1902629 19


DEPARTMENT OF INFORMATION TECHNOLOGY

To

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

data returned is stored in a result table, called the result-set.

Syntax for SELECT STATEMENT:


SELECT * FROM table_name;

Example:
Insert Into Hritik_1902629 ( Rollno,Name,Contactno, DOB )
Values (1902629, 'Hritik', 123456789,12345);
Insert Into Hritik_1902629 ( Rollno,Name,Contactno, DOB )
Values (1902643, 'krish', 1236789,00000);

Hritik Sharma 1902629 20


DEPARTMENT OF INFORMATION TECHNOLOGY

Insert Into Hritik_1902629 ( Rollno,Name,Contactno, DOB )


Values (1902642, 'kartik', 4136789,20000);
Insert Into Hritik_1902629 ( Rollno,Name,Contactno, DOB )
Values (1902640, 'Manav', 4189789,7000);
Insert Into Hritik_1902629 ( Rollno,Name,Contactno, DOB )
Values (1902620, 'John', 4159789,7500);
SELECT * FROM Hritik_1902629;

OUTPUT:

3. SELECT DISTINCT Statement : used to return only distinct (different) values.


Syntax:
SELECT DISTINCT column1, column2, ...
FROM table_name;

Example:
SELECT DISTINCT Name
From Hritik_1902629;

OUTPUT:

Hritik Sharma 1902629 21


DEPARTMENT OF INFORMATION TECHNOLOGY

Practical No.-5
Aim: SQL using- where; OR, AND and OR operators
Objectives: To Study and Execute SELECT Statement using:
1. WHERE Clause
2. Where with “AND, OR, NOT”
3. Where with Relational Operator (<,>,<=,>=,<>)
Theory:
1) WHERE Clause:

▪ The WHERE Clause is used to filter records.


▪ It is used to extract only whose records that fulfil a specified condition.
Syntax: Select column1, column2
From table_name
Where Condition;

Example: Select name,marks from hritik_1902629_5


Where City='Delhi';

Screenshot:

Hritik Sharma 1902629 22


DEPARTMENT OF INFORMATION TECHNOLOGY

Where”AND, OR and NOT” Operators:-The WHERE clause can be combined with


AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one condition:

AND:-The AND operator displays a record if all the conditions


separated by AND are TRUE.

Syntax:-
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

Example:
Select Roll_No,name,marks from hritik_1902629_5
Where Marks<=50 And City='Delhi';

Screenshot:

Hritik Sharma 1902629 23


DEPARTMENT OF INFORMATION TECHNOLOGY

OR:-The OR operator displays a record if any of the conditions


separated by OR is TRUE.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 ORcondition2 AND condition3 ...;

Example:-
Select Roll_No,name,marks from hritik_1902629_5
Where Marks<=50 OR City='Delhi';

Screenshot:

Hritik Sharma 1902629 24


DEPARTMENT OF INFORMATION TECHNOLOGY

NOT:-The NOT operator displays a record if the condition(s) is NOT


TRUE.

Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

Example:- Select Name, City from hritik_1902629_5


Where City<>'Delhi';

Screenshot:

Hritik Sharma 1902629 25


DEPARTMENT OF INFORMATION TECHNOLOGY

Where with Relational operators (<,>,<=,>=,<>):-It is basically used in binary


operators as well as comparison purpose.

Greater than(>):It checks whether the left operand is greater or not.

Syntax:-
SELECT[column_name| * value1>value2;

Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks>50;

Hritik Sharma 1902629 26


DEPARTMENT OF INFORMATION TECHNOLOGY

Screenshot:

less than(>):It checks whether the right operand is greater or not.

Syntax:-
SELECT[column_name| * value1<value2;

Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks<50;

Screenshot:

Hritik Sharma 1902629 27


DEPARTMENT OF INFORMATION TECHNOLOGY

Greater than and equal to(>=):It checks whether the left operand is greater than and equal
to or not.

Syntax:-
SELECT[column_name| * value1>=value2;

Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks>=50;

Screenshot:

Hritik Sharma 1902629 28


DEPARTMENT OF INFORMATION TECHNOLOGY

lessthan(<=):It checks whether the right operand is greater and equal to or not.
Syntax:-
SELECT[column_name| * value1<=value2;
Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks<=50;

Screenshot:

Hritik Sharma 1902629 29


DEPARTMENT OF INFORMATION TECHNOLOGY

Equal to(=):-It checks whether the both values are equal or not.
Syntax:-
SELECT[column_name| * value1=value2;
Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks=50;

Screenshot:

Hritik Sharma 1902629 30


DEPARTMENT OF INFORMATION TECHNOLOGY

Not Equal to(<>):-It prints the values which are not equal.
Syntax:-
SELECT[column_name| * value1<>value2;
Example:-
Select Roll_No,Name from hritik_1902629_5
Where Marks<>50;

Screenshot:

Hritik Sharma 1902629 31


DEPARTMENT OF INFORMATION TECHNOLOGY

Hritik Sharma 1902629 32

You might also like