From 8661c26aaf5c322218413124dc4c5243cec88705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E6=99=BA=E7=A0=94?= <3058944672@qq.com> Date: Tue, 21 Feb 2023 23:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...46\254\241\344\275\234\344\270\232.md.txt" | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 "46 \350\203\241\346\231\272\347\240\224/20230221\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md.txt" diff --git "a/46 \350\203\241\346\231\272\347\240\224/20230221\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md.txt" "b/46 \350\203\241\346\231\272\347\240\224/20230221\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md.txt" new file mode 100644 index 0000000..334ebe1 --- /dev/null +++ "b/46 \350\203\241\346\231\272\347\240\224/20230221\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md.txt" @@ -0,0 +1,195 @@ +## 第1题 + +-- 1、创建数据库test01_company +create database test01_company; +use test01_company; +-- 2、创建表格offices +-- +-- | 字段名 | 数据类型 | +-- | ---------- | ----------- | +-- | officeCode | int | +-- | city | varchar(30) | +-- | address | varchar(50) | +-- | country | varchar(50) | +-- | postalCode | varchar(25) | +create table offices( +officeCode int, +city varchar(30), +address varchar(50), +country varchar(50), +postalCode varchar(25) +); +-- 3、创建表格employees +create table employees( +empNum int, +lastName varchar(50), +firstName varchar(50), +mobile varchar(25), +code int, +jioTitle varchar(50), +birth date, +Note varchar(255), +Sex varchar(5) +); +-- | 字段名 | 数据类型 | +-- | --------- | ------------- | +-- | empNum | int(11) | +-- | lastName | varchar(50) | +-- | firstName | varchar(50) | +-- | mobile | varchar(25) | +-- | code | int | +-- | jobTitle | varchar(50) | +-- | birth | date | +-- | Note | varchar(255) | +-- | Sex | varchar(5) | +-- +-- **要求4:**将表employees的mobile字段修改到code字段后面。 +alter table employees modify mobile varchar(25) after code; +-- **要求5:**将表employees的birth字段改名为birthday; +alter table employees change birth birthday date; +-- **要求6:**修改sex字段,数据类型为char(1)。 +alter table employees modify Sex char(1); +-- **要求7:**删除字段note; +alter table employees drop Note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity varchar(100); +-- **要求9:**将表employees的名称修改为 employees_info +alter table employees rename employees_info; +``` + +### 第二题 + +```mysql +-- ## 第2题 +-- +-- 1、创建数据库test02db +create database test02db; +use test02db; +-- 2、创建表格pet +create table pet( +name varchar(20), +owner varchar(20), +species varchar(20), +sex char(1), +birth year, +death year +); +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +-- +-- 3、添加记录 +-- +-- | name | owner | species | sex | birth | death | +-- | ------ | ------ | ------- | ---- | ----- | ----- | +-- | Fluffy | harold | Cat | f | 2003 | 2010 | +-- | Claws | gwen | Cat | m | 2004 | | +-- | Buffy | | Dog | f | 2009 | | +-- | Fang | benny | Dog | m | 2000 | | +-- | bowser | diane | Dog | m | 2003 | 2009 | +-- | Chirpy | | Bird | f | 2008 | | +insert into pet values('Fliffy','harold','Cat','f',2003,2010), +('Claws','gwen','Cat','m',2004,null), +('Buffy',null,'Dog','f',2009,null), +('Fang','benny','Dog','m',2000,null), +('bowser','diane','Dog','m',2003,2009), +('Chirpy',null,'Brid','f',2008,null); +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_birth year; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set owner='kevin' where name='Claws'; +-- 6、 将没有死的狗的主人改为duck +update pet set owner='duck' where species='Dog' and death is null; +-- 7、 查询没有主人的宠物的名字; +select name from pet where owner is null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,owner,death from pet where species='cat' and death is not null; +-- 9、 删除已经死亡的狗 +delete from pet where death is not null and species='dog'; +-- 10、查询所有宠物信息 +select * from pet; +``` + +### 第三题 + +```mysql +# ## 第3题 +# +# 1、创建数据库:test03_company +create database test03_company; +use test03_company; +# 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +# +# A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +create table department( + depid int primary key not null , + depname varchar(10) not null , + deinfo varchar(50) +); +# B. 雇员表(emoloyee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +create table emoloyee( + empid int primary key not null , + name varchar(10), + sex enum('男','女') default '男' not null , + title varchar(10), + birthday date, + depid int, + foreign key(depid) references department(depid) +); +# * ​ 雇员编号为主键; +# * ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +# * ​ 性别默认为男; +# +# C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +create table salary( + empid int primary key , + basesalary int, + titlesalary int, + deduction int, + foreign key (empid) references emoloyee(empid) +); +# 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) + +# 4、添加数据如下: +# +# 部门表: +# +# | 部门编号 | 部门名称 | 部门简介 | +# | -------- | -------- | ------------ | +# | 111 | 生产部 | Null | +# | 222 | 销售部 | Null | +# | 333 | 人事部 | 人力资源管理 | +insert into department values (111,'生产部',null), + (222,'销售部',null), + (333,'人事部','人事资源管理'); +# 雇员表: +# +# | 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +# | -------- | ---- | ---- | ---------- | ---------- | ------------ | +# | 1001 | 张三 | 男 | 高级工程师 | 1975-1-1 | 111 | +# | 1002 | 李四 | 女 | 助工 | 1985-1-1 | 111 | +# | 1003 | 王五 | 男 | 工程师 | 1978-11-11 | 222 | +# | 1004 | 张六 | 男 | 工程师 | 1999-1-1 | 222 | +insert into emoloyee values (1001,'张三','男','高级工程师','1975-1-1',111), + (1002,'李四','女','助工','1985-1-1',111), + (1003,'王五','男','工程师','1978-11-11',222), + (1004,'张六','男','工程师','1999-1-1',222); +# 工资表: +# +# | 雇员编号 | 基本工资 | 职务工资 | 扣除 | +# | -------- | -------- | -------- | ---- | +# | 1001 | 2200 | 1100 | 200 | +# | 1002 | 1200 | 200 | NULL | +# | 1003 | 2900 | 700 | 200 | +# | 1004 | 1950 | 700 | 150 | +insert into salary values (1001,2200,1100,200), + (1002,1200,200,null), + (1003,2900,700,200), + (1004,1950,700,150); + +``` -- Gitee