mysql常用语句 4 + mysql的约束(非空,唯一,主键,外键)

1.更新语句
update dept1 set loc = ‘wz’,dname = ‘xueshenghui’ where deptno = 10;

mysql> update dept1 set loc = ‘wz’,dname = ‘xueshenghui’ where deptno = 10;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from dept1;
+——–+————-+———+
| DEPTNO | DNAME | LOC |
+——–+————-+———+
| 10 | xueshenghui | wz |
| 20 | RESEARCH | DALLAS |
| 30 | SALES | CHICAGO |
| 40 | OPERATIONS | BOSTON |
+——–+————-+———+
4 rows in set (0.00 sec)

更新语句格式:update 表名 set 字段=”,字段=” where 字段=”;

2.更新所有记录,不加条件即可

update dept1 set dname = ‘bangongshi’, loc = ‘cqust’ ;
mysql> update dept1 set dname = ‘bangongshi’, loc = ‘cqust’ ;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0

mysql> select * from dept1;
+——–+————+——-+
| DEPTNO | DNAME | LOC |
+——–+————+——-+
| 10 | bangongshi | cqust |
| 20 | bangongshi | cqust |
| 30 | bangongshi | cqust |
| 40 | bangongshi | cqust |
+——–+————+——-+
4 rows in set (0.00 sec)

3.删除数据

delete from dept1 where deptno = 10;
mysql> delete from dept1 where deptno = 10;//这里如果不加条件会删除表中的所有数据。
和drop分开区别。drop是删除表,delete是删除数据。
Query OK, 1 row affected (0.01 sec)

mysql> select * from dept1;
+——–+————+——-+
| DEPTNO | DNAME | LOC |
+——–+————+——-+
| 20 | bangongshi | cqust |
| 30 | bangongshi | cqust |
| 40 | bangongshi | cqust |
+——–+————+——-+
3 rows in set (0.00 sec)

5.约束:对字段进行合理的限制。
not null非空,unique 唯一,primary key 主键,foreign key外键。

6.非空约束
drop table if exists t_user;
create table t_user(

id int(3) not null default 100,
name varchar(7) default 'hch'

);
insert into t_user (id,name) values(1,’h1′);
insert into t_user (id,name) values(null,’h2′);

//这里不能为空
mysql> insert into t_user (id,name) values(null,’h2′);
ERROR 1048 (23000): Column ‘id’ cannot be null

7.唯一约束,存在列级约束,表级约束
drop table if exists t_user;
create table t_user(

id int(3)   ,
name varchar(7),
unique(id,name)

);
insert into t_user (id,name) values(1,’h1′);
insert into t_user (id,name) values(1,’h2′);

//这里添加了唯一约束,
mysql> insert into t_user (id,name) values(1,’h2′);
ERROR 1062 (23000): Duplicate entry ‘1’ for key ‘id’
mysql>

