结构
create table 表名(字段名 类型);
建立表s1,id字段为int类型,设置为自增主键
create table s1(
id int AUTO_INCREMENT PRIMARY KEY,
name char(20),
age int
);
插入一条数据
insert into s1(name,age) values('xx',11),('dd',35);
将s1复制为s2表,from后面可以接查询语句去查询。
create table s2 select * from s1;
为s2表新增一个age字段,放在name字段后面
alter table s2 add age int(4) after name;
复制s1的表结构成s2,不包含数据。
create table IF NOT EXISTS s2 (LIKE s1);
删除表
drop table 表名;
删除表所有记录
DELETE FROM 表名;
删除某个表id为3百万的记录,只要匹配就删除。
delete from s1 where id=3000000;
清空表数据-1
truncate table table_name;
清空表数据-2
delete * from table_name;
注 : truncate操作中的table可以省略,delete操作中的*可以省略
truncate、delete 清空表数据的区别 :
将表s1的名字改为s2
alter table s1 rename to s2;
将s1表所有id设置为10
update s1 set id=10;
将名字为张三的行,id号设置为20
update s1 set id=20 where name='zhangsan';
更改默认存储引擎
alter table 表名 ENGINE=InnoDB;
语法,格式固定
select xx from xx where xx;
以列表形式显示
select xx from xx where xx\G;
将name显示为姓名,age显示为年龄
select name AS '姓名',age AS '年龄' from xx where xx;
可以多条件查询
select * from test where id>2 and id<5;
模糊匹配
select * from s3 where name LIKE 'A%' or email like '3%' ;
正则匹配,以A开头或者A结尾的
select * from s3 where name regexp '^A|A$';
将age的平均数作为条件,和age比对,找出大于平均数的
select name,age from s1 where age > (select avg(age) from a1);
将所有id号求和
select sum(id) from s1;
查看表记录数量
SELECT count(*) FROM 表名;
统计一个字段不重复的值的个数
select count(distinct user) from mysql.user;
将s1,s2表的xx字段合并一起显示,但会重启,去重用union
select xx from s1 union all select xx from s2;
排列显示
按id的排列顺序显示查询到的东西
select * from s2 order by id asc;
倒叙排列
select * from s2 order by id desc;
行数显示
查看前2行数据
select * from s2 limit 2;
查看第一行的后2行数据
select * from s2 limit 1,2;
联合表查询
会将2个表拼一起
select * from s1,s2;
where将2部分相同的地方找出来,保证不重复。再将a1全部输出,a2只输出成绩部分。
select a1.*,a2.grade from a1,a2 where a1.name=a2.name;
查询表分区
show create table tlog_rsp\G;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。