DBMS Lab File
DBMS Lab File
(DBMS: Database language-DML, DDL, DCL, TCL (write the theory and syntax for each command
present in DDL, DML, DCL, TCL)):
DDL:
Aim: To understand the syntax of basic DDL commands such as
o Create table
o Alter table
Add/Drop attribute
Modify type and size of an attribute
Rename attribute
o Truncate
o Drop
Database used: Student
Table name: student
Students(rollno,name)Add branch
Modify the width of Attribute branch
Modify the datatype of Attribute
branch Rename the attribute
Remove the attribute
o Insert
o Update
o Delete
Database used: Student
mysql> create table students (rollno int, name char (20), branch varchar (20));
students;
Constraints
Aim: Objective to understand the commands used to impose constraints on a table such as
o Not null
o unique
o primary key
o foreign key
Database used: Bank
Branch ()
Account ()
Loan ()
Customer ()
Depositor ()
Borrower ()
mysql> create table Branch (branch_name varchar (15) , branch city varchar (15) not null,assets
integer not null, primary key(branch_name));
mysql> create table Account (account_number varchar (15), branch_name varchar (15) not
null,balance integer not null, primary key(account_number), foreign key(branch_name)
references Branch(branch_name));
(Only syntax)
AIM: Analyze the problem and come with the entities in it.
Weak Entity: A weak entity is an entity that cannot be uniquely identified by its
attributes alone. It depends on the existence of a related entity. It is
represented by a double rectangle in an ER diagram.
Exp No: 3
AIM: Represent all the entities in tabular fashion for Banking Database
(Tables with maximum 2 entries with output i.e. table diagram using SQL queries)
AIM: Create a Customer Database Table and insert datas using all the SQL queries
(Create, insert, update, delete, drop, alter with output i.e. table diagram)
(Tables with maximum 2 entries with syntax, query and output i.e. table diagram)
mysql> create table account(acc_no int PRIMARY KEY,acc_type varchar(10) NOT NULL
CHECK(acc_type IN('D','C')),bal int CHECK(bal>=0));
Query OK, 0 rows affected (0.04 sec)
(MIN, MAX, COUNT, SUM, AVG-write theory about aggregate functions with syntax of
each and its use)
(5 to 6 entries in account table with 3 attributes)
Aggregate functions are used to perform calculations on a set of values and return a single
result. They are commonly used in SQL queries to summarize data and provide insights into
large datasets. The most commonly used aggregate functions are MIN, MAX, COUNT,
SUM, and AVG.
1) MIN: The MIN function returns the minimum value in a set of values. The syntax for
the MIN function is:
(1) SELECT MIN(column_name)
(2) FROM table_name;
2) MAX: The MAX function returns the maximum value in a set of values. The syntax
for the MAX function is:
i) SELECT MAX(column_name)
ii) FROM table_name;
3) COUNT: The COUNT function returns the number of rows in a table or the number
of values in a specified column. The syntax for the COUNT function is:
i) SELECT COUNT(*|column_name)
ii) FROM table_name;
4) SUM: The SUM function returns the sum of all the values in a specified column.
The syntax for the SUM function is:
i) SELECT SUM(column_name)
ii) FROM table_name;
5) AVG: The AVG function returns the average value of a specified column. The syntax
for the AVG function is:
i) SELECT AVG(column_name)
ii) FROM table_name;
mysql> create database aggregate;
Query OK, 1 row affected (0.01 sec)
mysql> use aggregate;
Database changed
mysql> create table account(acc_no int, acc_branch varchar(40),bal int);
Query OK, 0 rows affected (0.05 sec)
(AND, OR, NOT, NOR, BETWEEN, IN, LIKE {use all like operation symbols},
EXISTS, ALL, ANY- write theory about Logical operators with syntax of each and its
use)
(5 to 6 entries in customer table)
Logical operators are used in SQL to combine or negate multiple conditions in a query. The
most commonly used logical operators in SQL are AND, OR, NOT, NOR, BETWEEN, IN,
LIKE, EXISTS, ALL, and ANY.
1. AND: The AND operator returns true if all conditions in a query are true. The syntax
for the AND operator is:
For example, to select all customers from a table called "Customers" who are
from the USA and have a "Premium" membership, you would use the following
query:
2. OR: The OR operator returns true if any of the conditions in a query are true. The syntax
for the OR operator is:
For example, to select all customers from a table called "Customers" who are from
the USA or have a "Premium" membership, you would use the following query:
SELECT *
FROM Customers
WHERE country = 'USA' OR membership = 'Premium';
3. NOT: The NOT operator negates a condition in a query. The syntax for the NOT
operator is:
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
For example, to select all customers from a table called "Customers" who are not from
the USA, you would use the following query:
SELECT *
FROM Customers
WHERE NOT country = 'USA';
4. NOR: The NOR operator returns true only if none of the conditions in a query are
true. The syntax for the NOR operator is:
For example, to select all customers from a table called "Customers" who are not from
the USA or have a "Premium" membership, you would use the following query:
SELECT *
FROM Customers
WHERE NOT (country = 'USA' OR membership = 'Premium');
5. BETWEEN: The BETWEEN operator selects values within a range. The syntax for
the BETWEEN operator is:
For example, to select all orders from a table called "Orders" with order dates between
January 1, 2022, and December 31, 2022, you would use the following query:
SELECT *
FROM Orders
WHERE order_date BETWEEN '2022-01-01' AND '2022-12-31';
6. IN: The IN operator selects values that match any of a specified list of values. The
syntax for the IN operator is:
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
For example, to select all customers from a table called "Customers" who are from the
USA, Canada, or Mexico, you would use the following query:
SELECT *
FROM Customers
WHERE country IN ('USA', 'Canada', 'Mexico');
7. LIKE: The LIKE operator selects values that match a specified pattern using wildcard
characters.
mysql> select cust_id from customer where cust_id BETWEEN 02 AND 04;
+ -+
| cust_id |
+ -+
| 2|
| 3|
| 4|
+ -+
3 rows in set (0.01 sec)
mysql> select cust_id from customer where cust_id>ALL(select cust_id from customer
where cust_city="ajmer");
+ -+
| cust_id |
+ -+
| 4|
| 5|
+ -+
2 rows in set (0.01 sec)
AIM: Write SQL query using Group by Function, Having and Order By Function