数据库使用1

表的完整行约束

default默认值


create table t2(
	id int,
    name char(16),
    gender enum('male','female') default 'male'
);

insert into t2(id,name) values(1,'zc');
insert into t2 values(2,'zcc','female');
+------+------+--------+
| id   | name | gender |
+------+------+--------+
|    1 | zc   | male   |
|    2 | zcc  | female |
+------+------+--------+

当我们插入‘zc’,没有给他传性别,默认的就是'male',

unique唯一

单例唯一

create table t3(
	id int unique,
    namechar(16)

);
insert into t3 values(1,'zc'),(1,'zcc');# Duplicate entry '1' for key 'id'
insert into t3 values(1,'zccz'),(2,'zz');
+------+------+
| id   | name |
+------+------+
|    1 | zc   |
|    2 | zz   |
+------+------+

联合唯一

'''
ip和port标识唯一的计算机
单个的可以重复,但是加在一起必须是唯一的
'''
create table t4(
	id int,
    ip char(16),
    port int,
    unique(ip,port)
);
insert into t4 values(1,'127.0.0.1',8080);
insert into t4 values(2,'127.0.0.1',8081);
+------+-----------+------+
| id   | ip        | port |
+------+-----------+------+
|    1 | 127.0.0.1 | 8080 |
|    2 | 127.0.0.1 | 8081 |
+------+-----------+------+

primary key 主键

  • 从约束效果来看的话primary key=not null unique

  • 除了有约束效果外,他还是innodb存储引擎组织数据的依据,innodb存储引擎在创建表的时候必须要有primary key 能够帮助提示查询效率并也是建表的依据

    • 一张表中有且有一个主键,没有主键从上往下搜索直到遇到一个非空且唯一的字段将它作为主键

      create table t5(
      	id int,
          name char(16),
          age int not null unique,
          addr char(32) not null unique
      );
      
    • 如果表中没有主键也没有其他任何的非空且唯一的字段,那么innodb会采用内部提供的隐藏字段作为主键

    • 一张表中通常都应该有一个主键字段。

      # 单个字段主键
      create table t6(
      	id int primary key,
          name char(16)
      
      );
      # 联合主键
      create table t7(
      	ip char(16),
          port int,
          primary key(ip,port)
      );
      
      
  • auto_increment自增
    当编号太多的时候,人为的去维护太麻烦了,我们可以用自增

    create table t8(
    	id int primary key auto_increment,
        name char(16)
    );
    insert into t8(name) values('zc'),('zz'),('zzc');
    # auto_increment通常是加在主键上面的,不能加在普通字段上面。
    

表与表之间的关系

  • 分类:一对多,多对多,一对一

  • 外键:就是帮我们建立表与表之间的关系的

🍕一对多

员工表和部门表的关系

员工表:一个员工只能呆在一个部门里,

部门表:一个部门可以有多个员工

create table dep(
	id int primary key auto_increment,
    dep_name char(16),
    dep_desc char(32)
);
create table emp(
	id int primary key auto_increment,
    name char(16),
    gender enum('male','female','others') default 'male',
    dep_id int,
    foreign key(dep_id) references dep(id) 
    on update cascade  # 同步更新
    on delete cascade  # 同步删除
);
insert into dep(dep_name,dep_desc) values('后勤部','管理后勤业务'),('市场部','扩业务'),('经理部','管理岗位');
insert into emp(name,dep_id) values('zz',2),('zc',3),('zzc',1)('zac',1);


'''
	1 一对多表关系   外键字段建在多的一方
    2 在创建表的时候 一定要先建被关联表 
    3 在录入数据的时候 也必须先录入被关联表
''' 

🍕多对多

按照上述的方式创建 一个都别想成功!!!
其实我们只是想记录书籍和作者的关系
针对多对多字段表关系 不能在两张原有的表中创建外键
需要你单独再开设一张 专门用来存储两张表数据之间的关系
create table book(
	id int primary key auto_increment,
    title varchar(32),
    price int
);
create table author(
	id int primary key auto_increment,
    name varchar(32),
    age int
);
create table book2author(
	id int primary key auto_increment,
    author_id int,
    book_id int,
    foreign key(author_id) references author(id) 
    on update cascade  # 同步更新
    on delete cascade,  # 同步删除
    foreign key(book_id) references book(id) 
    on update cascade  # 同步更新
    on delete cascade  # 同步删除
);

🍕一对一

两张表:学生表和客户表

一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系

关联方式:foreign key+unique
create table authordetail(
	id int primary key auto_increment,
    phone int,
    addr varchar(64)
);
create table author(
	id int primary key auto_increment,
    name varchar(32),
    age int,
    authordetail_id int unique,
    foreign key(authordetail_id) references authordetail(id) 
    on update cascade  # 同步更新
    on delete cascade  # 同步删除
)

其他例子

例一:一个用户只有一个博客

    用户表:
    id  name
    1    egon
    2    alex
    3    wupeiqi


    博客表   
           fk+unique
    id url name_id
    1  xxxx   1
    2  yyyy   3
    3  zzz    2



例二:一个管理员唯一对应一个用户
    用户表:
    id user  password
    1  egon    xxxx
    2  alex    yyyy

    管理员表:
       fk+unique
    id user_id password
    1   1      xxxxx
    2   2      yyyyy

修改表(了解)

### MySQL对大小写是不敏感的
"""
1 修改表名
	alter table 表名 rename 新表名;

2 增加字段
	alter table 表名 add 字段名 字段类型(宽度)  约束条件;
	alter table 表名 add 字段名 字段类型(宽度)  约束条件 first;
	alter table 表名 add 字段名 字段类型(宽度)  约束条件 after 字段名;

3 删除字段
	alter table 表名 drop 字段名;

4 修改字段
	alter table 表名 modify 字段名 字段类型(宽度) 约束条件;
	
	alter table 表名 change 旧字段名 新字段名 字段类型(宽度) 约束条件;
	
"""

复制表(了解)

"""
我们sql语句查询的结果其实也是一张虚拟表
"""
create table 表名 select * from 旧表;  不能复制主键 外键 ...

create table new_dep2 select * from dep where id>3;
posted @ 2020-05-05 15:35  小子,你摊上事了  阅读(118)  评论(0)    收藏  举报