diff --git "a/48 \351\251\254\345\256\217\350\276\276/20230223\344\270\223\344\270\232\350\257\276\344\275\234\344\270\232.md" "b/48 \351\251\254\345\256\217\350\276\276/20230223\344\270\223\344\270\232\350\257\276\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..6934d5a0f14d9acf8d443ecb33263151132f8962 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/20230223\344\270\223\344\270\232\350\257\276\344\275\234\344\270\232.md" @@ -0,0 +1,295 @@ +# 笔记 + +非空约束:not null + +默认约束:default 默认值 + +唯一约束:unique key,(值可以为空,但值不能重复) + +主键约束:primary key(默认非空,一个表只能有一个主键约束) + +复合主键primary key(字段名1,字段名2) 表示字段1和字段2的组合是唯一的 + +自增约束(和主键一起使用):auto_increment(不给值或者给0或NULL,会在列表最大值的基础上自增) + +删除唯一约束:alter table 表名称 drop index 索引名 + +删除主键约束:alter table 表名称 drop primary key; + +删除自增约束:alter table 表名称 modify 字段名 数据类型; + +添加自增约束:alter table 表名称 modify 字段名 数据类型 auto_increment; + +外键约束:foreign key + +例:create table 主表名称( + +字段1 数据类型 primary key, + +字段2 数据类型 + +); + +create table 从表名称( + +字段1 数据类型 primary key, + +字段2 数据类型, + +foreign key(从表字段) feferences 主表名(被参考字段) + +); + +从表字段的数据类型必须和主表名(被参考字段)数据类型一样。 + +-- ## 第1题 +-- + +-- 1、创建数据库test01_company +CREATE DATABASE test01_company +-- 2、创建表格offices +use test01_company; + +| -- | 字段名 | 数据类型 | +| --- | ---------- | ----------- | +| -- | officeCode | int | +| -- | city | varchar(30) | +| -- | address | varchar(50) | +| -- | country | varchar(50) | +| -- | postalCode | varchar(25) | + +CREATE table offices ( + officeCode int, + city VARCHAR(30), + address VARCHAR(30), + country VARCHAR(50), + postalCode varchar(25) +); + +-- 3、创建表格employees +use test01_company; +CREATE table employees ( +-- | 字段名 | 数据类型 | +-- | --------- | ------------- | +-- | 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) | + 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 int after code ; +-- **要求5:**将表employees的birth字段改名为birthday; +alter table employees change bitth bithday date ; +-- **要求6:**修改sex字段,数据类型为char(1)。 +alter table employees modify sex char(1); +-- **要求7:**删除字段note; +alter table drop note; +-- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); +alter table employees add favoriate_activity VARCHAR(100); +-- **要求9:**将表employees的名称修改为 employees_info +alter table employees rename [to] employees_info; +-- ```mysql +-- +-- ``` +-- +-- +-- +-- ## 第2题 +-- +-- 1、创建数据库test02db +CREATE DATABASE test02_db; +-- 2、创建表格pet +use test02_db; +-- | 字段名 | 字段说明 | 数据类型 | +-- | ------- | -------- | ----------- | +-- | name | 宠物名称 | varchar(20) | +-- | owner | 宠物主人 | varchar(20) | +-- | species | 种类 | varchar(20) | +-- | sex | 性别 | char(1) | +-- | birth | 出生日期 | year | +-- | death | 死亡日期 | year | +create table pet( + name VARCHAR(20), + owner varchar(20), + species varchar(20), + sex char(1), + birth year , + death year +) +-- 3、添加记录 +alter database test02_db CHARACTER set utf8; +alter table pet default charset utf8; +-- | 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( +`Fluffy`, `harold` ,`Cat`, `f `, ` 2003` ,`2010` +`Claws`, ` gwen `, `Cat`, ` m ` , `2004` +`Buffy`, ` Dog`, ` f `, | `2009` +`Fang`, `benny `, `Dog`, ` m `, `2000` +`bpswer`, `diane`, `Dog`, `m`, `2003`,`2009` +`chirpy`,`bird` ,`f`, `2008`, +) +-- 4、 添加字段主人的生日owner_birth。 +alter table pet add owner_borth int; +-- 5、 将名称为Claws的猫的主人改为kevin +update pet set `owner`=`kevin` where name=`Claws` +-- 6、 将没有死的狗的主人改为duck +updatepet set `owner`=`duck` where death is null ; +-- 7、 查询没有主人的宠物的名字; +select `owner` death where `owner` is null; +-- 8、 查询已经死了的cat的姓名,主人,以及去世时间; +select name,`owner`,death from pet where species=`Cat`; +-- 9、 删除已经死亡的狗 +delete from pet where species=`dog` and death=`2009`; +-- 10、查询所有宠物信息 +select * from pet; +-- ```sql +-- +-- ``` +-- +-- ## 第3题 +-- +-- 1、创建数据库:test03_company +create database test03_company; +use test03_company; +-- ```sql +-- create database test03_company charset utf8; +-- ``` +-- +-- 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 +-- +-- A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +-- +-- ```mysql +-- use test03_company ; +create table department( + depid int primary key auto_increment, + depname char(10) not null unique key, + deinfo varchar(200) + ) +-- ``` +-- +-- B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +-- +-- * ​ 雇员编号为主键; +-- * ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); +-- * ​ 性别默认为男; +-- +-- ```mysql +create table employee ( + empid int primary key auto_increment, + name varchar(10) not null, + sex enum('男','女') not null default '男', + title varchar(10), + birthday date, + depid int foreign key references department(depid) + ) +-- ``` +-- +-- C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +-- +-- 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) +create table salary( + empid int primary key, + basesalary double, + titlesalary double, + deduction double, + foreign key(empid) referenences employee (empid) + ); +-- 4、添加数据如下: +-- +-- 部门表: +desc departmnet; +show create table department; +alter table department convert to charater set utf8; +insert into department values +-- | 部门编号 | 部门名称 | 部门简介 | +-- | -------- | -------- | ------------ | +( 111, ` 生产部 ` , Null ) +( 222 , ,`销售部 ` , Null ) +( 333,`人事部 ` ,人力资源管理 ) +-- +-- 雇员表: +insert into employee values +-- | 雇员编号 | 姓名 | 性别 | 职称 | 出生日期 | 所在部门编号 | +-- | -------- | ---- | ---- | ---------- | ---------- | ------------ | +(1001 , '张三' , '男' , '高级工程师' ,' 1975-1-1' , '111' ); +(1002 ,'李四' , '女' , '助工' , '1985-1-1' , '111' ); + (1003 ,'王五' , '男' , '工程师' , '1978-11-11' , '222' ); + (1004 ,'张六', '男', '工程师' , '1999-1-1' ,'222'); +-- +-- 工资表: +insert into salary values +-- | 雇员编号 | 基本工资 | 职务工资 | 扣除 | +-- | -------- | -------- | -------- | ---- | +(1001 , 2200 , 1100 , 200 ); +( 1002 , 1200 , 200 , NULL); +( 1003 , 2900 , 700 , 200) ; + (1004, 1950, 700 , 150); +-- +-- +-- +-- ## 第4题 +-- +-- 1、创建一个数据库:test04_school +-- +-- 2、创建如下表格 +-- +-- 表1 Department表的定义 +-- +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | +-- | DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | +-- | DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | +-- +-- 表2 Teacher表的定义 +-- +-- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | +-- | ---------- | ------------ | ------------ | -------- | -------- | -------- | -------- | +-- | Number | 教工号 | int | 是 | 否 | 是 | 是 | +-- | Name | 姓名 | varchar(30) | 否 | 否 | 是 | 否 | +-- | Sex | 性别 | varchar(4) | 否 | 否 | 否 | 否 | +-- | Birth | 出生日期 | date | 否 | 否 | 否 | 否 | +-- | DepNo | 部门号 | int | 否 | 是 | 否 | 否 | +-- | Salary | 工资 | float | 否 | 否 | 否 | 否 | +-- | Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | +-- +-- 3、添加记录 +-- +-- | **DepNo** | **DepName** | **DepNote** | +-- | --------- | ----------- | ------------------ | +-- | 601 | 软件技术系 | 软件技术等专业 | +-- | 602 | 网络技术系 | 多媒体技术等专业 | +-- | 603 | 艺术设计系 | 广告艺术设计等专业 | +-- | 604 | 管理工程系 | 连锁经营管理等专业 | +-- +-- | **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | +-- | ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | +-- | 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | +-- | 2002 | Lucy | 男 | 1983-12-18 | 601 | 2500 | 北京市昌平区 | +-- | 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | +-- | 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | +-- | 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | +-- +-- -- 4、用SELECT语句查询Teacher表的所有记录。 diff --git "a/48 \351\251\254\345\256\217\350\276\276/20230302.md" "b/48 \351\251\254\345\256\217\350\276\276/20230302.md" new file mode 100644 index 0000000000000000000000000000000000000000..7e7db49c1d5c9006e386d248672588f48914fd19 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/20230302.md" @@ -0,0 +1,89 @@ +### 笔记 + +Avg()平均值 Sum()求和 Max()最大值 + +Min()最小值 count()统计记录器 + +count(_)统计全部,count_(字段)统计非null的值 + +关联查询 + +内连接:a inner join b on a.字段=b.字段 ab相同数据 + +左全连接:a left join b on a.字段=b.字段 where a.字段 is null 不重复的a数据 + +右全连接:a right join b on a.字段=b.字段 where b.字段 is null 不重复的b数据 + +左连接:a left join b on a.字段=b.字段 全a数据 + +右连接:a right join b on a.字段=b.字段 全b数据 + +# 作业 + +--创建添加数据顺序 1,4,2,3 + +create database glcx charset utf8; +use glcx; +create table Student( +Sno varchar (20) not null primary key, +Sname varchar (20) not null, +Ssex varchar (20) not null, +Sbirthday datetime, +Class varchar (20) +); +create table Course( +Cno varchar (20) not null primary key, +Cname varchar (20) not null, +Tno varchar (20) not null, +foreign key(Tno) references Teacher(Tno) +); +create table Score( +Sno varchar (20) not null, +foreign key(Sno) references Student(Sno), +Cno varchar (20), +foreign key(Cno) references Course(Cno), +Degree Decimal(4,1) +); +create table Teacher( +Tno varchar (20) not null primary key, +Tname varchar (20) not null, +Tsex varchar (20) not null, +Tbirthday datetime, +Prof varchar (20), +Depart varchar (20) not null +); +insert into Student values +(108,'曾华','男','1977-9-1',95033), +(105,'匡明','男','1975-10-2',95031), +(107,'王丽','女','1976-1-23',95033), +(101,'李军','男','1976-2-20',95033), +(109,'王芳','女','1975-2-10',95031), +(103,'陆君','男','1974-6-3',95031); +insert into Course VALUES +('3-105','计算机导论',825), +('3-245','操作系统',804), +('6-166','数字电路',856), +('9-888','高等数学',831); +insert into Score VALUES +(103,'3-245',86), +(105,'3-245',75), +(109,'3-245',68), +(103,'3-105',92), +(105,'3-105',88), +(109,'3-105',76), +(101,'3-105',64), +(107,'3-105',91), +(108,'3-105',78), +(101,'6-166',85), +(107,'6-166',79), +(108,'6-166',81); +insert into Teacher VALUES +(804,'李诚','男','1958-12-2','副教授','计算机系'), +(856,'张旭','男','1969-3-12','讲师','电子工程系'), +(825,'王萍','女','1972-5-5','助教',' 计算机系'), +(831,'刘冰','女','1977-8-14','助教',' 电子工程系'); +-- 1,查询所有学生,都学了哪些课程,要显示学生信息和课程信息/ +表1关联表3,表3关联表2 +select * from Student inner join Score inner join Course on +Student.Sno=Score.Sno and Score.Cno=Course.Cno; + diff --git "a/48 \351\251\254\345\256\217\350\276\276/20230309.md" "b/48 \351\251\254\345\256\217\350\276\276/20230309.md" new file mode 100644 index 0000000000000000000000000000000000000000..c4d29bb82322b1116ec416a5eb9fca8df5449a55 --- /dev/null +++ "b/48 \351\251\254\345\256\217\350\276\276/20230309.md" @@ -0,0 +1,88 @@ +笔记 + +单列单值可以放在select、where、having后面 + +多列多值只能放在where后面,用in、not in进行比较 + +作业 + +create database lianxi charset utf8; +use lianxi; +drop table stuinfo; +drop table stuExam; +create table stuinfo( +stuNO varchar(10) primary key, +stuName varchar(10), +stuSex char, +stuage int(3), +stuAddress varchar(5), +stuSeat int(2) +); +create table stuExam( +examNO int(2) primary key, +stuNO varchar(10), +foreign key(stuNO) references stuinfo(stuNO), +writtenExam int(3), +labExam int(3) +); +create table stuMarks( +examNO int(2), +foreign key(examNO) references stuExam(examNO), +stuID varchar(10), +score int(3) +); +insert into stuinfo VALUES +('s2501','张秋利','男',20,'美国硅谷',1), +('s2502','李斯文','女',18,'湖北武汉',2), +('s2503','马文才','男',18,'湖南长沙',3), +('s2504','欧阳俊雄','女',21,'湖北武汉',4), +('s2505','梅超风','男',16,'湖北武汉',5), +('s2506','陈旋风','男',19,'美国硅谷',6); +insert into stuExam values +(1,'s2501',50,70), +(2,'s2502',60,65), +(3,'s2503',86,70), +(4,'s2504',40,80), +(5,'s2505',70,85), +(6,'s2506',85,90); +insert into stuMarks values +(1,'s2501',88), +(2,'s2501',92), +(3,'s2501',53), +(4,'s2502',60), +(5,'s2502',99), +(6,'s2503',82); +-- 1.查询出年龄比班上平均年龄大的学生的信息 +select * from stuinfo where stuage>(select avg(stuage) from stuinfo); +-- 2.查询出每个学生的学号,姓名,性别和选修课程的最高分(stuMarks) +select a.stuNO,stuName,stuSex,score +from stuinfo a inner join stuMarks c on +a.stuNO=c.stuID +where score in (select max(score) from stuMarks group by stuID); +-- 3.查询出每个学生的学号,姓名,性别和考试平均分(stuExam) +select a.stuNO,a.stuName,a.stuSex,(writtenExam+labExam)/2 +from stuinfo a left join stuExam b ON +a.stuNO=b.stuNO +where (writtenExam+labExam)/2 in (select (writtenExam+labExam)/2 from stuExam group by examNO); +-- 4.查询性别是男并且年龄大于等于20的学生的信息(用两种方法实现:普通查询和子查询) +select * from stuinfo where stuSex='男' and stuage>=20; +select * from stuinfo +where stuSex in (select stuSex from stuinfo where stuSex='男') +and +stuage in (select stuage from stuinfo where stuage>=20); +-- 5.查询出年龄比所有男生年龄都大的女生的信息 +select * from stuinfo where stuage in (select max(stuage) from stuinfo where stuSex='女' group by stuSex); +-- 6.查询出所有选修课程都及格的学生的信息 (stuMarks) +select * from stuinfo a inner join stuExam b on +a.stuNO=b.stuNO +where b.writtenExam in (select b.writtenExam from stuExam where b.writtenExam>=60) +AND +b.labExam in (select b.labExam from stuExam where b.labExam>=60); +-- 7.查询出参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a inner join stuExam b inner join stuMarks c ON +a.stuNO=b.stuNO and b.examNO=c.examNO +where a.stuNO in (select stuID from stuMarks group by stuID); +-- 8.查询出没有参加考试的学生的信息(用表连接,in二种方法做)(stuMarks) +select a.* from stuinfo a inner join stuExam b inner join stuMarks c ON +a.stuNO=b.stuNO and b.examNO=c.examNO +where a.stuNO not in (select stuID from stuMarks group by stuID);