1.表操作:

create table tableName

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

(id int(6) not null primary key auto_increatment,name varchar(10) not null,value double(10,2) not null ); # 创建表

show columns from tableName; # 查看表结构

drop table tableName;  # 删除表

alter table tableName  add/drop/modify columnName; #对表字段进行增、删、改类型

alter table t1 change c1 c2 varchar(8); # 将表中列名c1进行修改c2(c2的类型)

alter table t1 rename t2; # 将表t1改名为t2

2.数据表中数据记录的增删改:

insert into tableName values (1,'苹果',12.22),(2,'香蕉',8.88);  # 插入表中数据

delete from tableName where id=1; # 删除满足条件的数据记录

update tableName set c1 = 2 where id = 1; # 更新满足条件的数据记录中的字段值

3.数据查询:

show databases;

show tables;

select * from tableName where id > 10 limit 0,2; # 查询满足条件的数据记录的前两行

count:

select count(name) from tableName where 1;

distinct:

select distinct(name) from tableName where 1;

sum:

select sum(value) from tableName where 1;

limit:

in:

select * from tableName where id in (5,10);

like:

 ? 任何一个单一的字符
 * 任意长度的字符
 # 0~9之间的单一数字
 [字符列表] 在字符列表里的任一值
 [!字符列表] 不在字符列表里的任一值

4.SQL函数实现批量插入数据:

DROP PROCEDURE IF EXISTS proc_initData; #--如果存在此存储过程则删掉


CREATE PROCEDURE proc_initData()


BEGIN
DECLARE i INT DEFAULT 1;

WHILE i<=10000 DO
INSERT INTO data(user_id) VALUES(i); #插入1000条此数据

SET i = i+1;

END WHILE;

END;

CALL proc_initData();

5.补充:

补充1——内连接与外连接,都要求两个表中的键相联系:

内连接:可以查询到双方联合的数据:

select t1.*,t2.* from table1,t1 inner join table2,t2 on t1.num = t2.num;

外连接:可以查询到一边表存在的符合条件的数据,左连接和右连接(left join,right join):

select t1.*,t2 from table1,t1 left jon table2,t2 on t1.name = t2.name where price>1000;

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