0% found this document useful (0 votes)
3 views14 pages

SQL Lab

The document provides a comprehensive guide on creating and managing databases using MySQL, including creating tables for students, staff, courses, and stock, as well as performing operations like inserting, updating, deleting records, and applying aggregate functions. It also covers joining tables, creating users with privileges, and executing transactions with rollback and commit functionalities. Various SQL queries are demonstrated to retrieve and manipulate data effectively.

Uploaded by

devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views14 pages

SQL Lab

The document provides a comprehensive guide on creating and managing databases using MySQL, including creating tables for students, staff, courses, and stock, as well as performing operations like inserting, updating, deleting records, and applying aggregate functions. It also covers joining tables, creating users with privileges, and executing transactions with rollback and commit functionalities. Various SQL queries are demonstrated to retrieve and manipulate data effectively.

Uploaded by

devi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1. Create a college database with tables course, staff and student details.

Insert records into the


tables using MYSQL.
create database college
use college

create table student(stu_id int primary key, stu_name varchar(50),address varchar(50),city


varchar(50),pincode varchar(6), mobileNo varchar(10))

desc student

insert into student values(1,'Devi','15 Park street','Surandai','CS','8945681236')


insert into student values(3,'Selva','715 New Street','CS','8945681236')
insert into student values(4,'Ganesh','19 Market Street','BCA','8000681236')

select * from Student


create table staff(staff_id int primary key, staff_name varchar(50),address varchar(50),city
varchar(50),pincode varchar(6), mobileNo varchar(10))

desc staff

insert into staff values(1001,'Jason','10, Main street','Surandai','627859','9999000001')


insert into staff values(1002,'Pondevi','267, New Market Road','Pavoorchatram','627808','9000990001')

insert into staff values(1003,'Vishnu','266, Temple Road','Alangulam','627851','9900099991')

select * from staff


create table course(course_id int primary key, course_name varchar(50),duration_in_year int)
desc course

insert into course values(501,'BSC',3)


insert into course values(502,'BCA',3)
insert into course values(503,'MSC',2)
insert into course values(504,'MCA',3)
insert into course values(505,'MPhil',1)

select * from course


2. Write MYSQL queries to update, remove and Delete record in stock table (Item no, Item name,
Item quantity, price, total).
create database shop
use shop

create table stock(Item_id int primary key,Item_name varchar(50) not null,Quantity int,Price
float,Total float)
desc stock

insert into stock values(101,'Monitor',1,4000.00,4000.00)


insert into stock values(102,'Keyboard',2,1000.00,2000.00)
insert into stock values(103,'Printer',1,4500.00,4500.00)
insert into stock values(104,'Mouse',3,400.00,1200.00)
insert into stock values(105,'Pendrive',4,500.00,2000.00)

select * from stock


update stock set Item_name='Hard Disk' where Item_id=102
select * from stock
After Update:

delete from stock where Item_id=103


select * from stock
After delete:
3. Write MYSQL queries to retrieve relevant information from an employee table.
create database company
use company

create table employee(emp_id int primary key, emp_name varchar(50),address varchar(50),city


varchar(50),Age int, salary int)

desc employee

insert into employee values(5001,'Harsha','10, Main Road','Surandai',25,50000)


insert into employee values(5002,'selva','126, Market Road','Pavoorchatram',22,0000)
insert into employee values(5003,'Ganesh','266, Temple Road','Tenkasi',40,500000)

select * from employee


select * from employee where emp_name like '%a'

select * from employee where age >=25

select * from employee where emp_id=5001


4. Create a student marks table using MYSQL and apply aggregate functions.
create database college
use college

create table student_mark(stu_id int,Mark1 int, Mark2 int,Mark3 int, Mark4 int)

insert into student_mark values(101,89,70,65,90)


insert into student_mark values(102,56,60,55,80)
insert into student_mark values(103,97,90,95,99)
insert into student_mark values(104,75,70,75,70)
insert into student_mark values(105,45,50,45,40)

select * from Student_mark

select min(mark4) from student_mark

select max(mark3) from student_mark


select sum(mark2) from student_mark

select avg(mark2) from student_mark

select count(mark1) from student_mark


5. Write MYSQL queries to join two tables (Inner join, Outer join).
create database shop
use shop

create table customer(cust_id int primary key,cust_name varchar(50),cust_address varchar(100))


desc customer

insert into customer values(1001,'Guna','12, Main road,Surandai')


insert into customer values(1002,'Ganesh','122, Market road,Pavoorchatram')
insert into customer values(1003,'selva','512, New Street,Tenkasi')
select * from customer

create table orders(order_id int primary key,cust_id int,orderder_date datetime)


desc orders

insert into orders values(5001,1002,'2024-02-15')


insert into orders values(5002,1003,'2024-02-17')
insert into orders values(5003,1002,'2024-02-18')
select * from orders

select orders.order_id,orders.order_date,customer.cust_name,customer.cust_address from orders


INNER JOIN customer on orders.cust_id=customer.cust_id;

select orders.order_id,orders.order_date,customer.cust_name,customer.cust_address from orders


LEFT OUTER JOIN customer on orders.cust_id=customer.cust_id

select orders.order_id,orders.order_date,customer.cust_name,customer.cust_address from orders


RIGHT OUTER JOIN customer on orders.cust_id=customer.cust_id
6. Write MYSQL statement to create a new user and set a password and privileges for an existing
database.
USE mysql
SELECT user FROM user;

CREATE USER devi IDENTIFIED BY '123'


SHOW GRANTS FOR devi

GRANT SELECT ON college.* TO devi


SHOW GRANTS FOR devi

REVOKE SELECT ON college.* FROM devi


SHOW GRANTS FOR devi
7. Write MYSQL statement to get name of the student containing exactly four characters and
determine the age of each of the students.
create database college
use college

create table studentdata(sid int,sname varchar(50),dob datetime,phone varchar(10))

insert into studentdata values(101,'Kala','2002-8-15','8456984587')


insert into studentdata values(102,'Mala','2005-12-25','9012984587')
insert into studentdata values(103,'Bala','2000-6-10','9800084587')
insert into studentdata values(104,'selva','2003-11-15','9012984587')
insert into studentdata values(151,'Ganesh','2000-9-17','9800084587')

select * from studentdata

select sid,sname,TIMESTAMPDIFF(YEAR,dob,curdate()) AS AGE,phone from studentdata where


sname like'____'
8. Write MYSQL statement for rollback, commit and save option.
create database shop
use shop

CREATE TABLE Books(BookID SMALLINT NOT NULL PRIMARY KEY, BookTitle VARCHAR(60) NOT
NULL,Copyright YEAR NOT NULL)
desc books

START TRANSACTION;
INSERT INTO Books VALUES (101,'Programming in JAVA', 1994)
INSERT INTO Books VALUES (102, 'Programming in VB', 1989)
SELECT * FROM BOOKS
SAVEPOINT sp1
INSERT INTO Books VALUES (103,'Programming in C#', 2004)
INSERT INTO Books VALUES (104, 'Programming in Python', 2001)
SELECT * FROM BOOKS
ROLLBACK TO SAVEPOINT sp1
SELECT * FROM BOOKS
INSERT INTO Books VALUES (105,'Programming in C', 1995)
INSERT INTO Books VALUES (106, 'Programming in HTML', 1998)
COMMIT;
SELECT * FROM BOOKS;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy