SQL-四大语句

looxy / 2024-11-13 / 原文

DDL

* 数据库操作

* 查询
    * show databases; -- 所有数据库
    * select database(); -- 当前数据库
* 创建
    * create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];
* 删除
    * drop database [if exists] 数据库名;
* 使用
    * use 数据库名;

* 表操作

* 查询
    * show tables; -- 所有表
    * desc 表名; -- 表结构
    * show create table 表名; --表创建语句
* 创建
    * create table 表名 (字段1 类型,字段2 类型 ... [constraint] [外键名称] foreign key(外键字段名) references 主表(主表列名) [on update 外键更新约束] [on delete 外键删除约束]); -- 创建表
* 删除
    * drop table 表名;
    * truncate table 表名;
* 修改
    * 表
        * alter table 表名 rename to 新表名; -- 表重命名
        * alter table 表名 add constraint 外键名称 foreign key  (外键字段名) references 主表(主表字段)
        * alter table 表名 drop foreign key 外键名称;
    * 字段
        * alter table 表名 add 字段名 类型(长度) [comment 注释] [约束];
        * alter table 表名 modify 字段名 新数据类型(长度);
        * alter table 表名 change 旧字段名 新字段名 类型(长度) [comment 注释] [约束];
        * alter table 表名 drop 字段名; -- 删除属性

DML

* 增
	* INSERT INTO stu VALUES (null,'Jack',18),(null,'Tom',20);
* 删
	* DELETE FROM stu WHERE id = 2;
* 改
	* UPDATE stu SET name='Jane' WHERE id = 1;

DQL

* 查
	* SELECT * FROM stu;