Touge MySQL Database - Basic Operations of Databases and Tables (1) Answers

Level 1: View table structure and modify table name

programming requirements

According to the prompt, supplement the code in the editor on the right:

  • Rename the data table tb_emp to jd_emp ;

  • View the list of data tables under the database;

  • See the basic structure of the data table jd_emp .

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 ##########

Level 2: Modify field name and field data type

programming requirements

According to the prompt, supplement the code in the editor on the right:

  • Rename the field Id of the data table tb_emp to prod_id , and the data type remains unchanged;

  • Change the data type of the data table tb_emp field Name to varchar(30) .

The data table structure is as follows:

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 MODIFY Name varchar(30);


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

DESCRIBE tb_emp;

Level 3: Adding and removing fields

programming requirements

According to the prompt, supplement the code in the editor on the right:

  • Add the field Country after the Name field of the data table tb_emp , and the data format is varchar(20) ;

  • Delete the field Salary in the data table tb_emp .

The data table structure is as follows:

USE Company;

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

########## add the column ##########
#ALTER TABLE 表 ADD 字段 数据类型 约束条件 [FIRSTIAFTER] 已存在字段名
ALTER TABLE tb_emp ADD Country varchar(20) AFTER Name;
 
########## delete the column ##########
ALTER TABLE tb_emp DROP Salary;


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

DESCRIBE tb_emp;

Level 4: Modify the arrangement position of the fields

programming requirements

According to the prompt, supplement the code in the editor on the right:

  • Move the Name field of the data table tb_emp to the first column, and the data format remains unchanged;

  • DeptId字段移至Salary字段的后边,数据格式不变。

数据表结构如下:

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 AFTER Salary;


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

DESCRIBE tb_emp;

第5关:删除表的外键约束

编程要求

我们已经为你建立了主表tb_dept和子表tb_emp,在表tb_emp上添加了名称为emp_dept的外键约束,外键名称为DeptId,依赖于表tb_dept的主键Id,下面那是两张表的结构展示:

请你根据提示,在右侧编辑器Begin-End中补充代码:

  • 删除数据表tb_emp的外键约束emp_dept

USE Company;

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

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

ALTER TABLE tb_emp DROP FOREIGN KEY emp_dept;


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

Guess you like

Origin blog.csdn.net/kercii/article/details/129725246