0% found this document useful (0 votes)
23 views32 pages

Shubham Record Work

Uploaded by

sydhasan003
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)
23 views32 pages

Shubham Record Work

Uploaded by

sydhasan003
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/ 32

Syed Hasan Ahmed RA2111008020174

EX.NO:1
SQL DATA DEFINITION LANGUAGE COMMANDS
DATE:

AIM:
To write a program for SQL Data Definition Language Commands on sample exercise.

DATADEFINITIONLANGUAGE[DDL]:

● Used to define database structure or pattern.


● Used to create schema, tables, indexes, constraints
● Create skeleton of the database.
● Used to store information of metadata like no. of tables and schemas, their
names, indexes, columns in each table, constraints

Some of the commands are:


1. Create
2. Alter
3. Drop
4. Truncate
5. Rename

1. CREATECOMMAND:
 Used to create objects in database.

SYNTAX:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE
table_name( column1
datatype,
column2datatype,
column3datatype,
.....
Column Ndatatype,
);

Input query:
create table stdinfo(
fnamevarchar(50),
lnamevarchar(50),
course varchar(30),
email varchar(15)
)

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
OUTPUT:
CREATE COMMAND:

desc stdinfo;

2. ALTER Command:
 The SQL ALTER TABLE command is used to add, delete or modify
columns in an existing table. We would also use ALTER TABLE command
to add and drop various constraints on an existing table.

SYNTAX:
2A)The basic syntax of ALTER TABLE to add a new column in an existing table
is as follows: ALTER TABLE table_name ADD column_name datatype;

2B)The basic syntax of ALTERTABLE to DROPCOLUMN in an existing able is as


follows:
ALTER TABLE table_name DROP COLUMN column_name;

2C)The basic syntax of ALTER TABLE to change the DATA TYPE of a column
in a table is as follows:
ALTER TABLE table_name MODIFYCOLUMN column_namedatatype;

Input query:

ALTER COMMAND TO ADD A COLUMN:


alter table stdinfo
add address varchar(10);

desc stdinfo;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

Drop column:
alter table stdinfo drop column fname;

desc stdinfo

Modify COLUMN:
alter table stdinfo
modify lnamevarchar(100);

desc stdinfo;

CREATE NEW TABLE:


Create table delete2(
name varchar(10),
lnamevarchar(10)
)
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174

Desc delete2

3 DROPCOMMAND:
 The DROP TABLE statement is used to drop an existing table in a database.

SYNTAX:
DROP TABLE table_name

INPUT QUERY

drop table delete2;

4 TRUNCATE COMMAND:

 The TRUNCATE TABLE statement is used to delete the data inside a table,
but not the table itself.

SYNTAX:
TRUNCATE TABLE table_name;

INPUT QUERY:

Truncate table stdinfo;


Desc stdinfo;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
5 RENAME COMMAND:
 Used to rename the table.

SQL offers two ways to rename tables.


The first one uses the ALTER TABLE
SYNTAX:
ALTER TABLE old_table_name RENAME new_table_name;

INPUT QUERY:
Alter table stdinfo rename to stdinfo2;

The second way is to use RENAME TABLE:

RENAME TABLE old_table_name TO new_table_name;

INPUT QUERY:
rename stdinfo to stdinfo2;

desc stdinfo;
BEFORE ALTER RENAME

AFTER ALTER RENAME

RESULT:Thus, the query for SQL Data Definition Language Commands are written, executed
and the outputs are verified successfully.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
EX.NO:2
SQL DATA MANIPULATION LANGUAGE COMMANDS
DATE:

AIM:
TowriteaprogramforSQLDataManipulationLanguageCommandsonsampleexercise.

DATAMANIPULATIONLANGUAGE:
 DML commands are the most frequently used SQL commands and is
used to query and manipulate the existing database objects.

Some of the commands are:


1. Insert
2. Select
3. Update
4. Delete

1. INSERT COMMAND:
 This is used to add one or more rows to a table. The values are
