第1關(guān):查看表結(jié)構(gòu)與修改表名
編程要求
根據(jù)提示,在右側(cè)編輯器補(bǔ)充代碼:
把數(shù)據(jù)表tb_emp改名為jd_emp;
查看該數(shù)據(jù)庫下數(shù)據(jù)表的列表;
查看數(shù)據(jù)表jd_emp的基本結(jié)構(gòu)。
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## 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 ##########
第2關(guān):修改字段名與字段數(shù)據(jù)類型
編程要求
根據(jù)提示,在右側(cè)編輯器補(bǔ)充代碼:
把數(shù)據(jù)表tb_emp的字段Id改名為prod_id,數(shù)據(jù)類型不變;
把數(shù)據(jù)表tb_emp字段Name的數(shù)據(jù)類型改為varchar(30)。
數(shù)據(jù)表結(jié)構(gòu)如下:

USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## 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;
第3關(guān):添加與刪除字段
編程要求
根據(jù)提示,在右側(cè)編輯器補(bǔ)充代碼:
在數(shù)據(jù)表tb_emp的Name字段后添加字段Country,數(shù)據(jù)格式為varchar(20);
刪除數(shù)據(jù)表tb_emp中的字段Salary。
數(shù)據(jù)表結(jié)構(gòu)如下:

USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## add the column ##########
#ALTER TABLE 表 ADD 字段 數(shù)據(jù)類型 約束條件 [FIRSTIAFTER] 已存在字段名
ALTER TABLE tb_emp ADD Country varchar(20) AFTER Name;
########## delete the column ##########
ALTER TABLE tb_emp DROP Salary;
########## End ##########
DESCRIBE tb_emp;
第4關(guān):修改字段的排列位置
編程要求
根據(jù)提示,在右側(cè)編輯器補(bǔ)充代碼:
將數(shù)據(jù)表tb_emp的Name字段移至第一列,數(shù)據(jù)格式不變;
將DeptId字段移至Salary字段的后邊,數(shù)據(jù)格式不變。
數(shù)據(jù)表結(jié)構(gòu)如下:

USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## 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關(guān):刪除表的外鍵約束
編程要求
我們已經(jīng)為你建立了主表tb_dept和子表tb_emp,在表tb_emp上添加了名稱為emp_dept的外鍵約束,外鍵名稱為DeptId,依賴于表tb_dept的主鍵Id,下面那是兩張表的結(jié)構(gòu)展示:


請你根據(jù)提示,在右側(cè)編輯器Begin-End中補(bǔ)充代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-428445.html
刪除數(shù)據(jù)表tb_emp的外鍵約束emp_dept。文章來源地址http://www.zghlxwxcb.cn/news/detail-428445.html
USE Company;
#請?jiān)诖颂幪砑訉?shí)現(xiàn)代碼
########## Begin ##########
########## delete the foreign key ##########
ALTER TABLE tb_emp DROP FOREIGN KEY emp_dept;
########## End ##########
SHOW CREATE TABLE tb_emp \G;
到了這里,關(guān)于頭歌 MySQL數(shù)據(jù)庫 - 數(shù)據(jù)庫和表的基本操作(一)答案的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!