SQL通用语法
SQL:
DDL数据库操作:
查询所有库数据:show databases;
查询当前库数据:select database();
创建:create database [if not exists] 数据库名
[default charset 字符集] [collate 排序规则]; utf8mb4
删除:drop database [if exists] 数据名;
使用:use 数据库名:
查询当前数据库所有表:show tables;
查询表结构:desc 表名;
查询指定表的建表语言:show create table 表名;
DDL-表操作-创建:
create table 表名( 字段1 字段1类型 [comment 字段1注释],
字段2 字段2类型 [comment 字段2注释],
字段3 字段3类型 [comment 字段3注释] )[comment 表注释];
ENGINE=InnoDB (存储引擎)
DDL-表操作-数据类型:
数值类型:
M(精度)和D(标度):eg:123.45 123 有三位即精度为3 45有两位即标度为2
TinyInt SamllInt MediumInt Int或者Integer BigInt Float Double Decimal
age TINTINT UNSIGNED
score double(4,1)eg:100.5 (一共有4位,小数点后1位)
字符串类型:
char(10)定长,不够10个空格来凑 ----->性能好
varchar(10)输入的长度只要不超过定义的长度则就是多少长度----->性能较差
日期时间类型:
案例
DDL-表操作-修改:
增加字段:Alter table 表名 add 字段名 类型(长度) [comment 注释] [约束];
修改数据类型:Alter table 表名 modify 字段名 新数据类型(长度);
修改字段名和字段类型:Alter table 表名 change 旧字段名 新字段名 类型(长度)
[comment 注释] [约束];
删除字段:Alter table 表名 drop 字段名;
修改表名:Alter 表名 rename to 新表名;
DDL-表操作-删除:
删除表:drop table [if exists] 表名;
删除指定表,并重新创建该表: truncate table 表名;