MySQL学习笔记(八)

184 阅读3分钟

这是我参与11月更文挑战的第3天,活动详情查看:2021最后一次更文挑战

3.3 UPDATE语句

  • 格式:update 表名 set 字段名1 = 值1 , 字段2 = 值2 , 字段3 = 值3 ... where 条件;
  • 注意:如果没有条件,数据会全部更新
 【案例】:修改第三条李四的信息为王五,生日为“2001-01-11”
  update t_user set name = 'wangwu', birth = '2001-01-11' where id = 3 ;
  
 【案例】:将所有信息的创建日期修改为此时
  update t_user set create_time = now();

4、约束

  • 概念:约束(constraint)是指在创建表的时候,可以给一些字段加入一些约束
  • 作用:保证表中的数据有效
  • 常见的约束:非空约束(not null)、唯一性约束(unique)、主键约束(primary key)、外键约束(foreign key)、检查约束(check , mysql不支持,oracle支持)

4.1 非空约束

  • not null : 约束的字段不能为空 , 只有列级约束 , 没有表级约束
 【案例】:测试非空约束
 drop table if exists t_vip ; 
 create table t_vip(
     id int ,
     name varchar(255) not null 
 ); 
 insert into t_vip (id , name) values(1 , 'zhangsan');
 insert into t_vip (id , name) values(2 ,'lisi');
 insert into t_vip (id) values (3); 
 // ERROR 1364 (HY000): Field 'name' doesn't have a default value

4.2 唯一性约束

  • unique : 约束的字段不能重复,但可以为null (列级约束)
 【案例】:测试唯一性约束
 drop table if exists t_vip ;
 create table t_vip(
     id int ,
     name varchar(255) unique ,          // 列级约束
     email varchar(255)
 );
 insert into t_vip (id , name , email) values (1 , 'zhangsan' , 'zahngsan@123.com');
 insert into t_vip (id , name , email) values (2 , 'lisi' , 'lisi@123.com');
 insert into t_vip (id , name , email) values (3 , 'wangwu' , 'wangwu@123.com');
 select * from t_vip ;
 ​
 insert into t_vip (id , name , email) values (4 , 'wangwu' , 'wangwu@xinlang.com');
 //ERROR 1062 (23000): Duplicate entry 'wangwu' for key 'name'
 ​
 insert into t_vip (id) values (4);
 insert into t_vip (id) values (5);
 select * from t_vip ;       // 可以执行
  • 多个字段联合唯一性(表级约束)

    • 格式:create table 表名 (字段1 , 字段2 .... ,unique(字段1 , 字段2));
 【案例】:测试两个字段联合唯一性约束
 drop table if exists t_vip ;
 create table t_vip(
     id int ,
     name varchar(255)  ,
     email varchar(255) ,
     unique(name , email)            // 表级约束
 );
 ​
 insert into t_vip (id , name , email) values (1 , 'zhangsan' , 'zhangsan@123.com');
 insert into t_vip (id , name , email) values (2 , 'zhangsan' , 'zhangsan@xina.com');
 select * from t_vip ;
 ​
 insert into t_vip (id , name , email) values (3 , 'zhangsan' , 'zhangsan@123.com');
 //ERROR 1062 (23000): Duplicate entry 'zhangsan-zhangsan@123.com' for key 'name'
  • 一个字段,被not null 和 unique 联合约束

    • 注意:被这两个字段联合约束的字段在desc中会在KEY中显示PRI(primary key) ,会被自动当做主键,成为主键约束 , (mysql专属)

 【案例】:测试一个字段的多种联合约束  drop table if exists t_vip ;  create table t_vip (   id int ,     name varchar(255) not null unique  );  desc t_vip ;  +-------+--------------+------+-----+---------+-------+  | Field | Type         | Null | Key | Default | Extra |  +-------+--------------+------+-----+---------+-------+  | id    | int(11)      | YES  |     | NULL    |       |  | name  | varchar(255) | NO   | PRI | NULL    |       |     PRI表示主键  | email | varchar(255) | YES  |     | NULL    |       |  +-------+--------------+------+-----+---------+-------+  3 rows in set (0.00 sec)  ​  ​  insert into t_vip (id , name) values (1 , 'zhangsan');  insert into t_vip (id , name) values (2 , 'zhangsan'); // 错误  insert into t_vip (id) values (2) ;   // 错误


#### 4.3 主键约束

-   primary key (pk) :

-   相关概念:
 -   主键约束:在主键上加的约束 , 一张表只能添加一个(复合主键也算一个)
    -   主键字段:含有主键的字段
    -   主键值:主键字段中的每一个值 , 每一行记录的唯一标识 , 类似于身份证号 不能重复、不能为null ,具有唯一性。主键值建议使用int \ bigint \ char 类型,不建议用varchar。主键值一般为数字,一般为定长

-   注意:任意一张表都要有主键 ,如果没有主键,表无效