--数据库操作(DDL:Data Definition Languages)--创建数据库(在磁盘上创建一个对应的文件夹)CREATEDATABASE[IFNOTEXISTS] aggressive2019 [characterSET utf8]--查看数据库SHOWdatabases;--查看所有数据库showCREATEDATABASE aggressive2019 --查看数据库的创建方式ALTERDATABASE aggressive2019 CHARACTERSET utf8 --修改数据库use aggressive2019;--使用数据库SELECTdatabase();--查看当前使用的数据库--数据表操作CREATETABLE employee(
id INTPRIMARYKEYAUTO_INCREMENT,
name VARCHAR(20),gender bitDEFAULT1,
birthday DATE,
entry_data date,
job VARCHAR(20),
salary DOUBLE(4,2)UNSIGNED,
resume text);--查看表结构DESC employee;--查看表结构SHOWcolumnsfrom employee;--查看当前数据库表建表语句SHOWCREATETABLE employee;--增加列(字段)ALTERALTERTABLE employee ADD addr VARCHAR(20)NOTNULLUNIQUEAFTER name;--添加多个字段ALTERTABLE employee ADD age intFIRST,ADD workage VARCHAR(20)AFTER addr;--修改一列类型 modifyALTERTABLE employee MODIFY age TINYINTDEFAULT20;--修改列名 changeALTERTABLE employee CHANGE age AGE INTDEFAULT28FIRST;--删除一列ALTERTABLE employee DROP addr;--修改表名RENAMETABLE employee to employee1;--修改表所用的字符集ALTERTABLE employee CHARACTERSET utf8;--删除表droptable employee;--添加主键,删除主键ALTERTABLE employee addPRIMARYKEY(id);ALTERTABLE employee MODIFY id INTAUTO_INCREMENT;--删除主键altertable employee modify id int;ALTERTABLE employee dropPRIMARYKEY;--添加唯一索引ALTERTABLE employee ADDUNIQUEINDEX index_age(age);--添加联合索引ALTERTABLE employee ADDUNIQUEINDEX name_age(name,age);--删除唯一索引ALTERTABLE employee DROPINDEX name_age;--创建文章表CREATETABLE article(
id INTPRIMARYKEYAUTO_INCREMENT,
title VARCHAR(20),
publish_date INT,
click_num INT,
is_top TINYINT(1),
content TEXT);--完整性约束条件之主键约束(一张表只能有一个主键,非空且唯一,主键类型不一定是非整型)--单字段主键CREATETABLE users(id INTPRIMARYKEY,
name VARCHAR(20),
city VARCHAR(20));--多字段联合主键CREATETABLE users2(
id INT,
name VARCHAR(20),
city VARCHAR(20),PRIMARYKEY(name,id));
2.表记录操作
--表记录之增、删、改--增加一条记录CREATETABLE employee_new(
id INTPRIMARYKEYAUTO_INCREMENT,
name VARCHAR(20)notnullunique,
birthday VARCHAR(20),
salary FLOAT(7,2));INSERTINTO employee_new(id, name, birthday, salary)VALUES(1,'yuan','1990-09-09',9000);INSERTINTO employee_new VALUES(2,'wx','1988-08-07',10000);INSERTINTO employee_new (name,salary)VALUES('xialv',1000);--插入多条数据INSERTinto employee_new values(4,'alvin1','1993-04-20',3000),(5,'alvin2','1995-05-12',5000);INSERTINTO employee_new SET id=12,name='alvin3';--set 插入法--修改表记录UPDATE employee_new SET birthday='1989-10-24'WHERE id=1;--将yuan的薪水在原有的基础上增加1000元UPDATE employee_new SET salary=salary+1000where name='yuan';--删除表记录(delete from employee_new WHERE ...)--删除表中名称为alex的记录DELETEFROM employee_new WHERE name='alex';--删除表中所有记录DELETEfrom employee_new;--使用truncate删除表中的记录TRUNCATEtable employee_new;
3.表记录之查询(单表查询)
--查询表达式SELECT*|field1,filed2 ...FROM tab_name
WHERE 条件
GROUPBY field
HAVING 筛选
ORDERBY field
LIMIT 限制条数
CREATETABLE ExamResult(id INTPRIMARYKEYAUTO_INCREMENT,
name VARCHAR(20),
JS DOUBLE,
Django DOUBLE,
OpenStack DOUBLE);INSERTINTO ExamResult VALUES(1,'yuan',98,98,98),(2,'xialv',35,98,67),(3,'alex',59,59,62),(4,'wusir',88,89,82),(5,'alvin',88,98,67),(6,'yuan',86,100,55);--查询表中所有学生的信息SELECT*FROM ExamResult;--过滤表中的重复数据SELECTDISTINCT JS FROM ExamResult;--查询表中所有学生的姓名和对应的JS成绩SELECT name,JS FROM ExamResult;--SELECT 也可以使用表达式,并且可以使用:字段 as 别名或者:字段 别名SELECT name,JS+10,Django+10,OpenStack+10FROM ExamResult;--统计每个学生的总分SELECT name,JS+Django+OpenStack FROM ExamResult;--使用别名表示学生总分SELECT name as 姓名,JS+Django+OpenStack as 总成绩 FROM ExamResult;select name ,JS+Django+OpenStack 总成绩 FROM ExamResult;SELECT name JS FROM ExamResult;--记得加逗号
4.使用where子句,进行过滤查询
--查询姓名为yuan的学生成绩SELECT*from ExamResult WHERE name='yuan';--查询JS成绩大于90分的同学SELECT id,name,JS FROM ExamResult WHERE JS>90;--查询总分大于200分的同学SELECT name,JS+Django+OpenStack as 总成绩 FROM ExamResult WHERE JS+Django+OpenStack>200;--查询JS分数在70——100之间的同学SELECT name,JS FROM ExamResult WHERE JS BETWEEN70AND100;--查询Django分数为75,98,77的同学SELECT name,Django FROM ExamResult WHERE Django in(75,98,77);--查询所有姓王的学生的成绩SELECT*FROM ExamResult WHERE name like'王%';(如果是%则表示任意多字符,此例如唐僧,唐国强
如果是_则表示一个字符唐_,只有唐僧符合。两个_则表示两个字符:__)--查询JS>90,Django>90的同学SELECT id,name FROM ExamResult WHERE JS>90AND Django>90;--查询缺考数学的学生姓名SELECT name FROM ExamResult WHERE JS isNULL;
5.order by 指定排序的列,排序的列即可是表中的列名,也可以是select语句后制定的别名
1--Asc升序、Desc 降序,其中asc为默认值 order by 子句应位于select 语句的结尾2--对JS成绩排序后输出3SELECT*FROM ExamResult ORDERBY JS;4--对总分排序后从高到底的顺序输出5SELECT name,JS+Django+OpenStack as 总成绩 FROM ExamResult ORDERBY 总成绩 DESC;6--对姓李学生成绩排序输出7SELECT name,JS+Django+OpenStack as 总成绩 FROM ExamResult WHERE name LIKE'a%'ORDERBY 总成绩 DESC;
--统计表中所有记录--COUNT(列名)统计行的个数--统计一个班有多少学生,先查出所有学生,再用count包上selectcount(*)from ExamResult;--统计JS成绩大于70的学生有多少个selectcount(JS)from ExamResult where JS>70;--统计总分大于280的人数有多少个selectcount(name)from ExamResult where JS+Django+OpenStack>280;
8.SUM(列名):统计满足条件的行的内容和
--统计一个班JS的总成绩,先查出所有JS成绩,再用SUM包上SELECTsum(JS)AS JS总成绩 FROM ExamResult;--统计一个班各科分别的总成绩selectsum(JS)AS JS总成绩,sum(Django)AS Django总成绩,sum(OpenStack)AS OpenStack总成绩 FROM ExamResult;--统计一个班各科的成绩总和selectsum(ifnull(JS,0)+ifnull(Django,0),+ifnull(OpenStack,0))AS 总成绩 FROM ExamResult;SELECTsum(JS+OpenStack+Django)AS 总成绩 FROM ExamResult;--统计一个班JS成绩平均分SELECTsum(JS)/count(*)FROM ExamResult;
--求班级最高分和最低分(数值范围在统计中特别有用)SELECTmax(ifnull(JS,0)+ifnull(Django,0)+ifnull(OpenStack,0)) 最高分 FROM ExamResult;SELECTmin(ifnull(JS,0)+ifnull(Django,0)+ifnull(OpenStack,0)) 最低分 FROM ExamResult;--求购物表中单价最高的商品名称及价格SELECT id,max(price)FROM order_menu;SELECTmax(price)FROM order_menu;
11.注意
--注意:Mysql在执行sql语句时的执行顺序:from where select group by having order by
分析:SELECT JS as JS成绩 FROM ExamResult WHERE JS成绩>90;不成功
SELECT JS as JS成绩 FROM ExamResult HAVING JS成绩>90;--成功
SELECT*FROM ExamResult WHERE name REGEXP'yu';SELECT*FROM ExamResult WHERE name REGEXP'yun$';SELECT*FROM ExamResult WHERE name REGEXP'm{2}';
14.外键约束
创建外键
--每一个班主任会对应多个学生,而每一个学生只能对应一个班主任--主表createtable ClassCharger(
id tinyintprimarykeyauto_increment,
name VARCHAR(20),
age INT,
is_married boolean);insertinto ClassCharger(name, age, is_married)VALUES('冰冰',12,0),('丹丹',14,0),('歪歪',22,0),('姗姗',20,0),('小雨',21,0);--子表(--作为外键一定要和关联主键的数据类型保持一致)createtable Student(
id INTPRIMARYKEYAUTO_INCREMENT,
name VARCHAR(20),
charger_id TINYINT)ENGINE=innodb;INSERTINTO Student(name,charger_id)VALUES("alvin1",2),("alvin2",4),("alvin3",1),("alvin4",3),("alvin5",1),("alvin6",3),("alvin7",2);--增加和删除外键altertable Student ADDconstraint abc foreignkey(charger_id)references ClassCharger(id);alterTABLE Student dropFOREIGNKEY abc;
15.多表查询
CREATEDATABASE aggressive2020 CHARACTERSET utf8;createtable employee(emp_id INTprimarykeynotnull,
emp_name varchar(50),
age int,
dept_id int);INSERTinto employee(emp_id,emp_name,age,dept_id)VALUES(1,'A',19,200),(2,'B',26,201),(3,'C',30,201),(4,'D',24,202),(5,'E',20,200),(6,'F',38,204);createtable department(
dept_id int,
dept_name varchar(100));insertinto department values(200,'人事部'),(201,'技术部'),(202,'销售部'),(203,'财政部');--内连接:仅选出两张表中互相匹配的记录,而外连接会先出其他不匹配的记录,最常用的是内连接-- select * from employee,department where employee.dept_id = department.dept_id;--select * from employee inner join department on employee.dept_id = department.dept_id;
外连接:
左连接:包含所有左边表中的记录甚至是右边表中没有和它匹配的记录
右连接:包含所有右边表中的记录甚至是左边表中没有和它匹配的记录
全外连接:在内连接的基础上增加左边有右边没有和右边有左边没有的结果
select*from employee RIGHTJOIN department on employee.dept_id = department.dept_id
UNIONselect*from employee LEFTJOIN department on employee.dept_id = department.dept_id;
多表查询之复合条件查询
--查询员工年龄大于等于25的部门selectdistinct dept_name from employee,department where
employee.dept_id =department.dept_id AND age>25;--以内连接的方式查询employee和department表,并且以age字段升序方式显示SELECT*FROM employee,department WHERE employee.dept_id=department.dept_id ORDERBY age ASC;--多表查询之子查询--查询employee表,但dept_id必须在department表中出现过select*from employee WHERE dept_id in(SELECT dept_id FROM department);--查询员工年龄大于等于25岁的部门select dept_id,dept_name FROM department WHERE dept_id IN(SELECTDISTINCT dept_id FROM employee WHERE age>25);select*FROM employee WHEREexists(SELECT dept_name FROM department
WHERE dept_id=205);