zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

MySQL数据库 SQL语句详解

2023-02-25 18:19:24 时间

数据库常用操作

操作

语句

创建数据库

create database if not exists 数据库名;

查看所有数据库

show databases;

切换数据库

use 数据库名;

删除数据库

drop database if exists 数据库名;

修改数据库编码

alter database 数据库名 character set utf8;

表结构常用操作

操作

语句

创建表

create table if not exists 表名(字段名 数据类型, 字段名 数据类型, ...);

查看当前数据库所有表

show tables;

查看表结构

desc 表名;

查看指定表的创建语句

show create table 表名;

删除表

drop table 表名;

添加列

alter table 表名 add 列名 数据类型(长度);

修改列名和类型

alter table 表名 change 旧列名 新列名数据类型(长度);

删除列

alter table 表名 drop 列名;

修改表名

rename table 表名 to 新表名;

增删改

操作

语句

向表中插入列

insert into 表名(列1, 列2, 列3) values(值1, 值1, 值1), (值2, 值2, 值2);

向表中所有列插入数据

insert into 表名 values(值1, 值1, 值1);

数据修改

update 表名 set 字段名=值, ... , 字段名=值 where 条件

删除表中数据

delete from 表名 where 条件

删除表

truncate 表名

MYSQL约束

操作

语句

主键

primary key

删除主键约束

alter table 表名 drop primary key;

自增长

auto_increment

非空

not null

创建表前指定

create table 表名(id int not null, account varchar(20) not null);

创建表后指定

alter table 表名 modify id int not null;

删除非空约束

alter table 表名 modify 字段 类型

唯一

unique

删除唯一约束

alter table 表名 drop index 唯一约束名

默认

default

删除默认约束

alter table 表名 modify 列名 类型 default null

零填充

zerofill

删除零填充约束

alter table 表名 modify 字段 类型

外键

foreign key