0% found this document useful (0 votes)
15 views13 pages

DBMS Lab Manual Exp 3,4 & 5

The document outlines SQL commands and concepts related to database management, including DDL, DML, DCL, and TCL, along with examples of ALTER, UPDATE, and DELETE statements. It also covers aggregate functions like MAX(), MIN(), AVG(), and COUNT(), and provides insights into normalization and its various forms. Additionally, it includes practical examples of inserting, updating, and deleting records in a database.

Uploaded by

fopivac546
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)
15 views13 pages

DBMS Lab Manual Exp 3,4 & 5

The document outlines SQL commands and concepts related to database management, including DDL, DML, DCL, and TCL, along with examples of ALTER, UPDATE, and DELETE statements. It also covers aggregate functions like MAX(), MIN(), AVG(), and COUNT(), and provides insights into normalization and its various forms. Additionally, it includes practical examples of inserting, updating, and deleting records in a database.

Uploaded by

fopivac546
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/ 13

Global Institute of Technology

Department of Computer Science & Engineering

Experiment -3
Aim- Write a SQL statement for implementing ALTER, UPDATE and DELETE.

Database Language: Database languages are used to read, update and store data in a database.
Read, Update, Manipulate, and Store data in a database using Database Languages. There are four
types of DBMS language:
1. DDL (Data Definition Language)
2. DML (Data Manipulation Language)
3. DCL (Data Control Language)
4. TCL (Transaction Control Language)

DDL (Data Definition language) - DDL is used when we need to create database and tables, alter them,
drop them and rename the table.

• CREATE: Create new database, table.

CREATE DATABASE dbname;


CREATE TABLE bank (id INT(16) PRIMARY KEY AUTO INCREMENT,
name VARCHAR(255) NOT NULL);

• ALTER: Alter existing database, table.

ALTER TABLE bank ADD COLUMN lastname VARCHAR(255) NOT NULL;

• DROP: Drop the database

DROP DATABASE dbname;


DROP TABLE bank;

• RENAME: Set a new name for the table.

RENAME TABLE bank TO student;

• TRUNCATE : To delete tables in a database instance

TRUNCATE student;

DML (Data Manipulation Language) - DML language is used to manipulate the database like
inserting data, updating table, retrieving record from a table .

• SELECT: Retrieve data from the database

SELECT * FROM student;


16
Global Institute of Technology
Department of Computer Science & Engineering

• INSERT: Insert data

INSERT INTO student (name, lastname) VALUES ('nams', 'Shivi),•

• UPDATE: Update data

UPDATE student SET name — 'Dia' WHERE lastname — 'Shivi',

DELETE: Delete all records

DELETE FROM student WHERE name — 'Dia',

• MERGE: It performs UPSERT (update and insert ) operation

DCL (Data Control Language) : Grant privilege to a user using the GRANT statement. revoke the
privilege using the REVOKE statement.

• GRANT: Give privilege to access the database.

CREATE USER 'dia'@’localhost’IDENTIFIED BY ’123';


GRANT ALL PRIVILEGES ON student TO 'dia'@'localhost',
FLUSH PRIVILEGES;

• REVOKE: Take back the privilege to access the database.

REVOKE ALL PRIVILEGES ON student* FROM 'dia’@’localhost';


FLUSH PRIVILEGES;

TCL (Transaction Control Language):Manage transactions in the Database using the Transaction
Control Language:

• COMMIT: Save the work.

START TRANSACTION,
INSERT INTO student (name, lastname) VALUES ('Dia', 'Shivi),
COMMIT,

• SAVEPOINT: Set a point in transaction to rollback later


• ROLLBACK: Restores since last commit

START TRANSACTION,
INSERTINTO student (name, lastname) VALUES ('Dia', 'Shivi);
ROLLBACK;

17
Global Institute of Technology
Department of Computer Science & Engineering

SELECT COMMAND

1. Viewing all data from table

a) Using all column names


Syntax:
Select <col> to <col n> from tablename;
Ex:
Select empno,ename,address,city from emp;

b) Using select *
Syntax:
Select * from tablename;
Ex:
Select * from emp;

c) View Selected Columns


Syntax:
Select <col1>,<col2> from <tablename>;
Ex:
Select empno,ename from emp;

d) List only the different (distinct) values in a table

Syntax:
Select distinct <col> from <tablename>;
Ex:
Select distinct ename from emp;

18
Global Institute of Technology
Department of Computer Science & Engineering

VIVA QUESTIONS

1. Explain DML, DDL, DCL and TCL statements with examples?


DML: DML stands for Data Manipulation Language. DML is used to retrieve, store, modify, delete,
insert and update data in database.
Examples of DML statements: SELECT, UPDATE, INSERT, DELETE statements.

DDL: DDL stands for Data Definition Language. DDL is used to create and modify the structure of
database objects.
Examples: CREATE, ALTER, DROP statements.

DCL: DCL stands for Data Control Language. DCL is used to create roles, grant and revoke
permissions, establish referential integrity etc.
Examples: GRANT, REVOKE statements

