




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Database Example for coding select , delete ,....
Typology: Exercises
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Branch Staff branchNo Street City StaffNo sName Position Sex DOB Salary branchNo
Create Database Staff_db; Show Databases; to show all the databases in MySQL Connect Staff_db; to connect to Staff_db database Show Tables; empty Create Table Branch ( branchNo char(7) Primary key, Street varchar(70), City varchar(30) ); Show Tables; Branch Desc Branch; to show the columns description of Branch table Create Table Staff ( StaffNo char(7) Primary key, sName varchar(50) not null, Position varchar(20), Sex char, DOB date, Salary double(7,2), branchNo char(7), Foreign key(branchNo) references Branch(branchNo) ON Delete Set Null ON Update Cascade ); Show Tables; Branch Staff Desc Staff; to show the columns description of Staff table
Test TestNo TestName Solution: Show Databases; Connect Staff_db; Show Tables; Branch Staff Create Table Test ( TestNo int, TestName varchar(30), Primary key(TestNo)); Show tables; Branch Staff Test Desc Test;
Drop Table Test; to drop (remove) Test table from the database Show Tables; Branch Staff
Select * from Branch; to show all rows and columns of Branch table Insert into Branch values (‘B005’,’22 Deer Rd’,’London’); Insert into Branch values (‘B00 7 ’,’16 Argyll St’,’Aberdeen’); Insert into Branch values (‘B00 3 ’,’163 Main St’,’Glasgow’); Insert into Branch values (‘B00 4 ’,’32 Manse Rd’,’Bristol’); Insert into Branch values (‘B00 2 ’,’56 Clover Dr’,’London’); Select * from Branch;
Select * from Staff; to show all columns and rows of Staff table Insert into Staff values (‘SL21’,’John’,’Manager’,’M’,’1970-10-1’,30000,’B005’); Insert into Staff values (‘SG37’,’Ann’,’Assistant’,’F’,’1995-11-10’,12000,’B003’)); Insert into Staff values (‘SG14’,’David’,’Supervisor’,’M’,’1982-3-24’,18000,’B003’); Insert into Staff values (‘SA9’,’Mary’,’Assistant’,’F’,’1990-2-19’,9000,’B007’); Insert into Staff values (‘SG5’,’Susan’,’Manager’,’F’,’1972-6-3’,24000,’B003’); Insert into Staff values (‘SL41’,’Julie’,’Assistant’,’F’,’1997-6-13’,9000,’B005’); Select * from Staff;
Branch Staff branchNo Street City StaffNo sName Position Sex DOB Salary branchNo B005 22 Deer Rd London SL21 John Manager M 1970 - 10 - 1 30000 B B007 16 Argyll St Aberdeen SG37 Ann Assistant F 1995 - 11 - 10 12000 B B003 163 Main St Glasgow SG14 David Supervisor M 1982 - 3 - 24 18000 B B004 32 Manse Rd Bristol SA9 Mary Assistant F 1990 - 2 - 19 9000 B B002 56 Clover Dr London SG5 Susan Manager F 1972 - 6 - 3 24000 B SL41 Julie Assistant F 1997 - 6 - 13 9000 B
Select * from Branch;
Select branchNo, City to show only branchNo and City columns From Branch;
Select branchNo, Street to show specific columns From Branch Where City = ’London’ ; where (condition): to show specific rows
Select sName, Position, Salary From Staff;
Select staffNo, sName, DOB From Staff Where Sex = ‘F’;
Branch Staff branchNo Street City StaffNo sName Position Sex DOB Salary branchNo B005 22 Deer Rd London SL21 John Manager M 1970 - 10 - 1 30000 B B007 16 Argyll St Aberdeen SG37 Ann Assistant F 1995 - 11 - 10 12000 B B003 163 Main St Glasgow SG14 David Supervisor M 1982 - 3 - 24 18000 B B004 32 Manse Rd Bristol SA9 Mary Assistant F 1990 - 2 - 19 9000 B B002 56 Clover Dr London SG5 Susan Manager F 1972 - 6 - 3 24000 B SL41 Julie Assistant F 1997 - 6 - 13 9000 B
Update Branch Set City = ‘Manchester’ the column(s) to be updated Where branchNo = ‘B004’; where (condition) to update specific row(s) Select * from Branch; to show the table rows after updating
Update Branch Set Street = ’555 Deer Rd’ Where City = ‘London’; Select * from Branch;
Update Staff Set Salary = 35000 Where Position = ‘Manager’; Select * from Staff;
Update Staff Set Salary = Salary + 3000 Where Position = ‘Assistant’; Select * from Staff;
Branch Staff branchNo Street City StaffNo sName Position Sex DOB Salary branchNo B005 22 Deer Rd London SL21 John Manager M 1970 - 10 - 1 30000 B B007 16 Argyll St Aberdeen SG37 Ann Assistant F 1995 - 11 - 10 12000 B B003 163 Main St Glasgow SG14 David Supervisor M 1982 - 3 - 24 18000 B B004 32 Manse Rd Bristol SA9 Mary Assistant F 1990 - 2 - 19 9000 B B002 56 Clover Dr London SG5 Susan Manager F 1972 - 6 - 3 24000 B SL41 Julie Assistant F 1997 - 6 - 13 9000 B
Delete from Branch Where City = ‘Bristol’; to delete specific row(s) Select * from Branch;
Delete from Staff Where Sex = ‘F’ and Position = ‘Assistant’; Select * from Branch;