mysql> create table t_user(
->
-> id int(3) unique ,
-> name varchar(7) unique//这里两个字段都添加约束,要求都不一样才可以成功。
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t_user (id,name) values(1,’h1′);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_user (id,name) values(2,’h2′);
Query OK, 1 row affected (0.00 sec)

mysql> create table t_user(
->
-> id int(3) ,
-> name varchar(7),
-> unique(id,name)//表级约束,插入数据要求两个联合起来唯一
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t_user (id,name) values(1,’h1′);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_user (id,name) values(1,’h2′);
Query OK, 1 row affected (0.00 sec)

8.主键(primary key)
按数量:单个、复合主键(不推荐)

[En]

By quantity: single, compound primary key (not recommended)

性质:自然、业务主键(不推荐)

[En]

By nature: natural, business primary key (not recommended)

在实际开发中,不要使用业务中的属性作为主键。

[En]

In actual development, do not use an attribute in the business as the primary key.

主键约束只能有一个。
功能:是一条数据的唯一标识

[En]

Function: it is the unique identification of a piece of data

主键值
主键约束
主键字段

drop table if exists t_user;
create table t_user(

id int(3) primary key auto_increment ,
name varchar(7)

);
insert into t_user (name) values(‘h1’);
insert into t_user (name) values(‘h2’);
insert into t_user (name) values(‘h3’);
insert into t_user (name) values(‘h4’);
insert into t_user (name) values(‘h5’);
insert into t_user (name) values(‘h6’);

mysql> create table t_user(
->
-> id int(3) primary key ,
-> name varchar(7)
->
-> );
Query OK, 0 rows affected (0.02 sec)

mysql> insert into t_user (id,name) values(1,’h1′);
Query OK, 1 row affected (0.00 sec)
//主键不能重复,而且不能为空
mysql> insert into t_user (id,name) values(1,’h2′);
ERROR 1062 (23000): Duplicate entry ‘1’ for key ‘PRIMARY’

mysql> create table t_user(
->
-> id int(3) primary key auto_increment ,//主键自增,这里主键只是一个标识作用
-> name varchar(7)
->
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t_user (name) values(‘h1’);
Query OK, 1 row affected (0.01 sec)

mysql> insert into t_user (name) values(‘h2’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user (name) values(‘h3’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user (name) values(‘h4’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user (name) values(‘h5’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t_user (name) values(‘h6’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t_user;
+—-+——+
| id | name |
+—-+——+
| 1 | h1 |
| 2 | h2 |
| 3 | h3 |
| 4 | h4 |
| 5 | h5 |
| 6 | h6 |
+—-+——+
6 rows in set (0.00 sec)

9.外键(foreign key)
FOREIGN KEY约束表明这两个表之间存在父子关系。

[En]

The foreign key constraint shows that there is a parent-child relationship between the two tables.

在建表的时候
例如:t_teacher(父表)
t_id t_name
1 laoyu
2 laodu
3 niexin
4 ruanqidong

t_student(子表)
stu_id stu_name t_name(添加外键约束)
1 h1 laoyu//这里的数据必须是父表中的数据。以此获得联系
2 h2 laoyu
3 h3 laoyu
4 h4 laodu

创建表时,请先创建父表,然后再创建子表

[En]

When creating a table, create the parent table first, and then create the child table

若要添加数据,请首先添加父表,然后添加子表

[En]

To add data, first add the parent table, and then add the child table

删除数据,先删除子表中的数据,再删除父表中的数据

[En]

Delete data, first delete the data in the child table, and then delete the data in the parent table

删除表,先删除子表,再删除父表

[En]

Delete the table, delete the child table first, and then delete the parent table

drop table if exists t_teacher2;
create table t_teacher2(
t_id int(2) primary key,
t_name1 varchar(10)
);
drop table if exists t_student2;
create table t_student2(
stu_id int(2) ,
stu_name varchar(10),
t_name2 int (10),
foreign key (t_name2) references t_teacher2(t_id)//这里的字段一般是主键,至少具有唯一性。
外键值可以为null

Original: https://www.cnblogs.com/journeyhch/p/15563870.html
Author: journeyhch
Title: mysql常用语句 4 + mysql的约束(非空,唯一,主键,外键)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508036/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 聊斋-河间生

    人的善恶在转瞬之间就可以改变,发现错误时往往已经差之千里了,但是发现错误及时改正这不也是很美好的一件事情么?河间生就是讲了这么一件事情。 主角简介:河间某生,家里比较富裕,烧火用的…

    数据库 2023年6月6日
    0109
  • 【MySQL】MySQL的安装、卸载、配置、登陆和退出

    1 MySQL安装 安装环境:Win10 64位软件版本:MySQL 5.7.24 解压版 1.1 下载 https://downloads.mysql.com/archives/…

    数据库 2023年6月16日
    094
  • 4. 事务和锁

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    数据库 2023年6月16日
    0105
  • django中的auth模块与admin后台管理

    1. auth模块 在创建完django项目之后,执行数据库迁移之后,数据库里会增加很多新表,其中有一张名为auth_user的表,当访问django自带的路由admin的时候,需…

    数据库 2023年6月14日
    095
  • Redis学习

    Redis 因为没有指定配置文件 需配置 redis-server redis.windows.conf 之后自动启动 测试性能 redis-benchmark -p 6379 -…

    数据库 2023年6月16日
    079
  • day01-需求分析和系统设计

    对传输数据的分析: 因为在通讯的时候信息的种类和信息比较多,如果使用文本的方式来传递数据,那么服务器拿到信息的时候对其进行拆解会很麻烦。因此使用对象的方式来进行数据的传输(同时使用…

    数据库 2023年6月11日
    092
  • python 学习笔记(十二)–Django 基本知识点小结

    构造函数格式: 作用: 向客户端浏览器返回相应,同时携带响应体内容。 参数: –content:表示返回的内容。 –status_code:返回的HTTP响…

    数据库 2023年6月16日
    084
  • gauss正则找汉字

    select substring( ‘沙特阿拉伯6.20’ from ‘[\u4E00-\u9FA5]{2,5}’ )返回 ‘沙特阿拉伯’ Original: https://ww…

    数据库 2023年6月16日
    076
  • 深入浅出的分析 Hashtable

    作者:炸鸡可乐原文出处:www.pzblog.cn 一、摘要 在集合系列的第一章,咱们了解到,Map 的实现类有 HashMap、LinkedHashMap、TreeMap、Ide…

    数据库 2023年6月14日
    094
  • [转]万智牌规则和异能详解

    下面这些都是之前的旧文档了,直到我发现了一个神奇的网站。建议大家有任何疑问,都可以到这里查看规则文档 点击网站的右上方可以搜索 最近游戏过程中发现规则和异能详解的文档很少,找起来非…

    数据库 2023年6月9日
    0194
  • MyBatis(一)-入门

    ==>>MyBatis中文网 1、第一个 mybastis程序 1.1 导入jar包 3.4.1 5.1.47 org.mybatis mybatis ${mybati…

    数据库 2023年6月16日
    094
  • TypeScript语言基础

    一、什么是TypeScript 编程语言包括动态类型语言和静态类型语言。动态类型语言是指在程序运行阶段才检查变量数据类型的语言,在定义变量时不需要指定变量的数据类型,通常在编译时不…

    数据库 2023年6月14日
    097
  • Consul 入门-gRPC 服务注册与发现

    前言 假如我有钱,我&am…

    数据库 2023年6月6日
    0101
  • Java的日志框架之Logback

    前言 在Java的日志系统里面,有两个概念,一个叫做日志框架,如我们熟悉的Logback, Log4j, Log4j2, JDK自带的java.util.logging等;一个叫做…

    数据库 2023年6月11日
    085
  • Spring Boot 整合Hibernate Validator

    Spring Boot 整合Hibernate Validator 代码仓库: https://github.com/Rain-with-me/JavaStudyCode/tree…

    数据库 2023年6月14日
    0112
  • 项目中所用到的mysql重复过滤

    问题:首先用户会本地上传一批号码(可能重复)到我们项目,通过解析文件,把号码入库(只验证是不是号码其他不做改动)到号码表,然后对号码进行去重操作. 表结构为:主键(id),号码(m…

    数据库 2023年6月11日
    084
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球