sql基础语法

1.注释语句:

#单行注释
/*多行注释
注释
*/
-- 单行注释,注意有注释符号和输入中间有空格

2.表结构语句:

  新建表:

create table t3(
    id int not null primary key, # id为主键,且不能为空
    name char(20) not null
    ); 
语法:语法 create table 表名称( 字段名 字段名类型 字段描述符,字段名 字段类型 字段描述符);

  删除表:

drop table t3;
语法:drop table 表名称;

  修改表:

alter table t2 add(score int not null);  #新增列(字段,且不能为空)
语法:alter table 表明称 add(字段名 类型 描述符);
alter table t2 drop column score; #删除列
语法:alter table 表名 drop colunm 字段名,drop colunm 字段名;
alter table t2 change name score int(22) not null; #修改列
语法:alter table 表名 change 旧字段名 新字段名 新字段描述符

3.数据型语句:

  新增数据:

insert into t2 values(007,22),(008,33);  #全字段插入
语法:insert into 表名 values(字段1值,字段2值,……),(字段1值,字段2值,……);
insert into t2(id,name) values(10,82); # #个别字段插入
语法:insert inton 表名(字段名) values(值一),(值二);
   insert inton 表名(字段名,字段名) values(值一),(值二);

  删除数据:

delete from t2 where id=4;
语法:delete from 表名 where 条件;(不加条件时:删除整张表数据)

  修改数据:

update t2 set score=69 where id=2;
语法:update 表名 set 更改的字段名=值 where 条件;

  查询数据:

单表查询:
select
* from t2; #单表查询
语法:select * from 表名;
select id from t1; #个别字段查询
语法:select 字段一,字段二 from 表名;
多表查询
select
name from t3 where id=(select id from t2 where score=55); #嵌套查询 语法:select 字段一,字段二…… from 表名 where 条件(查询); (select id from t3 )union(select id from t2); #并查询:union前后查询语句返回的数据数量必须一致 select id from t3 where id in (select id from t2); # 交查询

  常用函数:

select sum(score) from t2;        #求和
select avg(score) from t2;         #求平均值
select count(*) from t2;            #计数(不包含null)
select max(score) from t2;        #最大值
select min(score) from t2;        #最小值

  常用修饰符:

select distinct score from t2;        #distinct 字段中值唯一
select * from t2 limit 2;                    #结果数限制
select * from t2 order by score;    #排序(默认升序)
select * from t2 order by score desc;        #降序排序
select * from t2 order by score asc;        #升序排序
select score from t2 group by score;    #分组
原文地址:https://www.cnblogs.com/qiuqiu21/p/14944346.html