DBMS Lab1
DBMS Lab1
CLASS 1
MySQL
commands
□ help \h
□ Quit /exit \q
□ Cancel the command \c
□ Change database use
Info about databases and
tables
□ Listing the databases on the MySQL server host
■ >show databases;
□ Access/change database
■ >Use [database_name]
□ Showing the current selected database
■ > select database();
□ Showing tables in the current database
■ >show tables;
□ Showing the structure of a table
■ > describe [table_name]; or
■ > desc [table_name];
CREATE DATABASE
□ An SQL relation is defined using the CREATE
DATABASE command:
■ create database [database name];
□ Example
■ create database mydatabase;
SQL commands – Subgroups
DDL, DML, DCL, and TCL.
DDL Commands
□ DDL is short name of Data Definition Language, which deals
with database schemas and descriptions, of how the data
should reside in the database.
□ CREATE - to create a database and its objects like (table, index, views,
store procedure, function, and triggers)
□ ALTER - alters the structure of the existing database
□ DROP - delete objects from the database
□ TRUNCATE - remove all records from a table, including all spaces allocated
for the records are removed
□ COMMENT - add comments to the data dictionary
□ RENAME - rename an object
DML
Commands
□DML is short name of Data Manipulation Language which deals
with data manipulation and includes most common SQL
statements such SELECT, INSERT, UPDATE, DELETE, etc., and it is
used to store, modify, retrieve, delete and update data in a
database.
□ SELECT - retrieve data from a database
□ INSERT - insert data into a table
□ UPDATE - updates existing data within a table
□ DELETE - Delete all records from a database table
□ MERGE - UPSERT operation (insert or update)
□ CALL - call a PL/SQL or Java subprogram
□ EXPLAIN PLAN - interpretation of the data access path
□ LOCK TABLE - concurrency Control
DCL Commands
□ DCL is short name of Data Control Language which includes
commands such as GRANT and mostly concerned with rights,
permissions and other controls of the database system.
Types
□ The string types:
□ CHAR
□ VARCHAR
□ BINARY
□ VARBINARY
□ BLOB
□ TEXT
Type Description Display format Range in characters
CHAR Contains non- binary strings. Length Trailing spaces The length can be any value
is fixed as you declare while creating are removed. from 0 t 255.
a table. When stored, they are right-
padded with spaces to the specified
length
VARCHAR Contains non- binary strings. As stored. A value from 0
Columns are variable-length to 255 before
strings MySQL
5.0.3, and 0 to
65,535 in 5.0.3
and later
versions
BINARY Contains binary strings 0 to 255 BINARY
The purchasing of the items from different suppliers is based on the quotations that are
provided by various suppliers. The rules to purchase the items are defined as follows:
One supplier can provide quotations for different items. Different suppliers can provide
the quotation for the same item. For each item, a supplier has to provide a separate
quotation. Shop Rite management has the right to accept or reject a quotation based on
the quoted price. The management places the orders for the accepted quotations. Once
the supplier has supplied the ordered quantity, the quotation status will become closed.
• Create a database named COMPANY.
• For storing the supplier details, the supplier table needs to be
created. Table is created without any constraints.
Column name Data type
supplierid VARCHAR(6)
suppliername VARCHAR(30)
suppliercontactno VARCHAR(12)
supplieremailid VARCHAR(30)
Write SQL statement to add a record into the “supplier” table for a supplier
without email id is as follows VALUES ('S1002’,'EBATs’,'115‐340‐2345’,
NULL)
Create the table Retailstock with Retailoutletid and Itemcode as composite primary
key. Column name Data type
Retailoutletid VARCHAR(6)
Itemcode VARCHAR(6)
Retailunitprice INT
Quantityavailable INT
quotationid VARCHAR(6)
supplierid VARCHAR(6)
itemcode VARCHAR(10)
quotedprice INT
quotationdate DATE
quotationstatus VARCHAR(10)
CLASS 3
• The SELECT DISTINCT command returns only distinct (different) values in the result
set.
• The ORDER BY command is used to sort the result set in ascending or descending
order.
To get a list of unique departments and their highest salaries, sorted by salary in
descending order.
Create item table is as mentioned
below.
1. Retrieve the different item types.
2. Retrieve the different item types and categories of the items.
3. Retrieve the itemcode, description and price with the increasing order of item’s
price.
4. Retrieve the different item types and categories of the items in the increasing
order of item types and categories.
category CHAR(1)
COUNT ,GROUP BY , HAVING
AIM
phone_no INT
salary INT
city VARCHAR(255)
order_date DATE
quantity INT
total_amount INT
city VARCHAR(255)
VIEW
1. Create table Employee
2. Create table Department
3. CREATE VIEW RESEARCH_DPT
4. Display all the details of the created view
select college_name,faculty_count
from fac_count
where faculty_count =
(select max(faculty_count) from fac_count) or faculty_count=(select
min(faculty_count)from fac_count);
CLASS 5
My SQL Functions
AIM
To perform SELECT operations along with SQL functions on a given table
1. For a discount of 25.5% being offered on all FMCG item’s unit price,
display item code, existing unit price as “Old Price” and discounted price as
“New Price”. Round off the discounted price to two decimal values.
2. Retrieve the employee id, employee name of billing staff and the retail
outlet where they work. Perform a case insensitive search.
3. Retrieve the description of items which have more than 15 characters.
4. Display numeric part of supplier id.
5. Retrieve the maximum salary, minimum salary, total salary and average
salary of employees.
6. Retrieve the total number of items available in warehouse.
7. Retrieve the total number of orders made and the number of orders for
which payment has been done.
8. Retrieve the total number of different item types available.
CREATE TABLE item( itemcode VARCHAR(6) PRIMARY KEY, itemtype
VARCHAR(30), description VARCHAR(100) NOT NULL, price INT(10), reorderlevel
INT(6), quantityonhand INT(6), category VARCHAR(1) );
DCL commands
Grant and revoke permissions
CREATE USER 'SOBHA'@'localhost' IDENTIFIED BY '1234’;
GRANT ALL PRIVILEGES ON mydb.* TO 'SOBHA'@'localhost’;
SHOW GRANTS FOR 'SOBHA'@'localhost’;
REVOKE INSERT ON mydb.* FROM 'SOBHA'@'localhost’;
TCL commands
START TRANSACTION
COMMIT
ROLLBACK
SAVEPOINT
create database bank;
use bank;
CREATE TABLE IF NOT EXISTS employees (id INT AUTO_INCREMENT PRIMARY
KEY, name VARCHAR(100) NOT NULL,position VARCHAR(100),salary DECIMAL(10,
2));
INSERT INTO employees (name, position, salary) VALUES ('Alice Johnson', 'Engineer',
75000.00);
INSERT INTO employees (name, position, salary) VALUES ('Bob Smith', 'Manager',
85000.00);
SELECT * FROM employees;
START TRANSACTION;
UPDATE employees SET salary = 80000.00 WHERE name = 'Alice Johnson’;
SELECT * FROM employees;
ROLLBACK;
SELECT * FROM employees;
START TRANSACTION;
UPDATE employees SET salary = 80000.00 WHERE name = 'Alice Johnson’;
SELECT * FROM employees;
COMMIT;
ROLLBACK;
SELECT * FROM employees;
START TRANSACTION;
savepoint a;
INSERT INTO employees (name, position, salary) VALUES ('Carol White', 'Analyst',
72000.00);
SELECT * FROM employees;
ROLLBACK to savepoint a;
SELECT * FROM employees;
CLASS 7
Trigger
CREATE DATABASE SHOP;
USE SHOP;
mysql> DELIMITER //
mysql> CREATE TRIGGER update_last_updated
-> BEFORE UPDATE ON employees
-> FOR EACH ROW
-> BEGIN
-> SET NEW.last_updated = CURRENT_TIMESTAMP;
-> END//
mysql> DELIMITER ;
SHOW TRIGGERS;
INSERT INTO employees (name, position, salary) VALUES ('Alice Johnson',
'Engineer', 75000.00);
SELECT * FROM employees;
UPDATE employees SET salary = 80000.00 WHERE name = 'Alice Johnson';
SELECT * FROM employees;
CLASS 8
6. Create a table named ‘t1’ namely 10. Perform right join operation