MySQL速查
数据库
增
create database db_name;
删
drop database db_name;
改
use db_name;
查
show database;
show create database db_name;
数据表
增
create table tb_name (column1 datatype, column2 datatype, column3 datatype, ...);
例:
create table example_table (
id int auto_increment primary key comment '主键,自增字段',
name varchar(50) not null comment '字符串类型,不为空',
age int comment '整数类型',
email varchar(100) unique comment '字符串类型,唯一索引',
birth_date date comment '日期类型',
registration_date timestamp default current_timestamp comment '时间戳类型,默认为当前时间',
is_active tinyint(1) default 1 comment '布尔类型,默认为 1',
salary decimal(10, 2) comment '十进制类型,包含小数点',
gender enum('Male', 'Female', 'Other') comment '枚举类型,只能取 Male, Female, Other 中的一个值',
smallint_column smallint comment '小整数类型',
mediumint_column mediumint comment '中等整数类型',
bigint_column bigint comment '大整数类型',
float_column float(10, 6) comment '浮点类型,精度为10,小数位数为6',
double_column double(15, 8) comment '双精度浮点类型,精度为15,小数位数为8',
char_column char(10) comment '固定长度字符串类型',
text_column text comment '文本类型,适用于存储大量文本数据',
blob_column blob comment '二进制大对象,适用于存储大量二进制数据',
boolean_column boolean comment '布尔类型,可存储 TRUE 或 FALSE',
json_column json comment 'JSON 类型,用于存储 JSON 数据',
department_id int comment '外键,引用了另一个表中的 id 字段',
foreign key (department_id) references departments(id)
);
删
drop table tb_name;
改
alter table tb_name add column_name datatype;
alter table tb_name drop column column_name;
查
show tables;
desc tb_name;
show create table tb_name;
数据
增
insert into tb_name (column1, column2, column3, ...) values (value1, value2, value3, ...);
删
delete from tb_name where condition;
truncate table tb_name;
改
update tb_name set column1 = value1, column2 = value2, ... where condition;
查
select column1, column2, ... from tb_name where condition;
License:
CC BY 4.0