DBMS Lab Manual-2021 REG FINAL
DBMS Lab Manual-2021 REG FINAL
Regulation-2021
Bonafide Certificate
Table of Contents
S.No Date Name of the Experiment Pg.No Sign
8 TRIGGERS
COMMANDS) AIM:
SQL commands are grouped into four major categories depending on their
functionality. They are as follows:
These SQL commands are used for creating, modifying, and dropping the
structure of database objects. The commands are CREATE, ALTER, DROP, RENAME,
and TRUNCATE.
These SQL commands are used for storing, retrieving, modifying, and
deleting data. These commands are SELECT, INSERT, UPDATE, and DELETE.
These SQL commands are used for managing changes affecting the data.
These commands are COMMIT, ROLLBACK, and SAVEPOINT.
These SQL commands are used for providing security to database objects.
These commands are GRANT and REVOKE.
● CREATE
● ALTER
● DROP
● TRUNCATE
● RENAME
PROCEDURE
STEP 1: Start
STEP 2: Create the table with its essential attributes.
STEP 3: Execute different Commands and extract information from the table.
STEP 4: Stop
SQL COMMANDS
ALTER TABLE tablename RENAME COLUMN old column name TO new column
name;
QUERY: 01
Q1. Write a query to create a table employee with empno, ename, designation, and
salary.
QUERY: 01
Table
created.
QUERY: 02
Q2. Write a query to display the column name and datatype of the table employee.
Q3. Write a query for create a new table from an existing table with all the fields.
QUERY: 03
SQL> CREATE TABLE EMP1 AS SELECT * FROM EMP;
Table created.
SQL> DESC EMP1
Name Null? Type
----------------------------------------- -------- ------------------
EMPNO NUMBER(4)
ENAME VARCHAR(10)
DESIGNATIN VARCHAR(10)
SALARY NUMBER(8,2)
QUERY: 04
Q4. Write a query to create a table from an existing table with selected fields.
Syntax
SQL> CREATE TABLE <TARGET TABLE NAME> SELECT EMPNO, ENAME
FROM <SOURCE TABLE NAME>;
QUERY: 04
SQL> CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM EMP;
Table created.
QUERY: 06
Q6. Write a Query to Alter the column EMPNO NUMBER (4) TO EMPNO NUMBER(6).
QUERY: 06
SQL>ALTER TABLE EMP MODIFY EMPNO NUMBER (6);
Table altered.
QUERY: 07
Q7. Write a Query to Alter the table employee with multiple columns (EMPNO,
ENAME.)
QUERY: 07
SQL>ALTER TABLE EMP MODIFY EMPNO INT (7),MODIFY ENAME
VARCHAR(12));
Table altered.
QUERY: 08
QUERY: 08
QUERY: 09
Table altered.
SQL> DESC EMP;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(7)
ENAME VARCHAR(12)
DESIGNATIN VARCHAR2(10)
SALARY NUMBER(8,2)
QUALIFICATION VARCHAR(6)
DOB DATE
DOJ DATE
QUERY: 10
Q10. Write the query to change the table name emp as employee
QUERY: 11
Q11. Write the query to change the column name empno to eno of the
table employee
SQL> ALTER TABLE employee RENAME COLUMN EMPNO TO ENO;
REMOVE /
DROP QUERY:
12
QUERY: 13
QUERY: 14
These SQL commands are used for storing, retrieving, modifying, and
deleting data. These commands are SELECT, INSERT, UPDATE, and DELETE.
● INSERT- 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.
PROCEDURE:
STEP 1: Start.
STEP 2: Create the table with its essential attributes.
STEP 3: Insert the record into table.
STEP 4: Update the existing records into the table.
STEP 5: Delete the records in to the table.
SQL COMMANDS
INSERT
QUERY: 01
Q1. Write a query to insert the records in to employee.
1 row
created.
SELECT
QUERY: 02
QUERY: 02
UPDATE
QUERY: 04
Q1. Write a query to update the records from employee.
Syntax for update Records from the table:
QUERY: 04
SQL> UPDATE EMP SET SALARY=16000 WHERE EMPNO=101;
1 row updated.
QUERY: 05
Q5. Write a query to update multiple records from employee.
QUERY: 05
DELETE
QUERY: 06
Q5. Write a query to delete records from employee.
Result:
Thus the DDL, DML commands are executed in MySQL and verified
successfully.
EX: NO: 3 CREATION OF TABLES WITH CONSTRAINTS
AIM:
To execute and verify the SQL commands for adding constraints.
MySQL Constraints
● SQL constraints are used to specify rules for the data in a table.
● Constraints are used to limit the type of data that can go into a table.
This ensures the accuracy and reliability of the data in the table. If
there is any violation between the constraint and the data action, the
action is aborted.
PROCEDURE:
STEP 1: Start.
STEP 2: Create the table with its essential attributes.
STEP 3: Add the constraint as a column level and table level
STEP 4: check all the constraints with specified conditions.
Create table1:
mysql> create table emp(empno int(3),empname varchar(20),age int(3),deptno
int(3),salary float(7,2),phno int(5));
PRIMARY KEY
Q1: create the department table with the primary key as a table
level constraint.
Q2: Alter the employee table with the primary key as a column
level constraint.
DEFAULT CONSTRAINT:
UNIQUE CONSTRAINT
Q6: create a unique constraint for the column phone number and check
the constraint
Q7:
Q10:
primary key
check
not null
unique
dropping constraints
Result:
Thus the MySQL statements for executing constraints are executed
successfully.
EX: NO: 3 WHERE CLAUSE CONDITIONS AND IMPLEMENT
AGGREGATE FUNCTIONS
AIM:
To execute and verify the SQL commands using where clause conditions and
implement aggregate functions.
PROCEDURE:
Create table 2:
mysql> select empname,empid from emp where salary between 20000 and 40000;
Q8: display the employee details having employee id greater than 103 and
department number 4.
Q10: display the employee details that are not in 102 and 104
Create table 2:
Q3:Display the company name which one having more than one employee.
mysql> insert into works values(105,'wipro','bangalore',30000);
Query OK, 1 row affected (0.03 sec)
mysql> select companyname,count(empid) as no_of_emp from works group by
companyname having count(empid)>1;
Q10: Display company name which one is having less sum of salary compared
to others
mysql> select companyname from works group by companyname having
sum(salary)<=all(select sum(salary)from works group by companyname);
+-------------+
| companyname |
+-------------+
| infosis |
+-------------+
order by clause:
Result:
Thus the where clause conditions using MySQL statements are verified and
executed successfully.
EX: NO: 4 SIMPLE JOIN OPERATIONS
AIM:
SUB-QUERY:
A sub-query is a SQL query nested inside a larger query. A Sub Query can also be called a
Nested/Inner Query.
1. SELECT CLAUSE
2. FROM CLAUSE
3. WHERE CLAUSE
CREATE TABLE 1:
mysql> CREATE TABLE EMP(EMPID INTEGER,EMPNAME VARCHAR(20),AGE
INTEGER,SALARY INTEGER);
+-------+---------+------+-------+
+-------+---------+------+-------+
| 1 | ASHI | 16 | 10000 |
| 2 | ANI | 18 | 20000 |
| 3 | BISMI | 17 | 15000 |
+-------+---------+------+-------+
CREATE TABLE 2:
+-------+------+-------+
+-------+------+-------+
| 3 | 17 | 15000 |
| 1 | 16 | 10000 |
| 4 | 16 | 20000 |
+-------+------+-------+
A sub-query in a WHERE clause can be used to qualify a column against a set of rows.
SYNTAX:
mysql> SELECT * FROM REPORT WHERE EMPID IN(SELECT EMPID FROM EMP
WHERE EMPNAME='BISMI');
+-------+------+-------+
+-------+------+-------+
| 3 | 17 | 15000 |
+-------+------+-------+
SYNTAX:
+---------+--------+
| EMPNAME | AGE |
+---------+--------+
| ASHI | 16.0000 |
| BISMI | 17.0000 |
+---------+--------+
+-------+---------+------+-------+
+-------+---------+------+-------+
| 1 | ASHI | 16 | 10000 |
+-------+---------+------+-------+
SYNTAX:
+-------+------------+
| EMPID | MAX(SALARY) |
+-------+------------+
| 2| 20000 |
| 3| 15000 |
+-------+------------+
JOINS:
The USING clause specifies which columns to test for equality when two tables are
joined.
SYNTAX:
+-------+---------+------+--------+------+-------+
+-------+---------+------+--------+------+-------+
ON clause can be used to join columns that have different names. Use the ON
clause to specify conditions or specify columns to join.
SYNTAX:
+---------+-------+------+-------+
+---------+-------+------+-------+
| BISMI | 3 | 17 | 15000 |
| ASHI | 1 | 16 | 10000 |
+---------+-------+------+-------+
Result:
Thus the where clause conditions using MySQL statements are verified and
executed successfully.
EX: NO: 5 NATURAL, EQUI AND OUTER JOIN OPERATIONS
AIM:
To Query the Database Table and explore natural, equi and outer
join operations.
CREATE TABLE 1:
CREATE TABLE 2:
+-------+-------------+------+-----+---------+------+
+-------+-------------+------+-----+---------+------+
+-------+-------------+------+-----+---------+------+
+-------+-------------+------+-----+---------+------+
+-------+-------------+------+-----+---------+------+
+-------+-------------+------+-----+---------+------+
+-------+----------+-----+
+-------+----------+-----+
| Ajay | Chennai | 11 |
| Vijay | Banglore | 12 |
| Sujay | Chennai | 13 |
| Jay | Madurai | 14 |
+------+-------+------+
+------+-------+------+
| 11 | IT | 20000 |
| 12 | CSE | 20020 |
| 13 | IT | 20050 |
| 14 | CSE | 20000 |
+------+-------+------+
A natural join is a type of join operation that creates an implicit join by combining tables
based on columns with the same name and data type
+------+-------+----------+-------+------+
+------+-------+----------+-------+------+
+------+-------+----------+-------+------+
MySQL CROSS JOIN is used to combine all possibilities of the two or more
tables and returns the result that contains every row from all contributing tables. The
CROSS JOIN is also known as CARTESIAN JOIN, which provides the Cartesian
product of all associated tables.
Syntax:
+-------+----------+------+------+-------+------+
+-------+----------+------+------+-------+------+
+-------+----------+------+------+-------+------+
The MySQL Inner Join is used to returns only those results from the tables
that match the specified condition and hides other rows and columns. MySQL assumes it
as a default Join, so it is optional to use the Inner Join keyword with the query.
Syntax:
+-------+----------+------+------+-------+------+
The LEFT JOIN returns all the rows from the table on the left even if no matching
rows have been found in the table on the right. Where no matches have been found in the
table on the right, NULL is returned.
Syntax:
+-------+----------+------+------+-------+------+
+-------+----------+------+------+-------+------+
RIGHT JOIN is obviously the opposite of LEFT JOIN. The RIGHT JOIN returns all the
columns from the table on the right even if no matching rows have been found in the
table on the left. Where no matches have been found in the table on the left, NULL is
returned.
Syntax:
SELECT column-lists FROM table1 Right outer join table2;
+-------+----------+------+------+-------+------+
+-------+----------+------+------+-------+------+
+-------+----------+------+------+-------+------+
Result:
Thus the MYSQL commands to execute the natural, equi and outer join
operations are executed and verified successfully.