Cs Record SQL
Cs Record SQL
SQL Command: -
2.Write an SQL command to create a STUDENT TABLE with the following attributes and
fields.
TABLE: STUDENT
mysql> Create table student (stud_id int primary key, stud_name varchar(20), class
char(2), sec char(2),stream varchar(10));
3.Write an SQL command to display the structure of the table named student.
mysql> Desc student; (or) Describe student;
OUTPUT:
mysql> insert into student values (1, “Ajay”, “12”, “A”,” Science”);
mysql> insert into student values (2, ”Siva”,”12”,”B”,”Science”);
1
OUTPUT:
OUTPUT:
2
MYSQL EXERCISE # 2
Aim: - To create an employee table and execute the following queries.
Table: Employee
Table: Department
1.Write an SQL query to fetch unique values of city from Employee table.
mysql> Select DISTINCT city from Employee;
OUTPUT:
2. Write an SQL query to print all Employee details from the Employee table order by
Employee Name Ascending.
mysql> Select * from Employee order by emp_name;
OUTPUT:
3
3. Write an SQL query to print details for department name with the dept_name as “HR”
and “Admin” from department table.
mysql> Select * from department where dept_name in (“HR”,”Admin”);
OUTPUT:
5. Write an SQL query to print details of the Employee whose Name ends with “a”.
mysql> Select emp_name from employee where emp_name like ‘%a’;
OUTPUT:
6. Write an SQL query to print details of workers whose Name contains four characters.
mysql> Select * from Employee where emp_name like ‘____’;
OUTPUT:
4
7. Write an SQL query to print details of Employee whose salary lies between 45000 and
50000.
mysql> Select * from employee where salary between 45000 and 50000;
OUTPUT:
8. Write an SQL query to print details of Employee name and their department name.
mysql> Select emp_name, dept_name from employee e, department d where e.emp_id =
d.emp_id;
OUTPUT:
5
MYSQL EXERCISE # 3
Aim: - To create a stock table and execute the following queries.
Table: STOCK
1.Compute the number of products with a price larger than or equal to 20.
mysql> Select count(*) from stock where price>=20;
OUTPUT:
2. Select the name and price of all products with a price is less than 50 and sort price by
descending order;
mysql> select stock_name, price from stock where price<50 order by price desc;
OUTPUT:
6
4. Apply a 5% discount to all products with a price larger than 25;
mysql> select stock_name, price, price-price*5/100 as 'Discount price' from stock where
price>25;
OUTPUT:
7. To display the details of all products whose stock is purchased January month.
mysql> select * from stock where dob>="2024-01-01" and dob<="2024-01-31";
OUTPUT:
8. Write an SQL query to delete the record whose stock price is 10.
mysql> delete from stock where price=10;
7
MYSQL EXERCISE # 4
Aim: - To create a book table and execute the following queries.
Table: BOOK
Table: AUTHOR
2. Write an SQL query to display the details of book name and author name of English
book.
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and book_name="eng";
OUTPUT:
8
3. Write an SQL query to add a primary key of book table.
mysql> alter table book add primary key (book_id);
OUTPUT:
4. Write an SQL query to display the book name and author name whose price is more
than 250 and quantity in not null;
mysql> select book_name, author_name from book b, author a where
b.book_id=a.book_id and price>250 and qty is not null;
OUTPUT:
5. Write an SQL query to display the sum of all price from book table.
mysql> select sum(price) from book;
OUTPUT:
6. Write an SQL query to display the details of book whose price is null;
mysql> select * from book where price is null;
OUTPUT:
9
7. Write an SQL query to display the total price without repetition and null.
mysql> select count(distinct price) from book;
10
INTERFACE PROGRAMS -
STORE AND DISPLAY AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and
display employee details.
SOURCE CODE:
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
query="select * from employee"
cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")
STORE AND UPDATE AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and
update employee details.
INTERFACE PROGRAMS -
import mysql.connector as mycon
con = mycon.connect(host='localhost',user='root',password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. UPDATE RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice==2:
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO UPDATE :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchone()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT","%10s"%"SALARY")
print("%10s"%result[0],"%20s"%result[1],"%15s"%result[2],"%10s"%result[3])
choice=input("\n## ARE YOUR SURE TO UPDATE ? (Y) :")
if choice.lower()=='y':
print("== YOU CAN UPDATE ONLY DEPT AND SALARY ==")
d = input("ENTER NEW DEPARTMENT:")
s = int(input("ENTER NEW SALARY:"))
query="update employee set dept='{}',salary={} where empno={}".format(d,s,eno)
cur.execute(query)
con.commit()
print("## RECORD UPDATED ## ")
ans=input("UPDATE MORE (Y) :")
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")
STORE AND SEARCH AN EMPLOYEE RECORD
AIM: To write a Python program to connect with database and to store an employee record and search
employee details.
SOURCE CODE:
import mysql.connector as mycon
con = mycon.connect(host='localhost', user='root', password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table employee(empno int, name varchar(20), dept varchar(20), salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. SEARCH RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee2 values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice==2:
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchone()
if cur.rowcount==0:
print("Sorry! Empnumber not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME", "%15s"%"DEPARTMENT","%10s"%"SALARY")
print("%10s"%result[0],"%20s"%result[1],"%15s"%result[2],"%10s"%result[3])
ans=input("SEARCH MORE (Y/N) :")
elif choice==0:
con.close()
print("## Bye!!Thank you!!! ##")
else:
print("## INVALID CHOICE ##")