SQL常用语句

1.简易建表语句(设置自增主键)

CREATE TABLE IF NOT EXISTS tb_user(
    id         INTEGER auto_increment PRIMARY KEY,
    name       VARCHAR(10) NOT NULL,
    password   VARCHAR(10) NOT NULL,
    createtime datetime
)

2.完整建表语句(设置自增主键、外键约束、存储引擎、编码字符集等等)

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user`  (
  `id` int(10) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT COMMENT 用户ID,
  `name` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 用户名,
  `password` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 密码,
  `role_id` int(11) NULL DEFAULT 1 COMMENT 角色ID,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `fk_role`(`role_id`) USING BTREE,
  CONSTRAINT `fk_role` FOREIGN KEY (`role_id`) REFERENCES `tb_role` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB AUTO_INCREMENT = 1000 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = 用户表\r\n ROW_FORMAT = Dynamic;

SET FOREIGN_KEY_CHECKS = 1;

3.查看表结构

SHOW FULL COLUMNS FROM tb_user FROM test;
DESC tb_user;
DESC tb_user id;

4.修改表结构

-- 添加新的字段,修改字段类型
ALTER TABLE tb_user ADD email VARCHAR(50) NOT NULL,MODIFY name VARCHAR(50) NOT null;
-- 修改字段名
ALTER TABLE tb_user CHANGE COLUMN name username VARCHAR(30) NOT NULL;
--删除字段
ALTER TABLE tb_user DROP email;
-- 修改表名
ALTER TABLE tb_user RENAME AS table_user;
RENAME TABLE table_user TO tb_user,table_role TO tb_role;

5.复制表

-- 复制表结构
CREATE TABLE IF NOT EXISTS tb_copy LIKE tb_user;
-- 复制结构和内容
CREATE TABLE IF NOT EXISTS tb_content AS SELECT * FROM tb_user;

6.删除表

DROP TABLE IF EXISTS tb_copy,tb_content;

7.需要注意的运算符

select 4 div 2 ;#除法另一种写法,两参数任一为0则返回null
select 4 mod 3 ;#求模另一种写法,两参数任一为0则返回null
select 1 is null;#判断是否为空,不能用=null,只能有is null判断
select 1 is not null;#判断不为空
select 123 like(%2%) ;#模糊查询
select 123 not like(_2_) ;
select 3 between 1 and 5;#包含了1和5本身
select h in (h,e,l);#是否子集
select h not in (h,e,l);
select 0 and 1;select 0 and null;select 1 and null;select 1 and 1;#结果为:0,0,null,1
select 0 or 1;select 0 or null;select 1 or null;select 0 or 0;#结果为:1,null,1,0
select not 1;select not 0;select not null;#结果为0,1null
select 0 xor 1;select 0 xor null;select 1 xor null;select null xor null;#结果为:1,null,null,null

 

存储过程

1.if

create PROCEDURE example_if(in x int)
begin
    IF x=1 THEN
        select 1;
    ELSEIF x=2 THEN
        select 2;
    ELSE
        select 3;
    END IF;
end;

2.case

create PROCEDURE example_case(in x int)
begin
    CASE x
    WHEN 1 THEN
        select 1;
    WHEN 2 THEN
        select 2;
    ELSE
        select 3;
    END CASE;
end;

3.while

DROP PROCEDURE IF EXISTS example_while;
create PROCEDURE example_while(out sum int)
begin
DECLARE i int DEFAULT 1;
DECLARE s int DEFAULT 0;
    WHILE i<=100 DO
    SET s = s + i;
    SET i = i + 1;
    END WHILE;
SET sum = s;
end;

call example_while(@result);
select @result;

4.loop

DROP PROCEDURE IF EXISTS example_loop;
create PROCEDURE example_loop(out sum int)
begin
DECLARE i int DEFAULT 1;
DECLARE s int DEFAULT 0;
    loop_label:loop
        SET s = s + i;
        SET i = i + 1;
        IF i>100 THEN
            LEAVE loop_label;
        END IF;
    end loop;
SET sum = s;
end;

call example_loop(@result);
select @result;

5.repeat

DROP PROCEDURE IF EXISTS example_repeat;
create PROCEDURE example_repeat(out sum int)
begin
DECLARE i int DEFAULT 1;
DECLARE s int DEFAULT 0;
    REPEAT
        SET s = s + i;
        SET i = i + 1;
    UNTIL i>100 END REPEAT;
SET sum = s;
end;

call example_repeat(@result);
select @result;

表数据操作

1.增删改

-- 插入全部字段数据
INSERT INTO tb_role VALUES(null,管理员,3,CURRENT_TIME);#自增主键可以插入null或者不重复数字
-- 插入指定字段数据
INSERT INTO tb_role (role,num,time) VALUES(管理员,3,CURRENT_TIME());
-- 插入多行数据
INSERT INTO tb_role (role,num,time) VALUES(管理员,4,CURRENT_TIME()),(管理员,5,CURRENT_TIME()),(管理员,6,CURRENT_TIME());
-- 插入查询结果
INSERT INTO tb_role (role,num,time) SELECT 用户 r,1 n,NOW() t from dual;
相关文章
相关标签/搜索