1.數(shù)據(jù)庫中有如下兩個表:
表1:employee
員工編號:employee_id (NOT allows null)
員工姓名:employee_name (NOT allows null)
年 齡 :age,
雇用日期:hire_date,
部門:department
表2:salary
員工編號:employee_id
員工工資:salary
關(guān)于日期函數(shù):
year(date)返回日期中的年份谍椅;
month(date)返回日期中的月份;
day(date)返回日期中的天妓美;
(1)計算2015年以后雇傭的員工個數(shù)
select count(*) from employee where year(hire_date)>2015;
(2)工資大于9000的員工編號尖滚、姓名和工資
select employee.employee_id,employee.employee_name,salary.salary from employy,salary where salary>900 and employee.employee_id=salart.employee_id;
(3)計算各個部門的員工個數(shù)茬射,表頭顯示為:部門、員工個數(shù)
select department as 部門,count(*) as 員工個數(shù) from employee group by department;
(4)按工資的高低列出工資表
select * from salary order by salary;
(5)個人操作中用到的一些sql語句
select * from salary order by salary desc;
刪除表中的一個字段:
alter table salary drop column employee_id;
新增字段salary:
alter table salary? add employee_id
2.數(shù)據(jù)庫查詢 對下面兩張表進(jìn)行查詢操作:
學(xué)生信息表student:
班級信息表class:
(1)目前要查詢班主任LiFang的班級下全體學(xué)生的信息情況。 要求:用兩種不同的sql查詢語句(連接查詢和嵌套查詢)村怪,并說明兩種sql語句的執(zhí)行效率哪個更高以及原因法瑟。
a.連接查詢
select * from student? inner join class on calss.class_id =student.calss_id where class.Master="Li Fang";
b.嵌套查詢
select * from student calss_id in(select calss_id from class where Matser="Li Fang");
3)操作中遇到的一些問題
插入數(shù)據(jù)的時候報了下面的錯誤:
該錯誤是由于輸入了中文冀膝,Class_ID 的屬性為char(255),不能接收中文,修改字段的屬性為下面即可:\
alter table student modify Class_ID char(255) charset uft8;
修改某個字段的內(nèi)容(把Li XiaoTing的Student_ID號改為2)
update student set Student_ID=2 where Student_Name="Li Xiao Ting";