0% found this document useful (0 votes)
7 views

DBM12 (1)

The document outlines the use of Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL) in SQL, including commands for creating, altering, and managing database tables and data. It provides syntax and examples for various SQL operations such as CREATE, INSERT, UPDATE, DELETE, and querying with conditions using ANY, ALL, IN, EXISTS, and UNION. The document concludes with successful execution of queries to retrieve specific student information based on ranks and scores.

Uploaded by

abiaravind8383
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)
7 views

DBM12 (1)

The document outlines the use of Data Definition Language (DDL), Data Manipulation Language (DML), Transaction Control Language (TCL), and Data Control Language (DCL) in SQL, including commands for creating, altering, and managing database tables and data. It provides syntax and examples for various SQL operations such as CREATE, INSERT, UPDATE, DELETE, and querying with conditions using ANY, ALL, IN, EXISTS, and UNION. The document concludes with successful execution of queries to retrieve specific student information based on ranks and scores.

Uploaded by

abiaravind8383
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/ 10

DATE: EXP NO: Page.

No:
AIM: Creation, altering and droping of tables and inserting rows into a table(useconstraints
while creating tables) examples using SELECT command.
DATA DEFINATION LANGUAGE:
DDL commands are used to define and manage all structures in a database. These commands deal
with the schema and structure of the database.
CREATE: Used to create database objects like tables, indexes, and views.
Syntax:
CREATE TABLE table_name (
column1 datatype constraint1,
column2 datatype constraint2,
...
columnN datatype constraintN
);
Example:
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE
);
Output:
Table created.
ALTER: Used to modify existing database objects.
Syntax:
ALTER TABLE table_name
ADD column_name datatype;
-- To modify a column
ALTER TABLE table_name
MODIFY column_name new_datatype;
Example:
ALTER TABLE Employees
ADD Email VARCHAR(100);
Output: Table altered.
TRUNCATE: The TRUNCATE TABLE command removes all rows from a table
SYNTAX:
TRUNCATE TABLE table_name;

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: Page.No:
Example:
TRUNCATE TABLE Employees;
Output:Table Truncated.
RENAME: The RENAME TABLE command changes the name of an existing table.
Syntax:
RENAME TABLE old_table_name TO new_table_name;
Example:
RENAME TABLE employees TO staff;
Output:
Table Renamed
Data Manipulation Language:
DML commands are used for managing data within schema objects. These commands allow you to
insert, update, delete, and retrieve data.
INSERT: Used to add new records to a table.
Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
INSERT INTO Employees VALUES (1, 'John', 'Doe', ’[email protected]’);
INSERT INTO Employees VALUES (2, 'kavya', 'sri',’[email protected]’);
INSERT INTO Employees VALUES (3, 'veena', 'jessy',’[email protected]’);
INSERT INTO Employees VALUES (4, 'greeshma', 'siri',’[email protected]’);
INSERT INTO Employees VALUES (5, 'varsha','tej',’[email protected]’);
Output:
5 rows created.
UPDATE: Used to modify existing records in a table.
Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
UPDATE Employees
SET LastName = 'Johnson'
WHERE EmployeeID = 1;
Output:1 row updated.
SELECT: Used to query and retrieve data from one or more tables.

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:
Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
Select*from employees;
Output:
EMPLOYEEID FIRSTNAME LASTNAME EMAIL

1 john doe [email protected]


2 kavya sri [email protected]
3 veena jessy [email protected]
4 greeshma siri [email protected]
5 varsha tej [email protected]

DELETE: Used to remove records from a table.


Syntax:
DELETE FROM table_name
WHERE condition;
Example:
DELETE FROM Employees
WHERE EmployeeID = 2;
Output:
1 row deleted.
TCL (Transaction Control Language):
TCL commands are used to manage transactions in a database. These commands ensure that a
series of operations are completed successfully and maintain the integrity of the database.
COMMIT: Used to save all changes made during the current transaction.
Syntax: COMMIT;
Example: commit;
Output:
Commit complete.
ROLLBACK: Used to undo changes made during the current transaction.
Syntax:ROLLBACK;
Example: rollback;
Output: Rollback complete.

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:
SAVEPOINT: Used to set a point within a transaction to which you can later roll back.
Syntax:
SAVEPOINT savepoint_name;
Example:
INSERT INTO Employees VALUES (6, 'rish','prajkat',’[email protected]’);
1 row created.
SQL> savepoint sp1;
Output:
Savepoint created.
SQL> INSERT INTO Employees VALUES (7, 'mostly','sane',’[email protected]’);
1 row created.
SQL> rollback to sp1;
Output:
Rollback complete.
Data Control Language:
GRANT: Used to give users access privileges to database objects.
Syntax:
GRANT privilege_type ON object TO user;
Example: GRANT SELECT ON Employees TO system;
Output: Grant succeeded.
REVOKE: Used to remove access privileges from users.
Syntax:
REVOKE privilege_type ON object FROM user;
Example: REVOKE SELECT ON Employees FROM system;
Output: Revoke succeeded.
RESULT:
Hence for Creation, altering and droping of tables and inserting rows intoatable
(useconstraints while creating tables)was successfully executed.

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

