sonu dbms
sonu dbms
WEEK-1
Date: 11-12-2024
Description :
DDL (Data Definition Language) is a set of SQL commands used to define and modify the
structure
of a database. It is used to create, alter, and drop database objects such as tables, indexes,
views,
and relationships.
DML is used to manipulate the data in the database, such as inserting, updating, or retrieving
records. DML commands focus on managing and modifying the data within tables.
DML Commands
1. INSERT: Adds new records into a table by specifying the values for each column.
1
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
3· DELETE: Removes specific records from a table without affecting its structure.
4· SELECT: Retrieves data from one or more tables based on specified criteria.
COMMAND CREATE
OUTPUT
OBSERVATIONS ● This command tells the database management system to switch to the
specified database, allowing you to access its tables, views, and other
objects.
COMMAND CREATE
OUTPUT
2
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
OUTPUT
OBSERVATIONS ● Alter with modify used to change the datatype and change the size if
mentioned.
OUTPUT
OBSERVATIONS ● The ALTER TABLE command is used to modify the structure and data
of an existing table.
3
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
OUTPUT
OBSERVATIONS ● The ALTER TABLE command is used to modify the structure and data
of an existing table.
COMMAND RENAME
OUTPUT
COMMAND DROP
Desc student1;
OUTPUT
OBSERVATIONS ● When you use the DROP command, the specified table is
permanently deleted from the database. This action:.
4
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
COMMAND TRUNCATE
OUTPUT
COMMAND INSERT
OUTPUT
OBSERVATIONS ● Here we can insert all the values at a time and also we can insert some
specific values based on our requirement in the table by using insert
command.
● When we insert values in some specific cells then remaining will be
5
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
null.
COMMAND UPDATE
SYNTAX update “table_name” set column1 = value1, column2 = value2, .. where condition;
OUTPUT
OBSERVATI ● The UPDATE command allows you to modify existing data in a table. If no condition is
ONS specified or if there's an error in the condition:
● The entire column will be updated with the new value.
● All rows in the table will be affected.
●
6
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
WEEK-2
Date: 18-12-2024
Description :
On update cascade : When a value in the parent table is updated, the corresponding values in
the
child table are automatically updated to match the new value.
On delete cascade : When a row in the parent table is deleted, the corresponding rows in the
child
table are automatically deleted as well.
Query CREATE TABLE employee(eno int primary key,ename char(40) not null,email varchar(20)
unique,dept char(10),doj date,salary int check(salary>5000),city char(15) default 'Chicago');
Output
7
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
3
Insert the following data in the table based on constraints
8
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
4
Insert (105, 'David Clark', '[email protected]', 'IT', '2020-11-20',
5000.00, 'Seattle')
Output
9
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
5
● Attempt to insert (107,’’,'[email protected]', 'HR',
'2021-03-15', 62000.00, 'Austin'), and observe the result
Output
6
Attempt to insert (108, 'Frank Miller', '[email protected]', 'Finance',
'2017-08-05', 2500.00, 'Boston'), and observe the results
Output
10
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
7
Attempt to insert (109, 'Grace Lee', '[email protected]', 'Sales',
'2023-02-01', 51000.00), and display the table.
Output
11
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
8
Without department information Insert remaining values into table (110,
'Helen Taylor', '[email protected]', '2016-12-10', 67000.00, 'San
Diego') and display the table.
Output
9
Now insert the Dept ‘marketing’ using update command for emp no 110 and
display the table
12
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
10
Delete some records and display the table
Query
delete from employee where eno<104;
Output
13
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
11
Update the city of Emp no 105 with current city as New York.
Output
12
Display the details of Employee.
Query
select * from employee;
Output
14
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
13
Delete the email_id of employee John Doe.
Output
EXP 2
1
Set DeptID as a primary key.
15
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Add a new column “Manager” with a unique constraint to the department table
Output
WORKSIN
Eno Dept ID
101 1
102 2
103 3
102 4
104 5
1
Create table WORKSIN with the following fields
Eno foreign key references Employee, DeptID foreign key references Department
table
Query create table worksin(eno int, deptid int, foreign key (eno) references employee(eno),
foreign key (deptid) references department(deptid));
16
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
1
Creating a table dept with deptid set to PRIMARY KEY and deptname set to
NOTNULL and inserting some values in to the relational table.
Output
17
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
2 Creating table emp having eno as primary key, deptno as foreign key referred fr
dept
Output
ON DELETE CASCADE:
1 Delete from the dept table where deptid=1 and check in the emp table
the corresponding row deleted in the emp table.
Output
Dept table -
ON
Emp table -
UPDATE CASCADE:
18
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
1 Update the deptid 3 from dept table to 4 and check whether its been
changed in emp table or not.
Output
Dept table -
Emp table -
OBSERVATION:
1. Table Creation and Constraints:
- Successful creation of Employee table with constraints (PRIMARY KEY, NOT NULL,
UNIQUE, DEFAULT, CHECK) ensures data integrity.
- Adding a new column (phnumber) and renaming an existing column (Email to Email_id)
demonstrates table modification capabilities.
2. Data Insertion and Constraint Enforcement:
- Insert operations demonstrate enforcement of PRIMARY KEY, NOT NULL, and CHECK
constraints.
- Errors occur when violating these constraints, ensuring data consistency.
3. Data Update and Deletion:
- Successful updates and deletions demonstrate data manipulation capabilities.
- FOREIGN KEY constraints prevent deletion of referenced data, maintaining referential
integrity.
4. Table Relationships and Joining:
- Creation of Department table and establishment of relationships with Employee table
demonstrate entity-relationship modeling.
- Inner join operation retrieves related data from both tables, showcasing data integration.
19
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
20
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
AIM : Implement the following queries to understand the usage of SELECT , WHERE,
BETWEEN ,AND, LIKE operators.
Description:
SELECT Operator
The SELECT operator is used to select data from a database table. It specifies which columns to
retrieve
and can also be used to perform calculations, aggregations, and transformations on the data.
Syntax
SELECT column1, column2, ...
FROM tablename;
Example
SELECT ename, salary
FROM employee;
WHERE Operator
The WHERE operator is used to filter data based on conditions. It specifies which rows to include in
the
result set.
Syntax
SELECT column1, column2, ...
FROM tablename
WHERE condition;
Example
SELECT ename, salary
FROM employee
WHERE salary > 50000;
21
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
BETWEEN Operator
The BETWEEN operator is used to select data within a specified range. It includes the start and end
values.
Syntax
SELECT column1, column2, ...
FROM tablename
WHERE columnname BETWEEN startvalue AND endvalue;
Example
SELECT ename, salary
FROM employee
WHERE salary BETWEEN 30000 AND 70000;
AND Operator
The AND operator is used to combine multiple conditions. It returns true only if all conditions are
met.
Syntax
SELECT column1, column2, ...
FROM tablename
WHERE condition1 AND condition2 AND ...;
Example
SELECT ename, salary
FROM employee
WHERE salary > 50000 AND deptno = 1;
LIKE Operator
The LIKE operator is used to search for patterns in strings. It uses wildcard characters such as % and
_.Syntax
SELECT column1, column2, ...
FROM tablename
WHERE columnname LIKE pattern;
22
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Example
SELECT ename, email
FROM employee
WHERE ename LIKE '%a%';
These operators are fundamental to SQL and are used to manipulate and retrieve data from
databases.
DEPARTMENT TABLE:
EMPLOYEE TABLE:
23
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
1 Display the employee names and mail id’s who are working in CSE depadepartment.
Query select ename, email from employee where deptno = (select deptid from department
where deptname='cse');
Output
2 Display the record of each employee who works in CSE and ECE.
Query select * from employee where deptno in (select deptid from department where deptname=
cse' and deptname='ece');
Output
Query select * from employee where deptno in (select deptid from department where deptname=
'cse' or deptname='ece');
24
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
4 Calculate a 10% bonus for all employees and display their updated salaries (Salary * 1.1)
Output
25
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
6 Subtract 1000 from the salary of employees who joined before '2020-01-01'.
26
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
Query update employee set salary=salary+5000 where deptno=(select deptid from department
Where deptname='hr');
Output
8 Retrieve details of employees whose salary is greater than 50,000 and who work in
having deptid 2 department.
Query select * from employee where salary>50000 AND deptno=(select deptid from departm
where deptname='it');
27
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
9 Find employees who joined after '2021-01-01' or whose salary is less than 40,000.
Output
Query select * from employee where deptno!=(select deptid from department where
location='newyork');
28
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
Output
12 Retrieve employees who work in the "Sales" department or have a salary between
30,000 and 70,000.
29
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
Output
30
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
Output
Output
31
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
18 Fetch employees whose first name contains 'h' anywhere in the name.
Output
32
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
20 Find employees whose first name starts with % and ends with _.
Output
21 Find employees whose name ends with 'e' and has at least 4 characters.
Output
33
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
22 Find employees whose name starts with 'A' and is followed by exactly 3
more characters.
Output
Output
34
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Query select * from employee where deptno!=(select deptid from department where
deptname='ece');
Output
Query select * from employee where joindate<='2021-01-01' and deptno=(select deptid from
Department where deptname='cse');
Output
35
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
27 27. Find employees who have NULL department IDs (employees with deleted
artments).
Output
Output
Query select * from employee where joindate > '2019-01-01' and joindate <'2021-12-31';
36
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
Output
OBSERVATIONS:
2.LIKE Operator
The LIKE operator is also known as the pattern matching operator. It uses special characters like % and _ to
match
patterns.
3.Escape Character
The default escape character is \. To identify special literals, use this escape character. For example, to find
the
literal %, use \%.
37
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
38
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
39
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
40
R23MSCSL006-DATABASE MANAGEMENT SYSTEM LAB 23331A05A3
OBSERVATION:
1. Since the nominee is a weak entity, it will participate in weak entity.
2. Weak entity does not have any primary key and it depends on other entity such that foreign
key is referred from other entity.
3. Cardinality is very important in er modelling between two entities.
4. It specifies the number of instances of one entity that can or must be associated with the
number of instances of another entity.
5. Account entity is divided into sub-entities using a process called specialization.
6. The attributes except primary key of entity are also the attributes of sub entities derived
from the process specialization.
41