separated by commas and the data types char and date are enclosed in
apostrophes. The values must be entered in the same order as they are
defined.

SYNTAX:
INSERTING A SINGLE ROW INTO A TABLE:
insertinto<tablename>values(fieldvalue-1,fieldvalue-2,…,fieldvalue-n);

INSERTING MORE THAN ONE RECORD USING A SINGLE INSERT


COMMAND:
INSERTINTO<tablename>VALUES(&fieldname-1,&fieldname-2,…&fieldname-n);

SKIPPING THE FIELDS WHILE INSERTING:


INSERT INTO <tablename(column names to which datas to be
inserted)>VALUES (list of values); Other way is to give null while passing
the values.
INSERTINTO<tablename>(SELECT(att_list)FROM<existingtablename>);

CREATE TABLE
create table dta(
snovarchar(10),
sender char(10) not null,
age char(10) not null,
receiver char(10) not null
)
desc dta;
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174

INPUT QUERY:
INSERT COMMAND:
insert into dta values('1','raj','20','mohan');
insert into dta values('2','manju','22','ram');
insert into dta values('3','manoj','25','shayam');

select * from dta;

2. SELECT COMMAND:
 This command is used to get data out of the database. It helps users of the database to
access from an operating system, the significant data they need. It sends a track result
set from one table or more.

SYNTAX:
SELECT *FROM <table_name>;

INPUT QUERY:
select age from dta;

OUTPUT

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

3. UPDATE:
 It is used to alter the column values in a table. A single column may be
updatedor more than one column could be updated.

SYNTAX:
UPDATE<table name>set(fieldname-1 = value, fieldname-2 = value,…,fieldname-n
= value) [WHERE <condition/expression>];

INPUT QUERY:
update BGMIX set Age = '21' where Age= 20;

OUTPUT:
UPDATE COMMAND:

4. DELETE:
 After inserting row in a tables, we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.

SYNTAX:
DELETEFROM<tablename>[where<condition/expression>];

INPUT QUERY:
delete from dta where age='22';
OUTPUT:

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

RESULT:Thus, the query for SQL Data Manipulation Language Commands on sample exercise
was executed and verified successfully.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
EX.NO:3
SQL DATA CONTROL LANGUAGE COMMANDS AND TRANSACTION
DATE: CONTROL COMMANDS

AIM:
To write a program for SQL Data Control Language Commands and Transaction
Control Commands on sample exercise.

DATA CONTROL LANGUAGE (DCL):

● DCL is used to retrieve stored or saved data.


● DCL execution is transactional. It also has rollback parameters.

Some commands are:


● GRANT
● REVOKE

GRANT:
It is used to give user access privileges to a database.

SYNTAX:
GRANT SELECT ,UPDATE ON TABLE NAME TO
SOME_USER,ANOTHER_USER;

REVOKE:
It is used to take back permissions from the user.

SYNTAX:
REVOKE SELECT,UPDATE ON TABLE NAME FROM USER1,USER2;

1. CREATE A NEW USER:


• Open MySQL Workbench and connect to your MySQL server using the root
account. • In the SQL Editor, execute the following query to create a new user.
Replace new_user and password with your desired username and password.
CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'password';

CODE:
CREATE USER 'shubham'@'localhost' IDENTIFIED BY 'root@123';

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

2. SHOW THE NEW USER AND THEIR PRIVILEGES:


• To view the newly created user, execute the following query:
SELECT user, host FROM mysql.user WHERE user = 'new_user';
• To display the privileges granted to the user:
SHOW GRANTS FOR 'new_user'@'localhost';

CODE:
SELECT user, host FROM mysql.user WHERE user = 'shubham';

SHOW GRANTS FOR 'shubham'@'localhost';

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

3.GRANT PRIVILEGES TO THE NEW USER:


• Grant the necessary privileges to the new user. Replace database_name.* with the
specific database and privileges you want to grant.
GRANT SELECT, INSERT, UPDATE ON database_name.* TO
'new_user'@'localhost';
CODE:
GRANT SELECT, INSERT, UPDATE ON database_name.* TO 'shubham'@'localhost';

