diff --git "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye1.sql" "b/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye1.sql" deleted file mode 100644 index bfe4a8948bb80d08edd885a5020d9d76fac6f0ff..0000000000000000000000000000000000000000 --- "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye1.sql" +++ /dev/null @@ -1,38 +0,0 @@ -create database test01_company; - -use test01_company; - -create table offices( -officeCode int, -city varchar(30), -address varchar(50), -country varchar(50), -postalCode varchar(25) -); - -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) -); - -alter table employees modify mobile varchar(25) after code; - -alter table employees change birth birthday date; - -alter table employees modify sex char(1); - -alter table employees drop note; - -alter table employees add favoriate_activity varchar(100); - -alter table employees rename to employees_info - -desc employees; - diff --git "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye2.sql" "b/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye2.sql" deleted file mode 100644 index acef5c33c766441ee13740b60b1b8f30c3c530ab..0000000000000000000000000000000000000000 --- "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye2.sql" +++ /dev/null @@ -1,42 +0,0 @@ -create database test02db; - -use test02db; - -create table pet( -name varchar(20) comment '宠物名称', -owner varchar(20) comment '宠物主人', -species varchar(20) comment '种类', -sex char(1) comment '性别', -birth year comment '出生日期', -death YEAR comment '死亡日期' -); - -insert into pet values('Fluffy','harold','Cat','f',2003,2010); - -select * from pet; - -insert into pet (name,owner,species,sex,birth) values ('Claws','gwen','Cat','m',2004); - -insert into pet (name,species,sex,birth) values ('Buffy','Dog','f',2009); - -insert into pet (name,owner,species,sex,birth) values ('Fang','benny','Dog','m',2000); - -insert into pet (name,owner,species,sex,birth,death) values('bowser','diane','Dog','m',2003,2009); - -insert into pet (name,species,sex,birth) values ('Chirpy','Bird','f',2008); - -select * from pet; - -alter table pet add owner_birth year comment '主人生日'; - -update pet set owner='kevin' where name='Claws'; - -update pet set owner='duck' where death is null and species ='Dog'; - -select name from pet where isnull(owner)=1; - -select name,owner,death from pet where ISNULL(death)=0 and species='Cat'; - -delete from pet where ISNULL(death)=0 and species='Dog'; - -select * from pet; \ No newline at end of file diff --git "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye3.sql" "b/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye3.sql" deleted file mode 100644 index c47fef153a4674da47d688bebf3902b43a1e3dc8..0000000000000000000000000000000000000000 --- "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye3.sql" +++ /dev/null @@ -1,48 +0,0 @@ -create database test03_company charset utf8; - -use test03_company ; - -create table department( - depid int primary key auto_increment, - depname char(10) not null unique key, - deinfo varchar(200) -); - -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 (depid) references department(depid) -); - -create table salary( -empid int primary key, -basesalary int, -titlesalary int, -deduction int -); - -insert into department (depid,depname) values (111,'生产部'); - -insert into department (depid,depname) values (222,'销售部'); - -insert into department (depid,depname,deinfo) values (333,'人事部','人力资源管理'); - -insert into employee values -(1001,'张三','男','高级工程师','1975-1-1',111); - - -insert into employee values -(1002,'李四','女','助工','1985-1-1',111), -(1003,'王五','男','工程师','1978-11-11',222), -(1004,'张六','男','工程师','1999-1-1',222); - -insert into salary values (1001,2200,1100,200); - -insert into salary (empid,basesalary,titlesalary) values (1002,1200,200); - -insert into salary values (1003,2900,700,200); -insert into salary values (1004,1950,700,150); \ No newline at end of file diff --git "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye4.sql" "b/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye4.sql" deleted file mode 100644 index 4a2c048681fa44618f22d4cce7f85816e7e3ca36..0000000000000000000000000000000000000000 --- "a/11 \351\273\216\344\277\212\346\235\250/20230221\347\272\246\346\235\237\344\275\234\344\270\232/zuoye4.sql" +++ /dev/null @@ -1,37 +0,0 @@ -create database test04_school charset utf8; - -show databases; - -use test04_school; - -create table Department( -DepNo int(10) PRIMARY KEY not null comment '部门号', -DepName varchar(20) not null comment '部门名称', -DepNote varchar(50) -); - -create table Teacher( -Number int primary key not null comment '教工号', -Name varchar(30) not null comment '姓名', -Sex varchar(4) comment '姓名', -Birth date comment '出生日期', -DepNo int comment '部门号', -Salary float comment '工资', -Address varchar(100) comment '家庭住址', -FOREIGN KEY (DepNo) REFERENCES Department(DepNo) -); - -insert into Department values -(601,'软件技术系','软件技术等专业'), -(602,'网络技术系','多媒体技术等专业'), -(603,'艺术设计系','广告艺术设计等专业'), -(604,'管理工程系','连锁经营管理等专业'); - -insert into Teacher VALUES -(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,'重庆市南岸区'); - -select * from Teacher; \ No newline at end of file diff --git "a/13 \346\264\252\351\224\246\346\264\213/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/13 \346\264\252\351\224\246\346\264\213/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 182eb6e4488c227f3b7b16164fbbed0e3a4643fc..0000000000000000000000000000000000000000 --- "a/13 \346\264\252\351\224\246\346\264\213/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,196 +0,0 @@ --- 1、创建数据库test01_market -create database test01_market; --- 2、创建表格customers -use test01_market; --- | 字段名 | 数据类型 | --- | --------- | ----------- | --- | c_num | int(11) | --- | c_name | varchar(50) | --- | c_contact | varchar(50) | --- | c_city | varchar(50) | --- | c_birth | date | -create table customers( - c_num int, - c_name varchar(50), - c_contact varchar(50), - c_city varchar(50), - c_birth date -); --- **要求3:**将c_contact字段移动到c_birth字段后面 -alter table customers modify c_contact varchar(50) after c_birth; -desc customers; --- **要求4:**将c_name字段数据类型改为 varchar(70) -alter table customers modify c_name varchar(70); -desc customers; --- **要求5:**将c_contact字段改名为c_phone -alter table customers change c_contact c_phone varchar(50); -desc customers; --- **要求6:**增加c_gender字段到c_name后面,数据类型为char(1) -alter table customers add c_gender char(1); -alter table cusromers modify column char(1) after c_name; --- **要求7:**将表名改为customers_info -alter table custo --- **要求8:**删除字段c_city --- ## 第2题 -select * from books; --- 1、创建数据库test02_library -create database test02_library; --- 2、创建表格books -use test02_library; -select * from books; -truncate books; --- | 字段名 | 字段说明 | 数据类型 | 允许为空 | 唯一 | --- | ------- | -------- | ------------- | -------- | ---- | --- | b_id | 书编号 | int(11) | 否 | 是 | --- | b_name | 书名 | varchar(50) | 否 | 否 | --- | authors | 作者 | varchar(100) | 否 | 否 | --- | price | 价格 | float | 否 | 否 | --- | pubdate | 出版日期 | year | 否 | 否 | --- | note | 说明 | varchar(100) | 是 | 否 | --- | num | 库存 | int(11) | 否 | 否 | -create table books( - b_id int(11), - b_name varchar(50), - authorss varchar(50), - price float, - pubdate year, - note varchar(100), - num int(11) -); --- 3、向books表中插入记录 --- 1) 指定所有字段名称插入第一条记录 -insert into books(b_id,b_name,authorss,price,pubdate,note,num)values(1,'Tal of AAA','Dickes',23,1995,'novel',11); --- 2)不指定字段名称插入第二记录 -insert into books values(2,'EmmaT','Jane',35,1993,'joke',22); --- 3)同时插入多条记录(剩下的所有记录) - --- | b_id | b_name | authors | price | pubdate | note | num | --- | ---- | ------------- | --------------- | ----- | ------- | -------- | ---- | --- | 1 | Tal of AAA | Dickes | 23 | 1995 | novel | 11 | --- | 2 | EmmaT | Jane lura | 35 | 1993 | joke | 22 | --- | 3 | Story of Jane | Jane Tim | 40 | 2001 | novel | 0 | --- | 4 | Lovey Day | George Byron | 20 | 2005 | novel | 30 | --- | 5 | Old land | Honore Blade | 30 | 2010 | law | 0 | --- | 6 | The Battle | Upton Sara | 30 | 1999 | medicine | 40 | --- | 7 | Rose Hood | Richard haggard | 28 | 2008 | cartoon | 28 | -insert into books values(3,'Story of Jane','Jane Tim',40,2001,'novel',0); -insert into books values(5,'Lovey Day','George Byron',20,2005,'novel',30); -insert into books values(5,'Old land','Honore Blade',30,2010,'law',0), - (6,'The Battle','Upton Sara',30,1999,'medicine',40), - (7,'Rose Hood','Richard haggard',28,2008,'cartoon',28); -select * from books; - --- 4、将小说类型(novel)的书的价格都增加5。 -update books set price=price + 5 where note='novel'; --- 5、将名称为EmmaT的书的价格改为40。 -update books set price = 40 where b_name='EmmaT'; --- 6、删除库存为0的记录 -delete from books where num=0; --- ```mysql --- --- ``` --- ## 第3题 --- --- 1、创建数据库test03_bookstore -create database tesr03_bookstore charset utf8; -use test03_bookstore; --- 2、创建book表 -create table book( - id int(11)not null, - title varchar(100)not null, - author varchar(100)not null, - price double(11,2)not null, - sales int(11)not null, - img_path varchar(100)not null -); --- ```mysql --- +----------+--------------+------+-----+---------+----------------+ --- | Field | Type | Null | Key | Default | Extra | --- +----------+--------------+------+-----+---------+----------------+ --- | id | int(11) | NO | PRI | NULL | auto_increment | --- | title | varchar(100) | NO | | NULL | | --- | author | varchar(100) | NO | | NULL | | --- | price | double(11,2) | NO | | NULL | | --- | sales | int(11) | NO | | NULL | | --- | stock | int(11) | NO | | NULL | | --- | img_path | varchar(100) | NO | | NULL | | --- +----------+--------------+------+-----+---------+----------------+ --- ``` --- --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+-------------+------------+-------+-------+-------+----------------------------+ --- | id | title | author | price | sales | stock | img_path | --- +----+-------------+------------+-------+-------+-------+-----------------------------+ -insert into book values(1,'解忧杂货铺','东野圭吾',27.20,102,98,'upload/books/解忧杂货铺.jpg'), - (2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); - --- | 1 | 解忧杂货店 | 东野圭吾 | 27.20 | 102 | 98 | upload/books/解忧杂货店.jpg | --- | 2 | 边城 | 沈从文 | 23.00 | 102 | 98 | upload/books/边城.jpg | --- +----+---------------+------------+-------+-------+-------+----------------------------+ --- ``` - --- 3、创建用户表users,并插入数据 - --- ```mysql --- +----------+--------------+------+-----+---------+----------------+ --- | Field | Type | Null | Key | Default | Extra | --- +----------+--------------+------+-----+---------+----------------+ --- | id | int(11) | NO | PRI | NULL | auto_increment | --- | username | varchar(100) | NO | UNI | NULL | | --- | password | varchar(100) | NO | | NULL | | --- | email | varchar(100) | YES | | NULL | | --- +----------+--------------+------+-----+---------+----------------+ --- ``` --- --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+----------+----------------------------------+--------------------+ --- | id | username | password | email | --- +----+----------+----------------------------------+--------------------+ --- | 1 | admin | 112233 | admin@mxdx.com | --- +----+----------+----------------------------------+--------------------+ --- ``` --- --- 4、创建订单表orders -create table orders(id varchar(100)not null prepare key, - order_time datetime not null, - total_amount double(11,2)not null, - state int(11)not null, - user_id int(11)not null, - key(user_id) -); --- ```mysql --- +--------------+--------------+------+-----+---------+-------+ --- | Field | Type | Null | Key | Default | Extra | --- +--------------+--------------+------+-----+---------+-------+ --- | id | varchar(100) | NO | PRI | NULL | | --- | order_time | datetime | NO | | NULL | | --- | total_count | int(11) | NO | | NULL | | --- | total_amount | double(11,2) | NO | | NULL | | --- | state | int(11) | NO | | NULL | | --- | user_id | int(11) | NO | MUL | NULL | | --- +--------------+--------------+------+-----+---------+-------+ --- ``` --- - --- 尝试添加部分模拟数据,参考示例如下: --- --- ```mysql --- +----+-------+--------+---------+---------+-------+----------------+----------------+ --- | id |count| amount| title | author | price | img_path | order_id | --- +----+-------+--------+------------+----------+-------+----------------+----------------+ --- | 1 | 1 | 27.20| 解忧杂货店 | 东野圭吾 | 27.20 | static/img/default.jpg|15294258455691 | --- | 2 | 1 | 23.00| 边城 | 沈从文 | 23.00 | static/img/default.jpg|15294258455691 | --- +----+-------+--------+------------+----------+-------+------------+----------------+ --- ``` -insert into book values(1,'解忧杂货铺','东野圭吾',27.20,102,98,'upload/books/解忧杂货铺.jpg'), - (2,'边城','沈从文',23.00,102,98,'upload/books/边城.jpg'); --- 参考答案: --- --- ```mysql --- --- ``` --- \ No newline at end of file diff --git "a/52 \347\216\213\345\217\221\346\247\220/2023.02.22sclect\345\222\214\350\277\220\347\256\227\347\254\246.md" "b/52 \347\216\213\345\217\221\346\247\220/2023.02.22sclect\345\222\214\350\277\220\347\256\227\347\254\246.md" new file mode 100644 index 0000000000000000000000000000000000000000..9124c0702099e312d8d06c65846f0c9d4dc7a7d0 --- /dev/null +++ "b/52 \347\216\213\345\217\221\346\247\220/2023.02.22sclect\345\222\214\350\277\220\347\256\227\347\254\246.md" @@ -0,0 +1,90 @@ +作业 + +```sql +第一题 + +create database x charset utf8; +use x; +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT + ); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); + + +-- 要求1:**查询出薪资在12000~13000之间的员工信息。 + select * from employee where salary between 12000 and 13000; +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 + select id,name,addr from employee where name like '刘%'; +-- **要求3:**将“李四”的家庭住址改为“广东韶关” + update employee set addr='广东韶关' where name ='李四'; +-- **要求4:**查询出名字中带“小”的员工 + select * from employee where name like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 + select * from employee where salary >11000 and sex='男'; +-- **要求6:**查询没有登记电话号码的员工 + select * from employee where tel is null; +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 + select * from employee where (salary >12000) or sex='男' and addr in ('广东深圳','广州'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name,salary from employee; +第二题 + +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); + +select * from countries_info; + +-- 要求1:** 查询大国 的国家名称、人口和面积。 + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +-- - 面积至少为 300万平方公里(即,3000000 km2) + +-- - 人口至少为 2500 万(即 25000000) + +select name,population,area from countries_info where area>3000000 or population >25000000; +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select * from countries_info where continent='Asia'; +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where population<100000 and area <10000; +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where name like '%o%'; +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp >10000000000; +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select name,population,gdp,gdp/population '人均贡献GDP值' from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select *,gdp/population '人均贡献GDP值' from countries_info where gdp/population<1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name,area,population,area/population '人均国土面积值' from countries_info; +``` + +笔记 + +``` + +``` diff --git "a/29 \346\262\210\346\231\223\347\220\263/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" "b/52 \347\216\213\345\217\221\346\247\220/2023.02\343\200\20221.md" similarity index 46% rename from "29 \346\262\210\346\231\223\347\220\263/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" rename to "52 \347\216\213\345\217\221\346\247\220/2023.02\343\200\20221.md" index c755b6e534feafe8eb10d8b3568a2968e07af9d0..2f3955e8867aa08c1c0690967e433e1ff40ef9e3 100644 --- "a/29 \346\262\210\346\231\223\347\220\263/20230220\347\272\246\346\235\237\347\232\204\345\255\246\344\271\240\345\222\214\344\275\234\344\270\232.md" +++ "b/52 \347\216\213\345\217\221\346\247\220/2023.02\343\200\20221.md" @@ -1,186 +1,61 @@ -# 一.**笔记** +笔记 +学习 +主键,外键,默认值,自增,唯一约束,非空 -约束 : 主键、唯一、非空、默认、自增、外键 +主键:primary key (非空 唯一) -1、主键约束 -(1)关键字是primary key -(2)特点 -A:每一个表只能有一个主键约束 -B:主键约束的字段值是不允许为null -C: 也不允许重复的 - -#解决方案一,单独使用一个id列当主键 - -```sql -drop table xuanke; -``` - -#解决方案二:复合主键 - -```sql -CREATE TABLE xuanke( - sid INT , - cid INT, - score INT, - PRIMARY KEY(sid,cid) #它俩的组合不能为NULL,并且唯一 +cr ta 表格名( + 字段 类型 primary key + 或者 primary key(字段1,字段2) + 或primary key(字段) ); -``` - -#建表后增加主键约束 - -```sql -ALTER TABLE 表名称 ADD PRIMARY KEY(字段列表); -``` - -#(字段列表)如果是一个字段,就是一个字段是主键 -#(字段列表)如果是多个字段,就是复合主键 - -#如果想要删除主键约束 - -```sql -ALTER TABLE 表名称 DROP PRIMARY KEY; - -ALTER TABLE student DROP PRIMARY KEY; -``` - -2、唯一键约束: -(1)关键字:unique key -(2)特点 -A:允许为null -B: 不能重复 -B:一个表可以有多个唯一键约束 - -#建表后增加唯一键约束 - -```sql -ALTER TABLE 表名称 ADD UNIQUE KEY (字段列表); - -ALTER TABLE student ADD UNIQUE KEY(sname); -``` - -#如果要用语句删除唯一键约束 - -```sql -ALTER TABLE 表名称 DROP INDEX 索引名; -``` - -3、非空约束 - -```sql -not null -``` - -4、默认值约束 -如果某个字段,在添加数据时未指定值时,希望不要用NULL处理,而是按照一个默认值处理, -就可以使用默认值约束。 - -```sql -DEFAULT -``` - -#删除score的非空约束 - -```sql - ALTER TABLE student MODIFY score INT; -``` - - #增加非空约束 - -```sql - ALTER TABLE student MODIFY score INT NOT NULL; -``` - - #删除gender的默认值约束,保留非空约束 - -```sql -ALTER TABLE student MODIFY gender ENUM('男','女') NOT NULL; -``` - - #删除gender的非空约束,保留默认值约束 - -```sql -ALTER TABLE student MODIFY gender ENUM('男','女') DEFAULT '男'; -``` - - #保留非空和默认值约束 - -```sql -ALTER TABLE student MODIFY gender ENUM('男','女') DEFAULT '男' NOT NULL; -``` - -查看一个表的约束名等信息,查看系统库“information_schema”的“table_constraints” - -```sql -SELECT * FROM information_schema.table_constraints WHERE table_name = '表名称'; - -SELECT * FROM information_schema.table_constraints WHERE table_name= 'student'; -``` - -5、检查约束 (MySQL5.7不支持CHECK约束,在MySQL8.0中开始支持CHECK约束) - -6、外键约束 -约束两个表的关系,或者是一个表的两个字段之间的关系。 - -(1)主表(父表)和从表(子表) -主表:被依赖,被参考 -从表:依赖别人的,参考别人的 - -例如:学生表、课程表、选课表 -选课表中的学号,它的值是依赖于学生表的学号。 -选课表是从表, -学生表是主表。 -选课表中的课程编号,它的值是依赖于课程表的课程编号。 -选课表是从表, -课程表示主表。 - -(2)外键的建立和指定要在从表中定义 -(3)关键字 foreign key -(4)格式 - -```sql -foreign key (从表的字段) references 主表(被依赖字段【主键】) -``` - -(5)要求 -A:主表必须先存在 -B:主表被依赖字段必须有索引,所以通常是主键列或唯一键列等才能作为被依赖字段。 -C:删除表时,要么先删除从表,再删除主表,要么先解除外键约束,然后各自删除。 -D:从(子)表的依赖字段的数据,必须在主(父)表被依赖字段中选择。 - 添加和修改都是这样的。 -E:主(父)表被依赖字段的数据不能轻易的删除和修改,要看是否被引用 - -```sql -外键约束等级为 on update/delete cascade /(set null) -``` - -7、自增属性 -维护某个字段的值自动增长的一个属性。 -它的要求: -(1)一个表最多只能有一个字段是自增的 -(2)自增的字段必须是整数类型 -(3)自增的字段必须是键约束的字段(通常是主键、唯一键) - -自增字段赋值为null和0的时候,会自增, -如果赋值为其他的值,按照你指定的值来。 +#建表后增加主键: +alter table 表名 add primary key(字段 (可以多个)) +#删除: +alter table 表名 drop primary key; +外键:foreign key + +cr ta 表格名( + 字段 类型, + foreign key (字段) references 主表(字段); (no update/delete x)外键等级 + +); +默认值:default -```sql -ALTER TABLE 表名称 AUTO_INCREMENT = 起始值; -``` +cr ta 表格名( + 字段 类型 default 默认值 +); +#默认值:一般不和唯一约束和主键一起加; +#建表后增加默认值: +alter table 表名 modify default 默认值 (not null); 看有没有加 +#删除 +alter table 表名 modify 字段名 数据类型 (not null); 看有没有加 +自增:auto_increment +唯一约束:unique key -# 二.作业 +非空:not null +cr ta 表格名( + 字段 类型 not null +); +2.作业 第一题 -```sql - -- 1、创建数据库test01_company --- -CREATE DATABASE test01_company CHARSET utf8; -use test01_company; +create database test01_company charset utf8; -- 2、创建表格offices +use test01_company; +create table offices( + officeCode int, + city varchar(30), + address varchar(50), + country varchar(50), + postalCode varchar(25) +); +-- -- | 字段名 | 数据类型 | -- | ---------- | ----------- | -- | officeCode | int | @@ -188,14 +63,20 @@ use test01_company; -- | 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 -- +-- 3、创建表格employees + create table employees( + empNum int, + lastName varchar(50), + firstName varchar(50), + mobile varchar(50), + code int, + jobTitle varchar(50), + birth date, + Note varchar(255), + Sex varchar(5) + ); + -- | 字段名 | 数据类型 | -- | --------- | ------------- | -- | empNum | int(11) | @@ -207,37 +88,35 @@ postalcode VARCHAR(25)); -- | birth | date | -- | Note | varchar(255) | -- | Sex | varchar(5) | -CREATE table employees( -empNum int, -lastName VARCHAR(50), -firstName VARCHAR(50), -mobile VARCHAR(25), -code int, -jobTitle VARCHAR(50), -birth date, -Note VARCHAR(50), -Sex VARCHAR(5)); +-- -- **要求4:**将表employees的mobile字段修改到code字段后面。 -alter table employees MODIFY mobile VARCHAR(25) after code; + alter table employees modify mobile varchar(25) after code; -- **要求5:**将表employees的birth字段改名为birthday; -alter table employees change birth birthday date; + alter table employees change birth birthady date; -- **要求6:**修改sex字段,数据类型为char(1)。 -alter table employees MODIFY sex char(1); + alter table employees modify sex char(1); -- **要求7:**删除字段note; -alter table employees drop note; + alter table employees drop Note; -- **要求8:**增加字段名favoriate_activity,数据类型为varchar(100); -alter table employees add favoriate_activity varchar(100); + alter table employees add favoriate_activity varchar(100); -- **要求9:**将表employees的名称修改为 employees_info -alter table employees rename to employees_info; -``` + alter table employees rename employees_info; 第二题 -```sql -- 1、创建数据库test02db -CREATE DATABASE test02db; + create database test02db charset utf8; -- 2、创建表格pet -use test02db; + use test02db; + create table pet( + name varchar(20), + owner varchar(20), + species varchar(20), + sex char(1), + birth year, + death year + + ); -- | 字段名 | 字段说明 | 数据类型 | -- | ------- | -------- | ----------- | -- | name | 宠物名称 | varchar(20) | @@ -246,15 +125,9 @@ use test02db; -- | 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、添加记录 -- +-- 3、添加记录 + -- | name | owner | species | sex | birth | death | -- | ------ | ------ | ------- | ---- | ----- | ----- | -- | Fluffy | harold | Cat | f | 2003 | 2010 | @@ -263,99 +136,112 @@ death year); -- | 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,null),('Buffy',null,'Dog','f',2009,null),('Fang','benny','Dog','m',2000,null),('bowser','diane','Dog','m',2003,2009),('Chirpy',null,'Bird','f',2008,null); -SELECT * from pet; +desc pet; +update pet set owner='gwen' where birth='2004'; +insert into pet values('Fluffy','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,'Bird','f','2008',null); -- 4、 添加字段主人的生日owner_birth。 -alter table pet add owner_birth VARCHAR(10); +alter table pet add owner_birth year; -- 5、 将名称为Claws的猫的主人改为kevin -UPDATE pet set owner='kevin' WHERE name='Claws'; -SELECT * from pet; +update pet set name='kevin' where name='Claws'; -- 6、 将没有死的狗的主人改为duck -UPDATE pet set owner='duck' WHERE name='Fang'; -SELECT * from pet; +update pet set owner='duck' where (death is NULL and species='Dog' and owner<>''); -- 7、 查询没有主人的宠物的名字; -SELECT * from pet where isnull(owner); -SELECT owner from pet where isnull; +select name from pet where owner is null; -- 8、 查询已经死了的cat的姓名,主人,以及去世时间; -select name,owner,death from pet where death !='' and species='Cat'; +select name,owner,death from pet where (species='Cat' and death<>''); -- 9、 删除已经死亡的狗 -delete FROM pet where death !=''and species='Dog'; + delete from pet where species='Dog' and death<>''; -- 10、查询所有宠物信息 -SELECT * from pet; -``` - +select * from pet; 第三题 -```sql -- 1、创建数据库:test03_company create database test03_company charset utf8; --- 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。 --- A.部门表(department): --- 部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 -use test03_company ; +-- ```sql +-- create database test03_company charset utf8; +-- ``` +-- +-- 2、在此数据库下创建如下3表,数据类型,宽度,是否为空根据实际情况自己定义。\ +-- A. 部门表(department):部门编号(depid),部门名称(depname),部门简介(deinfo);其中部门编号为主键。 +-- +use test03_company; create table department( -depid int primary key auto_increment, -depname char(10) not null unique key, -deinfo varchar(200) + depid int primary key, + depnmae varchar(10) not null unique key, + deinfo varchar(100) ); --- B.雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 + + +-- B. 雇员表(employee):雇员编号(empid),姓名(name),性别(sex),职称(title),出生日期(birthday),所在部门编号(depid);其中 +-- -- * ​ 雇员编号为主键; -- * ​ 部门编号为外键,外键约束等级为(on update cascade 和on delete set null); -- * ​ 性别默认为男; -create table employee ( -empid int primary key auto_increment, -name varchar(10) not null, -sex enum('男','女') not null default '男', +create table employee( +empid int primary key, +name varchar(5) not null, +sex enum('男','女') not null default '男', title varchar(10), birthday date, depid int, -foreign key (depid) references department(depid) +foreign key (depid) references department(depid) on update cascade on delete set null ); - --- C.工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 - +-- C. 工资表(salary):雇员编号(empid),基本工资(basesalary),职务工资(titlesalary),扣除(deduction)。其中雇员编号为主键。 +create table salary( + empid int primary key, + basesalary int, + titlesalary int, + deduction int + +); -- 3、给工资表(salary)的雇员编号(empid)增加外键约束,外键约束等级为(on update cascade 和on delete cascade) -CREATE table salary( -empid int PRIMARY key auto_increment, -basesalary VARCHAR(50), -titlesalary VARCHAR(50), -deduction VARCHAR(50), -FOREIGN key (empid) REFERENCES employee(empid) -); +alter table salary add foreign key (empid) references employee(empid) on update cascade on delete cascade; +-- select * from information_schema.table_constraints WHERE table_name = 'salary'; +-- alter table salary drop foreign key salary_ibfk_1; -- 4、添加数据如下: +-- -- 部门表: +-- -- | 部门编号 | 部门名称 | 部门简介 | -- | -------- | -------- | ------------ | -- | 111 | 生产部 | Null | -- | 222 | 销售部 | Null | -- | 333 | 人事部 | 人力资源管理 | -insert into department VALUES(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 employee VALUES(1001,'张三','男','高级工程师','1975-1-1',111),(1002,'李四','女','助工','1985-1-1',111),(1003,'王五','男','工程师','1978-11-11',222),(1004,'张六','男','工程师','1999-1-1',222); +insert into employee 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); -``` - -第四题 +insert into salary values(1001,2200,1100,200),(1002,1200,200,null),(1003,2900,700,200),(1004,1950,700,150); -```sql --- 1、创建一个数据库:test04_school -- -CREATE DATABASE test04_school CHARSET utf8; +-- +-- ## 第4题 +-- +-- 1、创建一个数据库:test04_school + create database test04_school charset utf8; + use test04_school; -- 2、创建如下表格 -- -- 表1 Department表的定义 @@ -365,11 +251,11 @@ CREATE DATABASE test04_school CHARSET utf8; -- | DepNo | 部门号 | int(10) | 是 | 否 | 是 | 是 | -- | DepName | 部门名称 | varchar(20) | 否 | 否 | 是 | 否 | -- | DepNote | 部门备注 | Varchar(50) | 否 | 否 | 否 | 否 | -use test04_school; -CREATE table department( -DepNo int not null PRIMARY key unique key, -DepName VARCHAR(50) not null, -DepNote varchar(50)); + create table Department( + DepNo int primary key, + DepName varchar(20) not null, + DepNote varchar(50) + ); -- 表2 Teacher表的定义 -- -- | **字段名** | **字段描述** | **数据类型** | **主键** | **外键** | **非空** | **唯一** | @@ -381,15 +267,16 @@ DepNote varchar(50)); -- | DepNo | 部门号 | int | 否 | 是 | 否 | 否 | -- | Salary | 工资 | float | 否 | 否 | 否 | 否 | -- | Address | 家庭住址 | varchar(100) | 否 | 否 | 否 | 否 | -CREATE table teacher( -Number int not null primary key unique key, -Name VARCHAR(30)not null, +create table Teacher( +Number int primary key, +Name varchar(30) not null, Sex varchar(4), Birth date, DepNo int, +foreign key (DepNo) references Department(DepNo) on update cascade on delete cascade, Salary float, -Address varchar(100), -FOREIGN key (DepNo)REFERENCES department(DepNo)); +Address varchar(100) +); -- 3、添加记录 -- -- | **DepNo** | **DepName** | **DepNote** | @@ -398,7 +285,10 @@ FOREIGN key (DepNo)REFERENCES department(DepNo)); -- | 602 | 网络技术系 | 多媒体技术等专业 | -- | 603 | 艺术设计系 | 广告艺术设计等专业 | -- | 604 | 管理工程系 | 连锁经营管理等专业 | -INSERT into department VALUES(601,'软件技术系','软件技术等专业'),(602,'网络技术系','多媒体技术等专业'),(603,'艺术设计系','广告艺术设计等专业'),(604,'管理工程系','连锁经营管理等专业'); + insert into Department values(601,'软件技术系','软件技术等专业'), + (602,'网络技术系','多媒体技术等专业'), + (603,'艺术设计系','广告艺术设计等专业'), + (604,'管理工程系','连锁经营管理等专业'); -- | **Number** | **Name** | **Sex** | **Birth** | **DepNo** | **Salary** | **Address** | -- | ---------- | -------- | ------- | ---------- | --------- | ---------- | ------------ | -- | 2001 | Tom | 女 | 1970-01-10 | 602 | 4500 | 四川省绵阳市 | @@ -406,8 +296,10 @@ INSERT into department VALUES(601,'软件技术系','软件技术等专业'),(60 -- | 2003 | Mike | 男 | 1990-06-01 | 604 | 1500 | 重庆市渝中区 | -- | 2004 | James | 女 | 1980-10-20 | 602 | 3500 | 四川省成都市 | -- | 2005 | Jack | 男 | 1975-05-30 | 603 | 1200 | 重庆市南岸区 | -INSERT into teacher VALUES(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,'重庆市南岸区'); + insert into Teacher values(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表的所有记录。 -SELECT*from teacher; -``` - +SELECT * FROM Teacher; \ No newline at end of file