AIM:Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints. Example:- Select the roll number and name of the student who
secured fourth rank in the class.
DESCRIPTION:
This query retrieves the roll number and name of the student who secured the 4th rank in the
class by joining the teacher and results tables.

CREATE TABLE Students (

RollNumber INT PRIMARY KEY,

Name VARCHAR(50) NOT NULL,

Class VARCHAR(20),

Rank INT,

DepartmentID INT

);

CREATE TABLE Departments (

DepartmentID INT PRIMARY KEY,

DepartmentName VARCHAR(50)

);

CREATE TABLE Scores (

RollNumber INT,

Subject VARCHAR(50),

Score INT,

FOREIGN KEY (RollNumber) REFERENCES Students(RollNumber)

);

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

select * from departmentss;

DEPARTMENTID DEPARTMENTNAME

1 Computer Science
2 Os
3 Mathematics
4 Physics
5 English
6 Es

ROLLNUMBER NAME CLASS RANK DEPARTMENTID

101 veena 12A 1 1

102 Bob 12A 3 2

103 teja 12B 4 1

104 varsha 12B 2 3

105 avi 12A 5 2

106 avi 12A 6 4

SELECT* from scores;

ROLLNUMBER SUBJECT SCORE

101 Math 90

101 Science 85

102 Math 88

103 Science 92

104 Physics 95

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

105 Math 80

106 Computer Science 70

FOURTH RANK:

SELECT RollNumber, Name FROM Students WHERE Rank = 4;

ROLLNUMBER NAME

103 teja

USING ANY CLAUSE:

Syntax:SELECT column_name FROM table_name WHERE column_name > ANY (SELECT


column_name FROM table_name WHERE condition);

QUERY:

SELECT s.RollNumber, s.Name FROM Students s WHERE s.RollNumber = ANY ( SELECT


sc.RollNumber FROM Scores sc WHERE sc.Score > ANY(SELECT Score FROM Scores WHERE
Subject = 'Math'));

OUTPUT:

ROLLNUMBER NAME

101 veena

102 Bob

103 teja

104 varsha

USING ALL CLAUSE:

SYNTAX:SELECT column_name FROM table_name WHERE column_name > ANY (SELECT


column_name FROM table_name WHERE condition);

QUERY: SELECT s.RollNumber, s.Name FROM Students s WHERE s.RollNumber = ANY ( SELECT
sc.RollNumber FROM Scores sc WHERE sc.Score > (SELECT ALL(Score) FROM Scores WHERE
Subject = 'Math'));

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

OUTPUT:

ROLLNUMBER NAME

103 teja

104 varsha

IN:

SYNTAX: SELECT column_name FROM table_name WHERE column_name IN (value1, value2,


...);

QUERY:

SELECT RollNumber, Name FROM Students WHERE DepartmentID IN (1, 2);

OUTPUT:

ROLLNUMBER NAME

101 veena

102 Bob

103 teja

105 avi

EXISTS:

SYNTAX: SELECT column_name FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE
condition);

QUERY:

SELECT RollNumber, Name FROM Students s WHERE EXISTS ( SELECT 1 FROM Scores sc
WHERE sc.RollNumber = s.RollNumbe);

OUTPUT:

ROLLNUMBER NAME

101 veena

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

102 Bob

103 teja

104 varsha

105 avi

106 avi

NOT EXISTS:

SYNTAX: SELECT column_name FROM table1 WHERE NOT EXISTS (SELECT * FROM table2
WHERE condition);

QUERY:

SELECT RollNumber, Name FROM Students s WHERE NOT EXISTS ( SELECT 1FROM Scores sc
WHERE sc.RollNumber = s.RollNumber);

OUTPUT:

No rows selected.

UNION:

SYNTAX: SELECT column_name FROM table1 WHERE condition UNION SELECT column_name
FROM table2 WHERE condition;

QUERY:

SELECT RollNumber, Name FROM Students WHERE DepartmentID = 1 UNION SELECT


s.RollNumber, s.Name FROM Students s JOIN Scores sc ON s.RollNumber = sc.RollNumber
WHERE sc.Score > 90;

OUTPUT:

ROLLNUMBER NAME

101 veena

103 teja

104 varsha

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT


DATE: EXP NO: DATE:

INTERSECT:

SYNTAX: SELECT column_name FROM table1 WHERE condition INTERSECT SELECT


column_name FROM table2 WHERE condition;

QUERY:

SELECT RollNumber, Name FROM Students WHERE DepartmentID = 1 INTERSECT SELECT


s.RollNumber, s.Name FROM Students s JOIN Scores sc ON s.RollNumber = sc.RollNumber;

OUTPUT:

ROLLNUMBER NAME

101 veena

103 teja

RESULT:

Hence Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, Constraints. Example:- Select the roll number and name of the student who
secured fourth rank in the class is executed successfully.

DATABASE MANAGEMENT SYSTEM LABORATORY CSE DEPARTMENT

You might also like