mysql 注意小结

char 默认是1 个字符     
char(12) 设置是12个字符  不管是中文还是英文或者数字只能有十二个     
    
设置外键时,这时候外键对应的父键的字段要是主键 非空而且是唯一.

create table t1 (id int primary key auto_increment,name char(12) not null,age int,sex enum('male','female'));
性别 可以在枚举中选择 也可以不写就默认male
sex enum('male','female') not null default 'male',

mysql> create table student(sid int primary key,sname char(3) not null,gender cha
r(12) enum('female','male'),class_id int , foreign key(class_id) references class_i
nfo(cid));
gender char(12) enum 这个位置是错的  本身语法就是错误的因为,enum 已经订好了枚举的内容了,就不需要char()去约束了

where 后边可以与group by 一起使用
group_concat 与group by 分组出来的数据,只是字符串不能用它进行操作的
选择有多个条件时
where 条件 order by 条件  


group by 与聚合函数 使用 比较好使
    不过,对于分中的内容可以求出最早或者最大的值,但是这个值对应的姓名等  是不能够知道的

    select name,post,max(age) from 表名 group by post;
    这个号找出部门的最大年龄,但是名字不知道是谁,这个名字只是随机匹配出来的



使用count()的时候,的得注意了  当count 于where 一起使用的时候,条件是什么计算的就是条件的内容,只要count()括号里边的内容不为空效果是一样的


having 一般都与group by 使用 
having select 选择后才到having  去筛选
不能这样使用的因为,having 之前有默认分组的可能,就把相同的年龄分在一起了,就没有结果;报错
select  name,age from 表 having age >18;
 oeder  by 子字段  一般都是数字型的字段
 order by  是根据条件来 找到符合的内容,然后根据某个字段来对这些内容进行排序
 默认升序 asc   desc 是降序
    比如: 
        年龄升序排的,name 是与age对应的字段
        select name,age from 表名 order by age ;
    查看讲师年龄小到大的员工姓名
    select age,name from 表名 where teacher order by age :
    
 order by  是在select 之前执行吗?  不是
比如说在 一个表中里  部门  员工  姓名  年龄 
想要从某个部门找到入职最早的员工
select name,age from 表名 where post=tercher order by age desc limit 1;
查找入职最早的人与入职时间;
select name,date from 表名 order by age desc limit1;
 
 
 limit
    limit n 取n 个
    limit n,m 从n+1 起取m个数;
 
 聚合函数,查到对于匹配的字段,是随机选取的而不是,准确的
 查出表年龄最大的人名
 select name ,max(age) from 表名 : 
 
 select 字段,avg(age) from 表名 grouop by 对应的字段:
    那么这个字段就是分组对应的平均年龄.  因为group by 是在select 之前执行 分组了,然后在去执行求平均年龄.
    
    
from where group by select having order by limit

比如  select dep_name name  from 表名  where  name='alex';
    会报错的,应为先走from  在走 where  就找不到 name  ,而name是在select 后在把 dep_name改名为 name的  所以报错 
    
    
    
内连接; inner join   俩张表要有共同内容



wherer 能与 count()使用
原文地址:https://www.cnblogs.com/LMTlmt/p/10502454.html