SQL基本用法总括一

整理一份SQL基本用法,

包含:①表: 创建,查询,修改,重命名

           ②删除:字段,表,数据,主键,自增长

           ③新增:字段,表

           ④更新,排序,分组

           ⑤having 子句

           ⑥内连接/外连接

           ⑦自增长

详细如下

一、表的创建,查询等

1.创建表   create table table_name

2.查库: show databases

3.查库下的表  :use  table_name  或者 use  tables

4.查创建表:show create table table_name

5.重命名表:rename table  old table_name to new table_name

6.修改字段集: alert table table_name charset (GBK/UTF8)

7.查表字段: desc table_name

二、删除表/字段/数据/主键/自增长

1.删除表:drop  table  table_name

2.删除字段:alter table  table_name drop name

3.删除数据:delete from table_name where 条件

4.删除数据,做限制  delete from table_name where 条件 limit  数量

5.删除主键:alter table table_name  drop  primary key

6.删除自增长:alter  table  table_name   modify  字段类型

三、新增字段/重命名字段

1.新增数据:insert into table_name(id,name,sex) values (1,'tom','m')

2.新增字段,放表第一位置:alter table table_name  add column  name  varchar(10)(/int/char) first

3.重命名字段:alter table  table_name change  old name   new name varchar(10)(/int/char) 

四、更新

1.update  table_name set   old value  =  new value   where  条件

五、排序

1.order by  字段名  [asc  or  desc]

六、分组

1.group by :  select id ,name,count(sex) from  table1_name, table2_name group by  id

分组规定:

  • GROUP BY 子句出现在 WHERE 子句之后,ORDER BY 子句之前;
  • 除了汇总字段外,SELECT 语句中的每一字段都必须在 GROUP BY 子句中给出;
  • NULL 的行会单独分为一组;
  • 大多数 SQL 实现不支持 GROUP BY 列具有可变长度的数据类型

七、having 子句

1.select id ,name,count(*) from  table1_name, table2_name group by  id  having count(*) >2

八、内连接/外连接(左连接 & 右链接)

1.内连接(inner join):左表 inner join  右表 on  左表.字段 = 右表.字段 

2.外连接 --->左连接(left  join)  :左表  left join  右表 on  左表.字段 = 右表.字段

3.外连接 --->右连接(right  join)  :左表  right join  右表 on  左表.字段 = 右表.字段

九、自增长

1.自增长语法:auto_increment

 2.修改自增长: alter  table  table_name  auto_increment =value

  ===>步骤: ①查看自增长的值: show  create table  table_name

                       ②修改值(只大不小):alter  table  table_name  auto_increment =value

                       ③插入一条数据,自增长从新值开始:

                       ④删除自增长: alter table  table_name  modify  字段类型

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Carl_奕然

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值