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.
CHAR Contains non- binary strings. Length is Trailing spaces are The length can be any value from 0 t
fixed as you declare while creating a table. removed. 255.
When stored, they are right-padded with
spaces to the specified length
VARCHAR Contains non- binary strings. Columns As stored. A value from 0 to
are variable-length strings 255 before MySQL
5.0.3, and 0 to
65,535 in 5.0.3 and later versions
BINARY Contains binary strings 0 to 255 BINARY
BLOB Large binary object that containing a TINYBLOB Maximum length of 255 characters.
variable amount of data. Values are treated
as binary strings. You don't need to MEDIUMBLOB Maximum length of 16777215
specify length while creating a column characters.
LONGBLOB Maximum length of 4294967295
characters
TEXT Values are treated as character TINYBLOB Maximum length of 255 characters.
strings having a character set
MEDIUMBLOB Maximum length of 16777215
characters.
Creation of Database Schema
and DML Commands
CREATE TABLE EMPLOYEE(Fname VARCHAR(20),Minit CHAR,Lname
VARCHAR(20),SSN INT,Bdate DATE,Address VARCHAR(50),Sex
CHAR,Salary INT,Super_ssn INT,Dno INT,PRIMARY KEY(SSN));
CREATE TABLE DEPARTMENT(Dname VARCHAR(20),Dnumber INT,Mgr_ssn
INT,Mgr_start_date DATE,PRIMARY KEY(Dnumber));
CREATE TABLE DEPT_LOCATIONS(Dnumber INT NOT NULL,Dlocation
VARCHAR(20),PRIMARY KEY(Dnumber, Dlocation));
CREATE TABLE PROJECT(Pname VARCHAR(20),Pnumber INT,Plocation
VARCHAR(20),Dnum INT,PRIMARY KEY(Pnumber));
CREATE TABLE WORKS_ON(Essn INT,Pno INT,Hours FLOAT,PRIMARY
KEY(Essn, Pno));
CREATE TABLE DEPENDENT(Essn INT,Dependent_name VARCHAR(20),Sex
CHAR,Bdate DATE,Relationship VARCHAR(20),PRIMARY KEY(Essn,
Dependent_name));
SHOW TABLES;
DESC DEPARTMENT;
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(Dnumber)
REFERENCES DEPARTMENT(Dnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(Dnum) REFERENCES
DEPARTMENT(Dnumber);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(Essn) REFERENCES
EMPLOYEE(SSN);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(Pno) REFERENCES
PROJECT(Pnumber);
ALTER TABLE DEPENDENT ADD FOREIGN KEY(Essn) REFERENCES
EMPLOYEE(SSN);
ALTER TABLE EMPLOYEE ADD email VARCHAR(25);
DESC EMPLOYEE;
ALTER TABLE EMPLOYEE MODIFY email VARCHAR(10) NOT NULL;
SHOW COLUMNS FROM EMPLOYEE;
ALTER TABLE EMPLOYEE DROP COLUMN email;
DML COMMANDS
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
Insert a record into retailstock table with:
1) Retailoutletid as ‘R1001’ and Itemcode as ‘I1001’.
2) Retailoutletid as ‘R1001’ and Itemcode as ‘I1002’.
3) Retailoutletid as ‘R1001’ and Itemcode as ‘I1001’.
4) Retailoutletid as ‘R1002’ and Itemcode as ‘I1001’.
5) Retailoutletid as NULL and Itemcode as ‘I1001’.
Create quotation table with supplierid as foreign key:
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.
price INT
COUNT ,GROUP BY , HAVING
AIM
phone_no INT
salary INT
city VARCHAR(255)
• Insert to workers 10 tuples like (1, 'John', 'john@example.com', 1234567890,
40000, 'New York’)
• Count the number of workers in each city
• Count the number of workers for each combination of city and salary
• Count the number of workers in each city and order the results by the number
of employees in descending order
• Count the number of workers in each city where the average salary is greater
than $30,000
Create Column Name Data Type Constraint
order_date DATE
quantity INT
total_amount INT
city VARCHAR(255)
• Count the number of orders in each city
• Count the number of orders in each city and order the results by the number of
orders in descending order
• Count the number of orders in each city where the average total amount is
greater than $100
• Count the number of distinct customers in each city
• Find the total quantity of products ordered in each city
• List the average total amount for each city, ordered by average total amount in
descending order
CLASS 4
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