sql平时作业

发布时间 2023-06-09 14:06:55作者: cspD-C

MySQL数据库 - 数据库和表的基本操作(一)

第一关:

修改表名:alter table 旧表名 rename 新表名;
查看表的基本结构:describe 表名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## modify the table name ##########

alter table tb_emp rename jd_emp;

########## show tables in this database ##########

show tables;

########## describe the table ##########

describe jd_emp;

########## End ##########

第二关:

修改字段名:alter table 表名 change 旧字段名 新字段名 新数据类型;
修改数据类型:alter table 表名 change modify 字段名 数据类型;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## change the column name ##########

alter table tb_emp change id prod_id int(11);

########## change the data type of column ##########

alter table tb_emp change Name Name varchar(30);

########## End ##########

DESCRIBE tb_emp;

第三关:

增加列:alter table 表名 add 新字段名 数据类型;
删除列:alter table 表名 drop 字段名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## add the column ##########

alter table tb_emp ADD Country varchar(20) after Name;
 
########## delete the column ##########

alter table tb_emp drop Salary;

########## End ##########

DESCRIBE tb_emp;

第四关:

修改字段排列位置:alter table 表名 modify 字段1 数据类型 [first/after] 字段二
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## modify the column to top ##########

alter table tb_emp modify Name varchar(25) first;

########## modify the column to the rear of another column ##########

alter table tb_emp modify DeptId int(11) after Salary;

########## End ##########

DESCRIBE tb_emp;

第五关:

删除外键约束:alter table 表名 drop foreign key 外键约束名;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the foreign key ##########

alter table tb_emp drop foreign key emp_dept;

########## End ##########
SHOW CREATE TABLE tb_emp \G;

数据库和表的基本操作(二)

第一关:

添加数据:insert into 表名(字段1,字段2,字段3...) values(对应数据...);
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## bundle insert the value ##########

insert into tb_emp(Id, Name, DeptId, Salary) values(1, "Nancy", 301, 2300.00), (2, "Tod", 303, 5600.00), (3, "Carly", 301, 3200.00);

########## End ##########
SELECT * FROM tb_emp;

第二关:

更新数据:
update 表名
set 字段名1 = ,字段名2 = ,...
where 字段名 = ();
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## update the value ##########

update tb_emp
set Name = "Tracy", DeptId = 302, Salary = 4300.00
where Id = 3;

########## End ##########

SELECT * FROM tb_emp;

第三关:

删除指定条件的数据:delete from 表名 where 条件;
USE Company;

#请在此处添加实现代码
########## Begin ##########

########## delete the value ##########

delete from tb_emp where Salary > 3000;

########## End ##########

SELECT * FROM tb_emp;