###数据库
- 增:create database zyw;
- 删:drop database zyw;
- 改:rename database zyw to newzyw;修改数据库名zyw为新名字newzyw
- 查:show databases;查看都有哪些数据库
- 用:use zyw;表示切换到zyw数据库里,后续操作都在这个库里 ###数据表
- 增:create table student(字段名称 数据类型,字段名称 数据类型),新增表必须确定都有哪些字段,每个字段的数据类型。 如create table student(id int(4), name char(20), sex int(4));
- 删:drop table student;
- 改:rename table student to newstudent;修改表名
- 查:show tables;查看这个库里所有表;
- 查询表所有内容:select * from student;查询表中所有记录
- 查表结构:desc student;查看表结构,含有哪些字段
表字段
- 增加字段:alter table student add cout int; 用add关键字增加+字段+字段数据类型;
- 删除字段:alter table student drop count;用drop删除关键字+字段,即可删除字段;
- 修改字段类型:alter table student modify name char(11);modify 修改的是name字段的数据类型及位数
- 修改字段名称:alter table student change sex nsex char(2); change 修改字段sex名称为nsex并设置数据类型和位数;
- 查表结构:desc student;查看表结构,含有哪些字段 ###表字段约束
- 唯一约束:unique,不能重复 比如id,身份证号,手机号,邮箱等唯一身份标记 创建表或增加字段时设置唯一的方法是一样的,在字段数据类型后面加unique 如:alter table users add uid int(2) unique; 增加一个uid唯一字段
- 非空约束,not null,不能为空 如:create table stu(id int(2) unique,name char(11) not null); 创建表时候设置id唯一,name非空;
- 同时限制唯一和非空,创建或修改时直接加约束,无须加逗号 如:create table stu(id int(2) unique not null,name varchar(11)); unique和not null顺序无所谓
- 唯一和非空的组和约束,就形成了主键约束,也可以直接用主键约束代替组和 如:create table stu(id int(2) primary key,name varchar(12));
- 自动增长auto_increment create table stu(id int primary key auto_increment,name varchar(12)); 一般用主键配合自动增长使用,int不设置位数。即便删除最后一位再增加会自动跳过并增加。
- 外键foreign key b表外键约束b表中某个字段必须参考a表的某个字段的记录 b表外键必须是a表的主键
create table class(id int primary key auto_increment,name varchar(12)); 先创建一个班级表
desc class; 主键是id自增长
insert into class values(null,"一班"); insert into class values(null,"二班"); 插入数据
create table stu(id int primary key auto_increment,name varchar(12),classnum int,foreign key(classnum) references class(id));
创建一个学生表,包含id,姓名,和班级号,外键班级号参考class表的id
insert into stu values(null,"jone",2);新增学生
本表最后一个字段时外键,如果是3,而班级表中没有id为3的记录,就插入不成功;
创建外键的语法
create table 表名(字段 类型及约束,...,foreign key(字段名) references 另表名(字段名));
###记录
-
增:insert into student(id,uname,age) values(1,"zyw","32");注意顺序对应,字符串加引号; 如果所有字段都插入,可以不写前面的字段,如insert into users values(2,"33","tom");
-
删:delete from 表名 where 条件表达式,如delete from student where id=1; delete from student;清空表数据,后面不跟条件的话
-
改:update 表名 set 字段名=新的字段值,... where 条件表达式,如 update student set name="xinxin",sex="f" where id=1;
-
查:seletc * from student;查询所有记录,后面还有复杂查询 ###查询
-
查询所有字段 select * from 表名
-
简单查询 select xxx from 表名; xxx可以是:字段列表,表达式,函数 字段列表:select sal,ename from emp; 从emp表中查询sal和ename字段,字段中间用逗号分隔,结果二维表按查询字段顺序列出 去掉重复的用distinct,如select distinct job from emp; 表达式:seletc ename,sal*12 from emp;简单查询年薪,字段的算术运算 函数:比较复杂
-
条件查询where+条件表达式 = > < >= <= <>不等于 select * from emp where sal>600; select * from emp where sal<>800;查询不等于800的结果
-
and,多个条件都要满足 select * from emp where sal>=800 and sal<1500;
-
between and,相当于大于等于a and 小于等于b select * from emp where sal between 800 and 1500; select * from emp where sal>=900 and sal<=1500; 完全一样的结果,注意是大于等于
-
or,或者的关系 select * from emp where sal=800 or sal>1300; select * from emp where sal=800 or sal=1600; 如果是=,并且是or,可以用in语句 in语句,表示只要符合条件中的任何一个都会被查询到 select * from emp where sal in (800,1600);和上面查询结果一样 not in语句,表示只要不是符合条件中的任何一个才会被查询到 select * from emp where sal not in (800);
-
null 筛选一个字段为空,用关键字is select * from emp where comm is null; 筛选一个字段不为空,用关键字is not null;
-
别名 查询中可以给查询字段设置一个别名,让返回结果表头更容易看懂 select ename as "姓名",sal*12 as "年薪" from emp; 姓名是ename字段的别名,也可以不写as
-
分组查询 select avg(sal) from emp;这样查的结果是,表中所有人的平均工资 如果我想按组划分,查询平均公司,就需要用分组查询group by select avg(sal) from emp group by deptno; 根据deptno字段分组查询平均工资 加上组名显示效果更佳 select avg(sal) as "平均",deptno as "组id" from emp group by deptno; 平均avg(),最大max(),最小min(),总数count() select deptno as "组id" ,count() as "计数" from emp group by deptno; select deptno as "组id" ,count() as "计数" from emp group by deptno; 不在分组函数中的字段,必须存在于group by后面,否则会出错
-
子查询 一个查询的结果可以作为另一个查询的数据源或条件 把查询结果当做条件 比如查询最高工资的人的姓名,就先查出最高工资,然后作为条件查人名 select max(sal) from emp select ename,sal from emp where sal=(select max(sal) from emp); 把查询结果当做数据源,接着查询 比如查询平均工资最大的部门,首先查询平均工资,然后再查出最大的 select avg(sal),deptno from emp group by deptno;
-
多表查询 select ename,dname from emp,dept where emp.deptno=dept.deptno; select ename,dname from emp join dept on emp.deptno=dept.deptno; 两种写法,从两张表中根据相同的deptno查询ename和dname