4.SHOW THE USER WITH GRANTED PRIVILEGES:


• Display the privileges granted to the user:
SHOW GRANTS FOR 'new_user'@'localhost';
CODE:
SHOW GRANTS FOR 'shubham'@'localhost';
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174

5.REVOKE PRIVILEGES FROM THE USER:


• If you want to revoke specific privileges, use the following query. Replace
database_name.* with the appropriate database and privileges.
REVOKE SELECT, INSERT, UPDATE ON database_name* FROM
'new_user'@'localhost';
CODE:
REVOKE SELECT, INSERT, UPDATE ON database_name.* FROM 'shubham'@'localhost';

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
6.SHOW THE USER AFTER REVOKING PRIVILEGES:
• Display the updated privileges for the user:
SHOW GRANTS FOR 'new_user'@'localhost';
CODE:
SHOW GRANTS FOR 'shubham'@'localhost';

TRANSACTION CONTROL LANGUAGE (TCL):


● TCL is used to run changes made by DML statement.
● TCL can be grouped into a logical transaction.

Some of the commands are:


● COMMIT
● ROLLBACK
● SAVEPOINT
● SETTRANSACTION

COMMIT:
Commit command is used to save all the transactions to the database.

SYNTAX:
commit;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
ROLLBACK:
Roll back command is to rollback a transaction in case of any error occurs.

SYNTAX:
rollback;

SAVEPOINT:
Save point command is to Seta save point with in a transaction . If transaction
happens in big data, then for checking and rollup can't do it with all the data, to
rollback the small part of the data we use save point query.

SYNTAX:
SAVE POINT savepoint _ name

SETTRANSACTION:
Set command is to Specify the characteristics of the transaction.

SYNTAX:
SET TRANSACTION Access NAME transaction_name

RESULT: Thus the query for SQL Data Control Language Commands and Transaction
Control Commands on sample exercise was executed and verified successfully.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
EX.NO:
INBUILT FUNCTIONS IN SQL
4
DATE:

AIM:
To implement query for inbuilt functions using SQL.

SQL Functions:

Functions in SQL Server are the database objects that contain a set of SQL statements to
perform a specific task. The main purpose of functions is to replicate the common task
easily. We can build functions one time and can use them in multiple locations based on our
needs. SQL Server does not allow to use of the functions for inserting, deleting, or updating
records in the database tables.
SQL functions are categorized into the following two categories:
1. Aggregate Functions
2. Scalar Functions
.
AGGREGATE SQL FUNCTIONS
The Aggregate Functions in SQL perform calculations on a group of values and then return a
single value. Following are a few of the most commonly used Aggregate Functions:

Function Description
SUM() Used to return the sum of a group of values.
COUNT() Returns the number of rows either based on a condition, or without a
condition.
AVG() Used to calculate the average value of a numeric column.
MIN() This function returns the minimum value of a column.
MAX() Return maximum value of a column.
FIRST() Used to return the first value of the column.
LAST() This function returns the last value of the column.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
SCALARSQLFUNCTIONS
The Scalar Functions in SQL are used to return a single value from the given
input value.Followingarea few of the most commonly used Aggregate Functions:

Function Description
LCASE() Used to convert string column values to lowercase
UCASE() This function is used to convert a string column values to Upper case.
LEN() Returns the length of the text values in the column.
MID() Extracts substrings in SQL from column values having String data type.
ROUND() Rounds off an numeric value to then nearest integer.
NOW() This function is used to return the current system date and time.
FORMAT( Used to for math a field must be displayed.
)

CHARACTER/STRING FUNCTION:

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

EXP.NO:5
CONSTRUCTING AN ER MODEL FOR THE APPLICATION
DATE:

AIM:

To construct an Entity Relation Diagram for Travel Management System using


RDBMS.

