MySQL基本命令(增删改查,check约束)总结:

表字段的增,删,改,查:

:alter table 表名 add 字段名 数据类型 【位置】

:alter table 表名 drop 字段名;

:alter table 表名 modify 字段名 数据类型 【位置】;

重命名: alter table 表名 change oldname newname 数据类型 【位置】;

查看表结构:desc 表名;

查看所有表:show tables;

查看部分表:show tables like ‘表名’;

数据的增,删,改,查:

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

insert into 表名 (字段1,字段2……)values(值1,值2.……);

: delete form 表名 where 条件;/ truncate 表名;

:update 表名 set 字段=‘值’ 【where 条件】;

:select */字段列表 form 表名 【where 条件】

表的增,删,改,查:

创建表:create table 表名();

删除表:drop table 表名称;

改表名:alter table 表名rename 新表名;/rename table表名 to 新表名;

查表

查看当前数据库的所有表格:show tables;

查看某数据库的所有表格:show tables from 数据库名;

数据库的增,删,改,查:

创建数据库:create database 数据库名 / create database 数据库名 charset=utf8;

删除数据库:drop database 数据库名;

选择数据库:use 数据库名;

查看当前正在使用的数据库:select database();

查看有哪些数据库:show databases;

约束:

check检查约束

在插入性别时,只能插入男或女,或者将数据控制在一定范围

check检查约束也可以使用enum类型或者触发器

enum:

添加性别字段对其进行约束:alter table 表名 add gender enum(‘男’,‘女’,‘未知’);

键约束:主键约束,外键约束,唯一键约束

Not NULL约束:非空约束

check约束:检查约束

default约束:缺省约束

主键约束(primary key)相当于唯一约束+非空约束的组合

不允许出现重复,也不允许出现空值

每个表只允许出现一个主键约束,一般跟在字段名后

删除主键约束后,非空还存在:alter table 表名称 drop primary key;

唯一键(unique key)简称UK

同一个表中可以有很多个唯一约束

会默认创建一个唯一索引

也是直接跟在字段名后

外键(forgin key)简称FK

用于两个表的两个字段之间的参照关系

保证一个或两个表之间的参照完整性

在从表上建立外键,而且主表要先存在

从表的外键列,在主表中引用的只能是键列(主键,唯一键,外键)

外键一定是在从表中创建,从而找到与主表之间的联系

添加外键进行关联:alter table 表明 foreign key(从表字段名) references 表明(主表字段名);

判断存在就删除然后创建

creata table if exists 表名  

drop table if exists 表名

创建临时表

create temporary table 表名

mysql自增长

auto_increment

添加外键约束

alter table 表名 add constraint fk_引用id foreign key(引用id) references 被引用表名 (被引用id)

添加主键约束

alter table 表名 add constraint pk_id primary key (id);

删除约束

alter table 表名 drop forign key fk_引用id

添加表的字段

alter table 表名 add 字段名 类型 ;

修改表中的字段为空

alter table 表名 modify 字段名  类型  null

修改表中的字段不为空

alter table 表名 modify 字段名  类型 not null

添加表的字段自增主键

alter table 表名 add column 字段名 类型 auto_increment not null, add primary key(cid);

删除表的字段

alter table 表名 drop column 字段;

删除表的主键

alter table 表名 drop primary key;

原文地址:https://www.cnblogs.com/sycl/p/13848903.html