Mysql学习基础语法

-- Mysql  数 据 库 语 法

 

创建数据库 create database 数据库名;


创建表 create table 表名(id int,name varchar(20)    );


复制表和数据 create table 复制后新的表名 select * from 旧表名;


复制表的结构 create table 复制后新的表名 select * from where 1=0 ;


创建索引 create index 索引名 on 表名(列名);


创建视图 create view 视图名 as select 列名1,列名2, ..... ;


删除表 drop table 表名;


显示表的结构 describe 表名 ;


修改表名 alter table 新表名 rename 要修改的表名;


创建列 alter table 表名 add 列名 数据类型;


删除列 alter table 表名 drop 列名;


修改列名或者数据类型 alter table 表名 change 列名 新列名 旧数据类型;


修改数据类型 alter table 表名 modify 列名 新数据类型;


删除索引 drop index 索引名 on 表名 ; alter table 表名 drop index 索引名 ;


主键约束 例如: 第一种方法 id int primary key ;
第二种方法 表里面内容最后写入 primary key (id) ;
唯一约束 例如: id int unique ;
与主键相同 id int unique not null ;
默认约束    例如: gender char(3) default '' ;
非空约束 id int not null ;

外键与外键约束 首先:创建一个外键 与另一个表产生关系


然后:constraint 外键约束名 foreign key(创建的外键名-》列名) references 另一个表名(列名-必须有主键约束) 

区别:有外键表示两表有关系, 有外键不一定有外键约束,有外键约束一定有外键。
删除数据 先删除从表数据 delete from 表名 where 列名 = 列名下的编号 ;
再删除主表数据 delete from 表名 where 列名 = 列名下的编号 ; 

-- 外部写入主键、外键、外键约束


添加自动增长(只能主键) alter table 表名 列名1 列名1 数据类型 auto_increment ;
改变自动增长初始值 alter table 表名 auto_increment=1000 ;
删除自动增长 alter table 表名 change 列名1 列名1 数据类型 ;    
添加主键约束 第一种 alter table 表名 add constraint primary key(列名) ;
第二种 alter table 表名 change 列名1 列名1 数据类型 primary key ;
删除主键约束(必须先删自动增长)    alter table 表名 drop primary key ;
添加唯一约束 alter table 表名 add unique(列名2);
删除唯一约束 alter table 表名 drop index 列名2 ;
添加默认约束 alter table 表名 change 列名2 列名2 数据类型 default '' ;
删除默认约束 alter table 表名 change 列名2 列名2 数据类型 ;
添加非空约束 alter table 表名 change 列名2 列名2 数据类型 not null ;
删除非空约束 alter table 表名 change 列名2 列名2 数据类型 ;
添加外键约束 alter table 表名 add constraint 外键约束名 foreign key(外键列名)references 另一个表名(列名) ;
删除外键约束 alter table 表名 drop foreign key 外键约束名 ;
添加数据 insert into 表名(对应的列:逗号隔开)values(数据1,数据2,*******);    
简写: insert into 表名 values(数据1,数据2,****);
修改数据 update 表名 set 列名 = 新数据    where pk_id=2(加入的条件);
处理空值时: update 表名 set 列名 = 新数据 where 列名 is null ;
只删除数据: update 表名 set 列名 = null where pk_id=2(加入的条件);
删除数据 删除后可以恢复 delete from 表名 where pk_id=2(删除的条件);    
truncate table 表名;

-- 查询数据 语法


查询表中所有信息 select 列名1,列名2 from 表名;
select * from 表名;
表别名 select 表别名.id as '编号',表别名.name as '名字' from 表名 as '表别名' ;    

列别名 select id as '编号',name as '名字' from 表名 ; 
计算列    select age as '当前年龄',age+10 as '10年后的年龄' from 表名;
在一列显示数据(字符串拼接函数) select concat(name," ",age) from 表名;    
计算长度函数 select length("内容");
多列去重复 select distinct 列名 1,列名2 from 表名;
分页(limit) select * from 表名 limit 2; 一个参数,表示从第一行开始返回指定行数的结果。 
select * from 表名 limit 2,5; 两个参数,表示从指定行开始返回指定行数的结果;
分页公式 (页数-1* 每页显示数据的条数 

between and(在什么之间包括上限和下限) select * from 表名 where 列名 between 18 and 20 ; 查询年龄在18--20岁的人(包括18和20)


in (在什么里面) select * from 表名 where 列名 in(17,20,25); 查询17,20,25 岁的人


select * from 表名 where age=17 or age=20 or age=20 ;


not in (不在什么里面) select * from 表名 where age!=15 and age!=30 ; 查询年龄不是15和30岁的人; 
select * from 表名 where age not in(15,30) ;
like 模糊查询 select * from 表名 where 列名 like '张_' ; 查询姓张的(两个字)同学的信息    
select * from 表名 where 列名 like '__' ; 查询姓名为两个字的同学信息
select * from 表名 where 列名 like '李%' ; 查询以李开头姓名的同学信息
select * from 表名 where 列名 like '%赵%'; 查询姓名包含赵字的同学的信息
查询空值 is select * from 表名 where 列名 is null ; 查询姓名为空值的同学的信息
排序 order by select * from 表名 order by age asc; 按年龄从小到大排列同学的信息
select * from 表名 order by age desc; 年龄从大到小排列同学的信息
多列排序 select * from 表名 order by age desc,chengji desc; 年龄从大到小排列,成绩从高到低排列;

执行步骤:1from 2where 3select 4order by 

-- 函数与分组

count(*)统计包括null值得行 select count(*) from 表名;
count(all 列名)统计不包括null的行 select count(all 列名) from 表名;
count(distinct 列名)统计不包括null的行,并且去重复 select count(distinct 列名) from 表名;
sum(列名) 对单个列求和 select sum(列名) from 表名; 
sum(distinct 列名)对单个列求和(相同只求一次) select sum(distinct 列名) from 表名;
avg(列名)求某个列的平均值 select avg(列名) from 表名; 
max(列名)求某个列的最大值 select max(列名) from 表名;
min(列名)求某个列的最小值 select min(列名) from 表名;
round(,2) 保留几位小数 select round(avg(列名1),2) from 表名; 表示对列名1取平均值后保留两位小数


分组 group by      select 列名1,列名2, 聚合函数 from 表名 where 过滤条件 group by 列名1,列名2;
having与where区别 where运行在分组前,having运行在分组后 


SQL语句 执行顺序 1 from 2 where 3 group by(分组) 4 select(投影) 5 having 6 order by(排序) 。

字符串转整型: atoi( 要修改的名字 ) ;
整型转字符串: itoa(新字符串名,要修改的名字,10);
浮点型转字符串: gcvt( 新字符串名,保留的位数,要改的名字);
原文地址:https://www.cnblogs.com/zf29506564/p/5680330.html