ENTITYRELATIONSHIPDIAGRAM:
An ER diagram shows the relationship among entity sets. An entity set is
a group of similar entities and these entities can have attributes.
Inter ms of DBMS,an entity is a table or attribute
ofatableindatabase,sobyshowingrelationshipamongtablesandtheirattributes,ERd
iagramshowsthecompletelogicalstructureofadatabase.

COMPONENTSOFERDIAGRAM:
An ER diagram has three main components:-
1. Entity:
Anentityisanobjectorcomponentofdata.AnentityisrepresentedasarectangleinanERdiagr
am.
Ithas2types:
- Weak Entity (represented by a double rectangle)
- Strong Entity (represented by a single rectangle).

2. Attributes:
An attribute describes the property of an entity.An attribute is represented as ER
diagram. There are four types of attributes:
- Key attribute
- Composite attribute
- Multi value attribute
- Derived attribute

3. Relationships:
A relationship is represented by a diamond shape in ER diagram ,its how the
relationship among entities.There are four types of relationships:
-One to One
-One to Many
-
-Many to One
-Many to Many

KEYCONSTRAINTS:

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
PRIMARYKEY:
Primary key uniquely identifies each record in a table. A Primary Key must contain
unique value and it must not contain null value. Usually the primary Key isused to
index the data inside the table.

FOREIGNKEY:
Foreign Key is used to relate two tables. The relationship between the two tables
matches the Primary Key in one of the tables with a Foreign Key in the second table.
This is also called are ferencing key.

ER DIAGRAM:

RESULT :Thus the Entity Relationship Diagram for student database has been constructed
and verified successfully.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

EXP :6
Nested Queries
DATE:

AIM: To implement Nested Queries commands on sample exercise using SQL.

Nested Query (Sub Query):


Sub Query can have more than one level of nesting in one single query. A SQL nested query is a
SELECT query that is nested inside a SELECT, UPDATE, INSERT, or DELETE SQL query.

1. Select Command Is Used To Select Records From The Table.


2. Where Command Is Used To Identify Particular Elements.
3. Having Command Is Used To Identify Particular Elements.
4. Min (Sal) Command Is Used To Find Minimum Salary.
SYNTAX FOR CREATING A TABLE:
SQL> Create (Column Name.1 (Size), Column Name.1 (Size) .............);
INPUT QUERY:

create table course(


sub_namevarchar(10),
course_idvarchar(10),
year_novarchar(4)
)

CREATE TABLE teacher(


course_idvarchar(10),
teacher_namevarchar(10)
)
OUTPUT:

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

SYNTAX FOR INSERT RECORDS IN TO A TABLE:


insert into course values('biology','456','2008');
insert into course values('maths','457','2009');
insert into course values('chemistry','458','2007');
insert into course values('english','459','2008');
insert into course values('german','460','2005');
select * from course;

insert into teacher values('457','raj');


insert into teacher values('458','aman');
insert into teacher values('459','kishore');
select * from teacher;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

SYNTAX FOR SELECT RECORDS FROM THE TABLE:


SQL> Select * From <Table Name>;
INPUT QUERY:
select sub_name, course.course_id, year_no
from course
where course.course_id in (
select course_id
from teacher
)

select count (sub_name)


from course
where year_no=2008

RESULT: Thus the Nested Query commands on a sample exercise using SQL was executed and
verified successfully.
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174

EXP:7 JOINS

DATE:

AIM:To implement join query commands using SQL.

JOIN QUERIES:
● SQL joins are used to query data from two or more tables, based on a
relationship
between certain columns in these tables.

Different Types of SQL JOINs


1. Inner Join
2. Self Join
3. Outer Join
a) Left Outer Join
b) Right Outer Join
c) Full Outer Join
4. Cross Join
INNER JOIN:
Returns records that have matching values in both tables

Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

CREATE:
create table first(
course_idvarchar(10),
title varchar(10),
dept_namevarchar(10),
credits varchar(10)
)

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

create table second(


course_idvarchar(10),
faculty_namevarchar(10)
)

