mysql建表约束
--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建表约束的更多相关文章
- 基于表的数据字典构造MySQL建表语句
表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...
- 三种常用的MySQL建表语句(转)
MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1( ...
- MySQL 建库、建用户及建表事项
1,MySQL建库语句比较简单,一句话: create database tppamltest3 2,创建用户及授权: insert into mysql.user(Host,User,Passwor ...
- MySQL 建表字段长度的限制
脑补,varchar(N),N指的是最大字符数,不是字节数. 先上测试说明: 在MySQL建表时,遇到一个奇怪的现象: root@localhost : test 10:30:54>CREA ...
- mysql建表出现Timestamp错误
mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...
- MySql 建表出现的问题:[ERR] 1064 - You have an error in your SQL syntax; check the manual.......
使用 MySql 建表出现的问题 在使用 Navicat Premium 运行 sql 语句进行建表时,MySQL 报错如下: 建表语句: DROP DATABASE IF EXISTS javawe ...
- (转载)用C#实现MySQL建库及建表
最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...
- MySQL 建表语句 create table 中的列定义
MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
随机推荐
- Leetcode(878)-第 N 个神奇数字
如果正整数可以被 A 或 B 整除,那么它是神奇的. 返回第 N 个神奇数字.由于答案可能非常大,返回它模 10^9 + 7 的结果. 示例 1: 输入:N = 1, A = 2, B = 3 输出: ...
- Java中的Lambda匿名函数后续
函数式编程(函数式接口):一个接口只包含一个方法实现 public interface Lambda{ void method(); } // 调用 Lambda lambda = new Lambd ...
- Web 前端 UI 组件库文档自动化方案 All In One
Web 前端 UI 组件库文档自动化方案 All In One 需求 自动化 动态 好用 markdown element-ui 中示例和说明按照一定规则写在md文件中,调用md-loader将md文 ...
- React Hooks: useEffect All In One
React Hooks: useEffect All In One useEffect https://reactjs.org/docs/hooks-effect.html https://react ...
- ituring 挂了
ituring 挂了 图灵社区 挂了 运行时错误 "/"应用程序中的服务器错误. 运行时错误 说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁止远程查看应用程序错 ...
- 微信公众号 webfullstack
微信公众号 webfullstack weixin refs https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_C ...
- HTTP/2 & Push Cache
HTTP/2 & Push Cache HTTP/2 & 推送缓存 https://caniuse.com/#search=http2 https://jakearchibald.co ...
- SameSite cookies explained
SameSite cookies explained
- c++ string split function
#include <string> #include <vector> #include <regex> struct SplitListItem { std::s ...
- 11月16日NGK公链第13期官方快讯!