DBM12 (1)
DBM12 (1)
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;
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.
Class VARCHAR(20),
Rank INT,
DepartmentID INT
);
DepartmentName VARCHAR(50)
);
RollNumber INT,
Subject VARCHAR(50),
Score INT,
);
DEPARTMENTID DEPARTMENTNAME
1 Computer Science
2 Os
3 Mathematics
4 Physics
5 English
6 Es
101 Math 90
101 Science 85
102 Math 88
103 Science 92
104 Physics 95
105 Math 80
FOURTH RANK:
ROLLNUMBER NAME
103 teja
QUERY:
OUTPUT:
ROLLNUMBER NAME
101 veena
102 Bob
103 teja
104 varsha
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'));
OUTPUT:
ROLLNUMBER NAME
103 teja
104 varsha
IN:
QUERY:
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
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:
OUTPUT:
ROLLNUMBER NAME
101 veena
103 teja
104 varsha
INTERSECT:
QUERY:
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.