INPUT QUERY:
SELECT first.course_id
FROM first
INNER JOIN second
ON first.course_id = second.course_id;

SELF JOIN:
A self join is a regular join, but the table is joined with itself.

Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;

INPUT QUERY:
SELECT
f1.course_id AS course_id1,
f1.title AS title1,
f2.course_id AS course_id2,

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
f2.title AS title2
FROM
first f1
INNER JOIN
first f2 ON f1.dept_name = f2.dept_name
WHERE
f1.course_id <> f2.course_id;

LEFT OUTER JOIN:


Returns all records from the left table, and the matched records from the right table.

Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

INPUT QUERY:
SELECT first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
FROM first
LEFT OUTER JOIN second
ON first.course_id = second.course_id;

RIGHT OUTER JOIN:


Returns all records from the right table, and the matched records from the left table

Syntax:
SELECT column_name(s)
FROM table1
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

INPUT QUERY:
select first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
from first
right join second
on first.course_id=second.course_id;

FULL OUTER JOIN:


Returns all records when there is a match in either left or right table

Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

INPUT QUERY:
SELECT first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
FROM first
FULL OUTER JOIN second
ON first.course_id = second.course_id;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174
CROSS JOIN:
Combines all of the possibilities of two or more tables and returns a result that includes every
row from all contributing tables. It's also known as CARTESIAN JOIN.

Syntax:
SELECT column_lists
FROM table1
CROSS JOIN table2;

INPUT QUERY:
SELECT *
FROM first
CROSS JOIN second;

RESULT:Thus the implementation of join queries using SQL was executed and verified
successfully.

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

EXP:8
SET OPERATORS & VIEWS

DATE:

AIM: To implement Set Operators & Views using SQL

SET Operators:

SET operators are special type of operators which are used to combine the result of
two queries.

Operators covered under SET operators are:


- UNION
- UNION ALL
- INTERSECT
- MINUS(EXCEPT)

create table std(


name varchar(10),
roll_no varchar(10),
city varchar(10)
)

create table extra(


name varchar(10),
marks varchar(10),
class varchar(10)
)

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

insert into std values('a','11','pune');


insert into std values('b','12','jaipur');
insert into std values('c','13','kolkata');
insert into std values('d','14','chennai');
insert into std values('e','15','delhi');

insert into extra values('d','10','9');


insert into extra values('e','35','8');

select * from std;


select * from extra;

Union:
UNION will be used to combine the result of two select statements. Duplicate rows
will be eliminated from the results obtained after performing the UNION operation.
Syntax:
SELECT *FROM (table_name1) UNION SELECT *FROM (table_name2);
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
UNION:-
select *
from std
union select * from extra;

Union All:
This operator combines all the records from both the queries. Duplicate rows will be
not be eliminated from the results obtained after performing the UNION ALL operation.
Syntax:
SELECT *FROM (table_name1) UNION ALL SELECT *FROM (table_name2);

UNION ALL:-

select *
from std
union ALL select * from extra;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J


Syed Hasan Ahmed RA2111008020174

Intersect:
It is used to combine two SELECT statements, but it only returns the records which
are common from both SELECT statements.
Synatx:
SELECT *FROM (table_name1) INTERSECT SELECT *FROM (table_name2);
INTERSECT:-
SELECT name
FROM std
INTERSECT
SELECT name
FROM extra;

Minus(Except):
It displays the rows which are present in the first query but absent in the second query
with no
duplicates.
Synatx:
SELECT *FROM(table_name1) MINUS SELECT *FROM (table_name2);
MINUS:-
select *
from std
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
MINUS select * from extra;

VIEWS:
1. CREATE VIEW command is used to define a view.
Syntax:
CREATE VIEW (view_name) AS SELECT (column1, column2.....)
FROM (table_name) WHERE (condition);
VIEW:-
CREATE VIEW VIEW1 AS SELECT NAME,CITY
FROM STD ;

SELECT * FROM VIEW1;

DATABASE MANAGEMENT SYSTEM LAB-18CSC303J

You might also like