TCL: TCL stands for Transactional Control Language. TCL is used to manage transactions within a
database.
Examples: COMMIT, ROLLBACK statements.

2. What do you mean by table and field in SQL?


A table refers to a collection of data in an organised manner in form of rows and columns. A field
refers to the number of columns in a table.
Example: Table: Student
Field: Stu_Id, Stu_Name, Stu_Marks

3. What is the SELECT and SELECT INTO statement?


A SELECT command gets zero or more rows from one or more database tables or views. SELECT
INTO command selects data from one table and inserts it into another.

4. What is the difference between DELETE and TRUNCATE?


DELETE is a DML statement used to remove specific rows from a table, whereas TRUNCATE is a
DDL statement used to remove all rows from a table

5. What is the difference between DROP and TRUNCATE?


Drop" in SQL refers to the permanently removal of table and its contents. "Truncate" is a SQL
command that removes all rows from a table but keeps the table's structure intact.

6. What is the difference between ALTER and UPDATE?


ALTER is a DDL statement used for updating the structure of tables in a database, whereas UPDATE
is a DML statement used for manipulating the data of existing columns but can’t change the
definition of a table.

19
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment-4

Aim- Write the query for implementing the following functions: MAX (),
MIN(), AVG () and COUNT ().

Aggregate Functions: Aggregate functions perform a calculation on a set of rows and return
a single row. The result of an aggregate function is a single value.

These functions are called aggregate functions because they operate on the aggregate of tuples.

You can use aggregate functions as expressions only in the following clauses i.e., SELECT
clause, HAVING clause. There are five aggregate functions:

• AVG() — returns the average value.


• COUNT() — returns the number of values.
• MAX() — returns the maximum value.
• MIN() — returns the minimum value.
• SUM() — returns the sum of all or distinct values.

AVG(): The AVG() function allows you to calculate the average value of a numeric column.

syntax : AVG(expression)

SELECT AVG(SALARY) “AVERAGE SAL” FROM emp1;

Output :

AVERAGE SAL

22000.0000

COUNT(): The COUNT() function returns the total number of values/rows in the specified field.

Syntax: COUNT(expression)

SELECT COUNT(*) FROM emp1;

Output:
count(*)
6

20
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

SELECT COUNT(DISTINCT Department) FROM emp1;


Output :

Department

MAX() : The MAX() function returns the maximum value from the specified table field.

syntax : MAX(expression)

SELECT MAX(Salary) FROM emp1;

Output :

MAX(Salary
)
30000

MIN(): The MIN() function returns the minimum value in the specified table field.

syntax : MIN(expression)

SELECT MIN(Salary)FROM emp1;

Output :

MIN(Salary)

SUM(): SUM() function returns the sum of all the values in the specified column. SUM() works
on numeric fields only. Null values are excluded from the result returned.

Syntax: SUM (expression)

SELECT SUM(Salary) "Total Salary” from emp1;

21
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Output:

Total Salary

132000

VIVA QUESTIONS:

1. Explain the aggregate functions in SQL.


The aggregate functions are used to perform the calculation on multiple rows of a single
column of a table and return a single value and also used to summarize the data.

There are five aggregate functions:


• AVG() — returns the average value.
• COUNT() — returns the number of values.
• MAX() — returns the maximum value.
• MIN() — returns the minimum value.
• SUM() — returns the sum of all or distinct values.

2. What is difference between count() and sum() function?


The COUNT() function returns the number of rows in a database table. It can work both
numeric and non-numeric datatypes.
The SUM() function returns the total sum of all numeric values in a column. It works on
numeric field only.

3. Describe the data types in SQL.


SQL supports various data types, including numeric, character, date/time, and binary types
such as int, varchar, date, blob etc.

4. What is the difference between SQL and MySQL?


SQL is a standard database language for retrieving, manipulating the relational database.
MySQL is a relational database management system, like SQL Server, Oracle or IBM DB2,
that is used to manage SQL databases.

5. Differences Between Char and Varchar in SQL?


Char data type has a fixed size and stores data of fixed length whereas varchar has a variable
size and stores variable format data.
6. How do you use the DISTINCT keyword in SQL?
The DISTINCT keyword in SQL is used to retrieve unique, distinct values from a specified
column or set of columns in a table.

7. Explain the DATE and TIME functions in SQL.


DATE and TIME functions that allow you to work with date and time values in various ways
like performing calculations, formatting, and extracting specific components of date and time
values.

22
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Experiment-5

Aim-Perform the following operation for demonstrating the insertion, updation and deletion.

INSERTION Query

Syntax : INSERT INTO table(column1, column2, ...) VALUES (value1, value2, ...);

 Inserting only values

Example :

INSERT INTO college VALUES(l01, 'Alia', 'CSE', 'B',9876540010) ;

INSERT INTO college VALUES(102, 'Alisha', 'ME','A',9876541110);

Output :

OLL_NO NAME DEPT SEC PHONE

B
101 Alia CSE 9876540010

