--mysql建表约束

--主键约束
它能够唯一确定一张表中的内容,也就是我们通过某个字段添加约束,就可以是的该字段唯一(不重复)且不为空。
create table  user(
    id int primary key,
    name varchar(20)
    );
    --联合主键
    只要联合的主键加起来不重复即可
    create table user1(
    id int,
    name varchar(20),
    password varchar(20),
    primary key(id,name)
    );
    insert into user1 values(1,'张三','123');
    insert into user1 values(2,'李四','123');
    insert into user1 values(3,'张三','123');
--自增约束
--auto_increment
create table user2(
 id int primary key auto_increment,
 name varchar(20)
);
insert into user2 (name)values('张三');
insert into user2 (name)values('李四');
mysql> insert into user2 (name) values('张三');
Query OK, 1 row affected (0.03 sec)

mysql> insert into user2 (name) values('李四');
Query OK, 1 row affected (0.03 sec)

mysql> select * from user2;
+----+--------+
| id | name   |
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
+----+--------+
会对id进行一个自动排序
2 rows in set (0.00 sec)
--如果说在创建表时忘记创建主键约束,如何添加主键约束
create table user3(
id int,
name varchar(20)
);
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

--修改表结构
alter table user3 add primary key(id);
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

--删除表结构

alter table user3 drop primary key;
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
可以会看到user3中没有了primary key
--使用modify修改字段,添加约束

--外建约束
--涉及到两个表:父表,字表

--班级
create table classes(
    id int primary key,
    name varchar(20)
);
--学生表
create table student(
    id int primary key,
    name varchar(20),
    class_id int,
    foreign key(class_id) references classes(id)
);
mysql> desc classes;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(20) | YES  |     | NULL    |       |
| class_id | int(11)     | YES  | MUL | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
insert into classes values(1,'一班');
insert into classes values(2,'二班');
insert into classes values(3,'三班');
insert into classes values(4,'四班');

insert into student values(1,'张三',1);
insert into student values(2,'李四',2);
insert into student values(3,'王五',3);
insert into student values(4,'赵流',4);
insert into student values(5,'赵流',5);
mysql> insert into student values(5,'赵流',5);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
--主表没有的元素,副表不可以进行引用
--主表中记录被副表引用的元素不能被删除
delete from classes where id = 4;
mysql> delete from classes where id = 4;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
mysql> select * from classes;
+----+--------+
| id | name   |
+----+--------+
|  1 | 一班   |
|  2 | 二班   |
|  3 | 三班   |
|  4 | 四班   |
+----+--------+
4 rows in set (0.00 sec)

mysql> select * from student;
+----+--------+----------+
| id | name   | class_id |
+----+--------+----------+
|  1 | 张三   |        1 |
|  2 | 李四   |        2 |
|  3 | 王五   |        3 |
|  4 | 赵流   |        4 |
+----+--------+----------+
4 rows in set (0.00 sec)

mysql>

--唯一约束
--约束修饰的字段的值不能重复

create table user5(
    id int,
    name varchar(20),
    primary key(id,name),:w
    
    unique(id)
);
mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  | UNI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
desc user5;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | NO   | PRI | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
insert into user5 values(1,'张三');
insert into user5 values(2,'李四');
insert into user5 values(3,'王五');
mysql> select * from user5;
+----+--------+
| id | name   |/
+----+--------+
|  1 | 张三   |
|  2 | 李四   |
|  3 | 王五   |
+----+--------+
3 rows in set (0.00 sec)
--如何删除唯一约束
alter table user5 drop index name;

--非空约束
--修饰的字段不能为空
create table user6(
    id int,
    name varchar(20) not null
);
desc user6;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

--默认约束
--就是当我们插入字段时,如果没有传值,就会使用默认值
create table user7(
    id int,
    name varchar(20),
    age int Default 10
);
mysql> desc user7;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| age   | int(11)     | YES  |     | 10      |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
insert into user7 (id,name)values(1,'张三');
mysql> select * from user7;
+------+--------+------+
| id   | name   | age  |
+------+--------+------+
|    1 | 张三   |   10 |
+------+--------+------+
1 row in set (0.00 sec)

mysql建表约束的更多相关文章

  1. 基于表的数据字典构造MySQL建表语句

    表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...

  2. 三种常用的MySQL建表语句(转)

    MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1(    ...

  3. MySQL 建库、建用户及建表事项

    1,MySQL建库语句比较简单,一句话: create database tppamltest3 2,创建用户及授权: insert into mysql.user(Host,User,Passwor ...

  4. MySQL 建表字段长度的限制

    脑补,varchar(N),N指的是最大字符数,不是字节数. 先上测试说明:   在MySQL建表时,遇到一个奇怪的现象: root@localhost : test 10:30:54>CREA ...

  5. mysql建表出现Timestamp错误

    mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...

  6. MySql 建表出现的问题:[ERR] 1064 - You have an error in your SQL syntax; check the manual.......

    使用 MySql 建表出现的问题 在使用 Navicat Premium 运行 sql 语句进行建表时,MySQL 报错如下: 建表语句: DROP DATABASE IF EXISTS javawe ...

  7. (转载)用C#实现MySQL建库及建表

    最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...

  8. MySQL 建表语句 create table 中的列定义

    MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value ...

  9. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

随机推荐

  1. cookie,session,token之间的联系与区别

    发展史 1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议, 就是请求加响应,  尤其是我不用 ...

  2. Git使用指南(上)

    1 Git简介 学习一门技术老师更加倾向于看官网的. 度娘看完了,官网看完了,大家还是很懵逼 学生成绩管理系统 登录模块   3.2 登录模块进一步完善    缺一个验证码的功能    3.3 登录模 ...

  3. sass文件编译(.scss->.css),使用ruby环境,在windows10,koala工具,Error: Invalid GBK character "\xE5"

    1 注意事项: 问题描述: 请确保 Encoding.default_external = Encoding.find('utf-8') 是uft-8 编码! sass文件编译时候使用ruby环境,在 ...

  4. Koa & WebSocket inAction

    Koa & WebSocket inAction node.js https://koajs.com/ ping / pong socket.io client send 2 as ping ...

  5. 十大排序算法时间复杂度 All In One

    十大排序算法时间复杂度 All In One 排序算法时间复杂度 排序算法对比 Big O O(n) O(n*log(n)) O(n^2) 冒泡排序 选择排序 插入排序 快速排序 归并排序 基数排序 ...

  6. Web 全栈开发 Redis 面试题

    Web 全栈开发 Redis 面试题 Redis 大 key 问题 https://www.cnblogs.com/xgqfrms/p/13601959.html Redis 数据结构 Redis 初 ...

  7. Web 网站安全测试 & 渗透测试

    Web 网站安全测试 & 渗透测试 Penetration Testing learning path 建一个测试环境来进行渗透测试 安装 Kali Linux -渗透测试操作系统 在虚拟机中 ...

  8. github & code owners

    github & code owners https://help.github.com/en/github/creating-cloning-and-archiving-repositori ...

  9. SPC空投价值高达310万美金,生态建设者直呼真香!

    市场上面有句名言:"人赚不到自己认知以外的财富",这在数字加密上也是共通的.早在本月12日,也就是前天,NGK官方发行的第N波利好---SPC侧链代币空投已经陆续发放了,NGK以及 ...

  10. 痞子衡嵌入式:串行NOR Flash的DQS信号功能简介

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是串行NOR Flash的DQS信号功能. 串行NOR Flash在嵌入式里的应用相当广泛,既可用作数据存储也可以用作代码(XiP)存储, ...