102 Alisha ME A 9876541110

 Inserting values in only specified columns

Example :

INSERT INTO college(ROLL_NO, NAME, SEC)VALUES(104,'Aditya','A');


INSERT INTO college(ROLL_NO, NAME, SEC)VALUES(105,'Nitya','B');
INSERT INTO college(ROLL_NO, NAME, SEC)VALUES(106,'Anaya','C');

Select * from college;

23
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Output:

Roll_NO NAME DEPT SEC PHONE


101 Alia CSE B 9876540010
102 Alisha ME A 9876541110
104 Aditya NULL A NULL
105 Nitya NULL B NULL
106 Anaya NULL C NULL

UPDATION Query :

syntax : UPDATE table name SET column1 value1, column2 value2,


...WHERE condition;

 Updating single column :

UPDATE college SET NAME = 'Amit' WHERE ROLL_NO = 104;

Select * from college;

Output :

Roll_NO NAME DEPT SEC PHONE


101 Alia CSE B 9876540010
102 Alisha ME A 9876541110
104 Amit NULL A NULL
105 Nitya NULL B NULL
106 Anaya NULL C NULL

 Updating multiple columns:

Example :

UPDATE college SET NAME = 'Atulya', DEPT = 'ECE' WHERE ROLL_NO = 105;

Select * from college;


Output :

24
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Roll_NO NAME DEPT SEC PHONE


101 Alia CSE B 9876540010
102 Alisha ME A 9876541110
104 Amit NULL A NULL
105 Atulya ECE B NULL
106 Anaya NULL C NULL

DELETION Query :

syntax : DELETE FROM table name WHERE condition;

 Deleting single record:

Example :

Select*from college;

Output :

Roll_NO NAME DEPT SEC PHONE


101 Alia CSE B 9876540010
102 Alisha ME A 9876541110
104 Amit NULL A NULL
105 Nitya NULL B NULL

• Deleting multiple records:

DELETE FROM college WHERE SEC = 'A';

ELECT * FROM college;

25
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

Output :

Roll_NO NAME DEPT SEC PHONE


101 Alia CSE B 9876540010
105 Nitya NULL B NULL

 Delete all of the records:

Delete from college;


OR
Delete * from college;

SQL Alter Query

ALTER : ALTER TABLE is used to add, delete/drop or modify columns in the existing table. It is
also used to add and drop various constraints on the existing table.

Syntax: ALTER TABLE table_name ADD (Columnname_1 datatype, Columnname_2


datatype,… Columnname_n datatype);

ALTER Table - DROP :

Syntax: ALTER TABLE table_name DROP COLUMN Column_name;

Example : Table Name – School

Roll_NO Name Class Address Marks

S-1 Aman 2 Goa 100


S-2 Ankita 3 Delhi 99
S-3 Ananya 4 Jaipur 98
S-4 Atul 5 Sikar 100

26
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

ALTER TABLE School DROP COLUMN Address;


Output :

Roll_NO Name Class Marks

S-1 Aman 2 100


S-2 Ankita 3 99
S-3 Ananya 4 98
S-4 Atul 5 100

 ALTER Table - MODIFY :

Syntax : ALTER TABLE table_ name MODIFY column_ name datatype;

Example :

Output :

Roll_NO Name Class Marks Age Subject

S-1 Aman 2 100


S-2 Ankita 3 99
S-3 Ananya 4 98
S-4 Atul 5 100

VIVA QUESTION

1. What is normalization in SQL?

Normalization is the process of organizing data in a database to eliminate


redundancy and dependency issues. It involves splitting tables into smaller, more
manageable entities.

27
Global Institute of Technology
DEPARTMENT OF COMPUTER SCIENCE ENGINEERING

2. Explain the different normal forms for normalization.

The most common normal forms are:


 First Normal Form(1NF) - A relation is in 1NF if it contains an atomic value.
 Second Normal Form(2NF) - A relation is in 2NF if it is in 1NF and each non-
prime attributes are functionally dependent in totality on the primary key.
 Third Normal Form(3NF) - A relation is in 3NF if it is in 2NF and no transition
dependency exists means no non-prime attribute is transitively dependent on the
primary key
 Boyce & Codd Normal Form(BCNF) - A relation is said to be in BCNF if it is
in 3NF and for every functional dependency X-->Y, X is the super key of the
table. It is stricter than 3NF.

3. Explain the advantages of Normalization.


 Normalization helps to minimize data redundancy.
 Greater overall database organization.
 Data consistency within the database.
 Much more flexible database design.
 Enforces the concept of relational integrity.

4. What is a CLAUSE in terms of SQL?


This is used with the SQL queries to fetch specific data as per the requirements on
the basis of the conditions that are put in the SQL.
Example, There is a query that has a WHERE condition or the query with the
HAVING clause.

5. What is the difference between a database schema and a database state?

The collection of information stored in a database at a particular moment in time is


called database state while the overall design of the database is called the
database schema.

6. What is a view in SQL?


A view is a virtual table based on the result-set of an SQL statement.

28

You might also like