MySQL数据库篇之完整性约束和表关系

主要内容:

  一、完整性约束

  二、表关系

1?? 完整性约束

  (1)何为完整性约束?  

  约束条件与数据类型的宽度一样,都是可选参数。

  作用:用于保证数据的完整性和一致性

  (2)分类主要有以下五类:

    1、not null 与 default

    2、unique

    3、primary key

    4、auto_increment

    5、foreign key

PRIMARY KEY (PK)    标识该字段为该表的主键,可以唯一的标识记录
FOREIGN KEY (FK)    标识该字段为该表的外键
NOT NULL    标识该字段不能为空
UNIQUE KEY (UK)    标识该字段的值是唯一的
AUTO_INCREMENT    标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT    为该字段设置默认值

UNSIGNED 无符号
ZEROFILL 使用0填充

说明:

1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
sex enum(‘male‘,‘female‘) not null default ‘male‘
age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
3. 是否是key
主键 primary key
外键 foreign key
索引 (index,unique...)

  (3)not null 与 default 

    是否可空,null表示空,非字符串
    not null - 不可空
    null - 可空

    默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值,如下:
    create table tb1(
    nid int not null defalut 2,
    num int not null
    )

    not null的实例如下:

 ==================not null====================
        mysql> create table t1(id int); #id字段默认可以插入空
        mysql> desc t1;
        +-------+---------+------+-----+---------+-------+
        | Field | Type    | Null | Key | Default | Extra |
        +-------+---------+------+-----+---------+-------+
        | id    | int(11) | YES  |     | NULL    |       |
        +-------+---------+------+-----+---------+-------+
        mysql> insert into t1 values(); #可以插入空

        mysql> create table t2(id int not null); #设置字段id不为空
        mysql> desc t2;
        +-------+---------+------+-----+---------+-------+
        | Field | Type    | Null | Key | Default | Extra |
        +-------+---------+------+-----+---------+-------+
        | id    | int(11) | NO   |     | NULL    |       |
        +-------+---------+------+-----+---------+-------+
        mysql> insert into t2 values(); #不能插入空
        ERROR 1364 (HY000): Field ‘id‘ doesn‘t have a default value

    default的实例

==================default====================
        #设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
        mysql> create table t3(id int default 1);
        mysql> alter table t3 modify id int not null default 1;
        mysql> create table t8(
            -> id int,
            -> name char(6),
            -> sex enum(‘male‘,‘female‘) not null default ‘male‘
            -> );
        Query OK, 0 rows affected (0.41 sec)

        mysql> insert into t8(id,name) values(1,‘cc‘);
        Query OK, 1 row affected (0.08 sec)

        mysql> desc t8;
        +-------+-----------------------+------+-----+---------+-------+
        | Field | Type                  | Null | Key | Default | Extra |
        +-------+-----------------------+------+-----+---------+-------+
        | id    | int(11)               | YES  |     | NULL    |       |
        | name  | char(6)               | YES  |     | NULL    |       |
        | sex   | enum(‘male‘,‘female‘) | NO   |     | male    |       |
        +-------+-----------------------+------+-----+---------+-------+
        3 rows in set (0.01 sec)

        mysql> select * from t8;
        +------+------+------+
        | id   | name | sex  |
        +------+------+------+
        |    1 | cc   | male |
        +------+------+------+
        1 row in set (0.00 sec)
        mysql> insert into department values
            -> (1,‘student‘),
            -> (2,‘school‘);
        Query OK, 2 rows affected (0.06 sec)
        Records: 2  Duplicates: 0  Warnings: 0

  

  (4) unique 

    单列唯一

      方法一:

 mysql> create table department(
            -> id int unique,
            -> name char(10) unique
            -> );
        Query OK, 0 rows affected (0.59 sec)

      方法二:

  mysql> create table department(
            -> id int,
            -> name char(10),
            -> unique(id),
            -> unique(name)
            -> );
        Query OK, 0 rows affected (0.49 sec)

    联合唯一(一组里的某个参数不同即可)

 mysql> create table services(
            -> id int unique,
            -> ip char(14),
            -> port int,
            -> unique(ip,port)
            -> );
        Query OK, 0 rows affected (0.48 sec)

        mysql> desc services;
        +-------+----------+------+-----+---------+-------+
        | Field | Type     | Null | Key | Default | Extra |
        +-------+----------+------+-----+---------+-------+
        | id    | int(11)  | YES  | UNI | NULL    |       |
        | ip    | char(14) | YES  | MUL | NULL    |       |
        | port  | int(11)  | YES  |     | NULL    |       |
        +-------+----------+------+-----+---------+-------+
        3 rows in set (0.06 sec)

        mysql> insert into services values
            -> (1,‘192.168.0.1‘,80),
            -> (2,‘192.168.0.1‘,81),
            -> (2,‘192.168.0.2‘,81);
        ERROR 1062 (23000): Duplicate entry ‘2‘ for key ‘id‘
        mysql> insert into services values
            -> (1,‘192.168.0.1‘,80),
            -> (2,‘192.168.0.1‘,81),
            -> (3,‘192.168.0.2‘,81);
        Query OK, 3 rows affected (0.10 sec)
        Records: 3  Duplicates: 0  Warnings: 0

        mysql> select * from services;
        +------+-------------+------+
        | id   | ip          | port |
        +------+-------------+------+
        |    1 | 192.168.0.1 |   80 |
        |    2 | 192.168.0.1 |   81 |
        |    3 | 192.168.0.2 |   81 |
        +------+-------------+------+
        3 rows in set (0.00 sec)

  (5)primary key --> not  null  unique   

  primary key字段的值不为空且唯一  约束:not null nique  存储引擎(innodb):对于innodb存储引擎来说,一张表必须有一个主键  一个表中可以:      单列做主键      多列做主键(复合主键)      但一个表内只能有一个主键primary key

  单列主键:  

 mysql> create table t9(
                -> id int primary key,
                -> name char(13)
                -> );
            Query OK, 0 rows affected (0.44 sec)

            mysql> desc t9;
            +-------+----------+------+-----+---------+-------+
            | Field | Type     | Null | Key | Default | Extra |
            +-------+----------+------+-----+---------+-------+
            | id    | int(11)  | NO   | PRI | NULL    |       |
            | name  | char(13) | YES  |     | NULL    |       |
            +-------+----------+------+-----+---------+-------+
            2 rows in set (0.01 sec)

            mysql> insert into t9 values
                -> (1,‘cc‘),
                -> (2,‘cc2‘);
            Query OK, 2 rows affected (0.07 sec)
            Records: 2  Duplicates: 0  Warnings: 0

            mysql> insert into t9 values
                -> (2,‘cc3‘);
            ERROR 1062 (23000): Duplicate entry ‘2‘ for key ‘PRIMARY‘

  多列主键

 mysql> create table t10(
                -> ip char(13),
                -> port int,
                -> primary key(ip,port)
                -> );
            Query OK, 0 rows affected (0.41 sec)

            mysql> desc t9;
            +-------+----------+------+-----+---------+-------+
            | Field | Type     | Null | Key | Default | Extra |
            +-------+----------+------+-----+---------+-------+
            | id    | int(11)  | NO   | PRI | NULL    |       |
            | name  | char(13) | YES  |     | NULL    |       |
            +-------+----------+------+-----+---------+-------+
            2 rows in set (0.00 sec)

            mysql> desc t10;
            +-------+----------+------+-----+---------+-------+
            | Field | Type     | Null | Key | Default | Extra |
            +-------+----------+------+-----+---------+-------+
            | ip    | char(13) | NO   | PRI |         |       |
            | port  | int(11)  | NO   | PRI | 0       |       |
            +-------+----------+------+-----+---------+-------+
            2 rows in set (0.01 sec)

            mysql> insert into t10 values(
                -> ‘1.1.1.1‘,88),
                -> (‘1.1.1.1‘,88);
            ERROR 1062 (23000): Duplicate entry ‘1.1.1.1-88‘ for key ‘PRIMARY‘
            mysql> insert into t10 values(
                -> ‘1.1.1.1‘,88),
                -> (‘1.1.1.1‘,89);
            Query OK, 2 rows affected (0.06 sec)
            Records: 2  Duplicates: 0  Warnings: 0

            mysql> select * from t10;
            +---------+------+
            | ip      | port |
            +---------+------+
            | 1.1.1.1 |   88 |
            | 1.1.1.1 |   89 |
            +---------+------+
            2 rows in set (0.00 sec)

  

  (6) auto_increment

  约束字段为自动增长,被约束的字段必须被key约束。

 mysql> create table t11(
                -> id int primary key auto_increment,
                -> name char(12)
                -> );
            Query OK, 0 rows affected (0.56 sec)

            mysql> insert into t11(name) values
                -> (‘cc1‘),
                -> (‘cc2‘),
                -> (‘cc3‘),
                -> (‘cc4‘);
            Query OK, 4 rows affected (0.08 sec)
            Records: 4  Duplicates: 0  Warnings: 0

            mysql> select * from t11;
            +----+------+
            | id | name |
            +----+------+
            |  1 | cc1  |
            |  2 | cc2  |
            |  3 | cc3  |
            |  4 | cc4  |
            +----+------+
            4 rows in set (0.00 sec)

            mysql> insert into t11(name) values
                -> (‘qq1‘),
                -> (‘qq2‘),
                -> (‘qq3‘);
            Query OK, 3 rows affected (0.09 sec)
            Records: 3  Duplicates: 0  Warnings: 0

            mysql> select * from t11;
            +----+------+
            | id | name |
            +----+------+
            |  1 | cc1  |
            |  2 | cc2  |
            |  3 | cc3  |
            |  4 | cc4  |
            |  5 | qq1  |
            |  6 | qq2  |
            |  7 | qq3  |
            +----+------+
            7 rows in set (0.00 sec)
            了解:
                mysql> show variables like ‘auto_inc%‘;
                +--------------------------+-------+
                | Variable_name            | Value |
                +--------------------------+-------+
                | auto_increment_increment | 1     |
                | auto_increment_offset    | 1     |
                +--------------------------+-------+
                2 rows in set (0.00 sec)
                #基于会话级别
                    set session auth_increment_increment=2 #修改会话级别的步长
                #基于全局级别的
                    set global auth_increment_increment=2 #修改全局级别的步长(所有会话都生效)

                # 步长:
                    auto_increment_increment默认为1
                    设置步长
                        mysql> set session auto_increment_increment = 5; # 零时
                        Query OK, 0 rows affected (0.00 sec)

                        mysql> set global auto_increment_increment=5; # 全局
                        Query OK, 0 rows affected (0.00 sec)
                        mysql> show variables like "auto_inc%";
                        +--------------------------+-------+
                        | Variable_name            | Value |
                        +--------------------------+-------+
                        | auto_increment_increment | 5     |
                        | auto_increment_offset    | 1     |
                        +--------------------------+-------+
                        2 rows in set (0.00 sec)

                #起始偏移量(起始偏移量 <= 步长)
                    auto_increment_offset 默认1
                    set global auto_increment_office

                    mysql> create table t12(
                        -> id int primary key auto_increment,
                        -> name char(18)
                        -> );
                    Query OK, 0 rows affected (0.50 sec)
                    mysql> insert into t12(name) values
                        -> (‘cc‘),
                        -> (‘sc‘);
                    Query OK, 2 rows affected (0.07 sec)
                    Records: 2  Duplicates: 0  Warnings: 0

                    mysql> select * from t12;
                    +----+------+
                    | id | name |
                    +----+------+
                    |  1 | cc   |
                    |  6 | sc   |
                    +----+------+
                    2 rows in set (0.00 sec)
                    使用极少,我们需要改回来
                    mysql> set global auto_increment_increment=1;
                    Query OK, 0 rows affected (0.00 sec)

                    mysql> set global auto_increment_offset=1;
                    Query OK, 0 rows affected (0.00 sec)

            清空表数据 :truncate
            mysql> select * from t12;
                +----+------+
                | id | name |
                +----+------+
                |  1 | cc   |
                |  6 | sc   |
                +----+------+
                2 rows in set (0.00 sec)

                mysql> truncate t12;
                Query OK, 0 rows affected (0.31 sec)

                mysql> select * from t12;

  (7)foreign key(外键),建立表之间的关系

    如何理解foreign key?

    假如员工信息表有三个字段:工号 姓名 部门

    公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费。该如何解决这个问题呢?

    解决办法:     

      我们可以先定义一个部门表,然后让员工信息表关联该表,如何关联,即使用 foreign key

    注意:

      表类型必须是innodb存储引擎,且被关联的字段,即references指定的另外一个表的字段,必须保证唯一。

    第一步,先建立被关联的表,并且保证被关联的子段唯一,此处的被关联的表是部门表(dep)如下:   

 mysql> create table dep(   # 父表,即被关联的表
            -> id int primary key,
            -> name char(20),
            -> comment char(16)
            -> );
        Query OK, 0 rows affected (0.42 sec)
        mysql> desc dep;
        +---------+----------+------+-----+---------+-------+
        | Field   | Type     | Null | Key | Default | Extra |
        +---------+----------+------+-----+---------+-------+
        | id      | int(11)  | NO   | PRI | NULL    |       |
        | name    | char(20) | YES  |     | NULL    |       |
        | comment | char(16) | YES  |     | NULL    |       |
        +---------+----------+------+-----+---------+-------+
        3 rows in set (0.01 sec)

    第二步,建立关联的表(此处指员工表),如下:  

  mysql> create table emp(   # 子表
            -> id int primary key,
            -> name char(13),
            -> sex enum(‘male‘,‘female‘),
            -> dep_id int,
            -> foreign key(dep_id) references dep(id)
            -> );
        Query OK, 0 rows affected (0.54 sec)
        mysql> desc emp;
        +--------+-----------------------+------+-----+---------+-------+
        | Field  | Type                  | Null | Key | Default | Extra |
        +--------+-----------------------+------+-----+---------+-------+
        | id     | int(11)               | NO   | PRI | NULL    |       |
        | name   | char(13)              | YES  |     | NULL    |       |
        | sex    | enum(‘male‘,‘female‘) | YES  |     | NULL    |       |
        | dep_id | int(11)               | YES  | MUL | NULL    |       |
        +--------+-----------------------+------+-----+---------+-------+
        4 rows in set (0.01 sec)

    第三步,先向被关联表中插入数据,再向关联表中插入数据(即先向部门表中插入数据,再向员工表插入数据)

 mysql> insert into dep values
            -> (1,‘销售‘,‘部门1‘),
            -> (2,‘财务‘,‘部门2‘),
            -> (3,‘售后‘,‘部门3‘);
        Query OK, 3 rows affected (0.07 sec)
        Records: 3  Duplicates: 0  Warnings: 0

        mysql> insert into emp values
            -> (1,‘cc‘,‘male‘,1),
            -> (2,‘sc‘,‘male‘,2),
            -> (3,‘ssc‘,‘female‘,3),
            -> (4,‘sscc‘,‘female‘,3);
        Query OK, 4 rows affected (0.06 sec)
        Records: 4  Duplicates: 0  Warnings: 0

        mysql> select * from emp
            -> ;
        +----+------+--------+--------+
        | id | name | sex    | dep_id |
        +----+------+--------+--------+
        |  1 | cc   | male   |      1 |
        |  2 | sc   | male   |      2 |
        |  3 | ssc  | female |      3 |
        |  4 | sscc | female |      3 |
        +----+------+--------+--------+
        4 rows in set (0.00 sec)
        mysql> select * from dep;
        +----+--------+---------+
        | id | name   | comment |
        +----+--------+---------+
        |  1 | 销售   | 部门1   |
        |  2 | 财务   | 部门2   |
        |  3 | 售后   | 部门3   |
        +----+--------+---------+
        3 rows in set (0.00 sec)

    第四步(删除数据才需要),先删除关联表中的数据,再删除被关联表中的数据

 mysql> delete from emp where dep_id=3;
        Query OK, 2 rows affected (0.06 sec)
        mysql> select * from emp;
        +----+------+------+--------+
        | id | name | sex  | dep_id |
        +----+------+------+--------+
        |  1 | cc   | male |      1 |
        |  2 | sc   | male |      2 |
        +----+------+------+--------+
        2 rows in set (0.00 sec)

        mysql> delete from dep where id=3;
        Query OK, 1 row affected (0.07 sec)
        mysql> select * from dep;
        +----+--------+---------+
        | id | name   | comment |
        +----+--------+---------+
        |  1 | 销售   | 部门1   |
        |  2 | 财务   | 部门2   |
        +----+--------+---------+
        2 rows in set (0.00 sec)

  注意:当表中包含 FOREIGN KEY约束时,插入数据要先被关联表,后关联表;而要删除数据时,

  顺序正好相反,先删除关联表中的数据,后删除被关联表中的数据。切记切记!!!

综合案例: 

 mysql> create table dep2(  # 被关联表
            -> id int primary key,
            -> name varchar(15) not null
            -> )engine=innodb;
        Query OK, 0 rows affected (0.36 sec)

        mysql> create table emp2( # 关联表
            -> id int primary key,
            -> name varchar(13) not null,
            -> dep2_id int ,
            -> foreign key(dep2_id) references dep2(id)
            -> on delete cascade  # 添加之后,可单独删除关联表数据
            -> on update cascade  # 添加之后,可单独更新关联表数据
            -> )engine=innodb;
        Query OK, 0 rows affected (0.36 sec)

        mysql> insert into dep2 values
            -> (1,‘部门1‘),
            -> (2,‘部门2‘),
            -> (3,‘部门3‘);
        Query OK, 3 rows affected (0.08 sec)
        Records: 3  Duplicates: 0  Warnings: 0

        mysql> insert into emp2 values
            -> (1,‘cc1‘,1),
            -> (2,‘cc2‘,2),
            -> (3,‘cc2‘,2),
            -> (4,‘cc2‘,3);
        Query OK, 4 rows affected (0.03 sec)
        Records: 4  Duplicates: 0  Warnings: 0

        mysql> delete from emp2 where id=4;
            Query OK, 1 row affected (0.04 sec)

            mysql> delete from emp2 where id=3;
            Query OK, 1 row affected (0.07 sec)

            mysql> select * from emp2;
            +----+------+---------+
            | id | name | dep2_id |
            +----+------+---------+
            |  1 | cc1  |       1 |
            |  2 | cc2  |       2 |
            +----+------+---------+
            2 rows in set (0.00 sec)

            mysql> delete from dep2 where id=3;
            Query OK, 1 row affected (0.07 sec)

            mysql> select * from dep2;
            +----+---------+
            | id | name    |
            +----+---------+
            |  1 | 部门1   |
            |  2 | 部门2   |
            +----+---------+
            2 rows in set (0.00 sec)
            mysql> delete from  dep2 where id=2;
            Query OK, 1 row affected (0.15 sec)

            mysql> select * from emp2;
            +----+------+---------+
            | id | name | dep2_id |
            +----+------+---------+
            |  1 | cc1  |       1 |
            +----+------+---------+
            1 row in set (0.00 sec)

            mysql> update emp2 set id=666 where id=1;
            Query OK, 1 row affected (0.09 sec)
            Rows matched: 1  Changed: 1  Warnings: 0

            mysql> select * from emp2;
            +-----+------+---------+
            | id  | name | dep2_id |
            +-----+------+---------+
            | 666 | cc1  |       1 |
            +-----+------+---------+
            1 row in set (0.00 sec)

  清空表:

delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

truncate table t1; #数据量大,删除速度比上一条快,且直接从零开始,

2?? 表关系

  1、如何分析两张表间的关系?

  实例如下,分析下面两张表:

emp    +----+------+--------+--------+
        | id | name | sex    | dep_id |
        +----+------+--------+--------+
        |  1 | cc   | male   |      1 |p
        |  2 | sc   | male   |      2 |
        |  3 | ssc  | female |      3 |
        |  4 | sscc | female |      3 |
        +----+------+--------+--------+
dep
        +----+--------+---------+
        | id | name   | comment |
        +----+--------+---------+
        |  1 | 销售   | 部门1   |
        |  2 | 财务   | 部门2   |
        |  3 | 售后   | 部门3   |
        +----+--------+---------+

分析流程如下:

  (1)先站在左表的角度去找

是否左表的多条记录可以对应右表的一条记录,如果是,则证明左表的一个字段foreign key 右表一个字段(通常是id)

  (2)再站在右表的角度去找

是否右表的多条记录可以对应左表的一条记录,如果是,则证明右表的一个字段foreign key 左表一个字段(通常是id)

  (3)总结:

#多对一:
如果只有步骤1成立,则是左表多对一右表
如果只有步骤2成立,则是右表多对一左表

#多对多
如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来专门存放二者的关系

#一对一:
如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可

  2、建立表关系

    2.1、多对一(一对多)

    两张表:出版社、书

    关联方式:foreign key

  create table press(
        id int primary key auto_increment,
        name varchar(20)
        );
        create table book(
        id int primary key auto_increment,
        name varchar(20),
        press_id int not null,
        foreign key(press_id) references press(id)
        on delete cascade
        on update cascade
        );
        insert into press(name) values
        (‘北京出版社‘),
        (‘知识产权无用出版社‘) ;
        insert into book(name,press_id) values
        (‘九阳神功‘,1),
        (‘九阴真经‘,1),
        (‘葵花宝典‘,2);
        (‘独孤九剑‘,2);

   

     2.2、多对多

    三张表:出版社,作者信息,书
    多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多   
    如果步骤1和2同时成立,则证明这两张表时一个双向的多对一,即多对多,需要定义一个这两张表的关系表来  专门存放二者的关系。
    关联方式:foreign key+一张新的表(出版社信息同上,不再追加)

  create table author(
            id int primary key auto_increment,
            name varchar(20)
            );
            书籍表和插入的书与之前共用
            create table author2book(
                id int not null unique auto_increment,
                author_id int not null,
                book_id int not null,
                (constraint fk_author) foreign key(author_id) references author(id)
                on delete cascade,
                on update cascade,
                (constraint fk_book) foreign key(book_id) references book(id)
                on delete cascade
                on update cascade,
                primary key(author_id,book_id)
                );
            insert into autor(name) values(‘cc1‘),(‘cc2‘),(‘cc3‘);
            #每个作者与自己的代表作如下
            cc1:
            九阳神功
            九阴真经
            cc2:
            九阳神功
            葵花宝典
            cc3:
            独孤九剑
            insert into author2book(author_id,book_id) values
                (1,1),
                (1,1),
                (2,1),
                (2,2),
                (3,2);

   

   2.3、一对一(一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系)

 如果1和2都不成立,而是左表的一条记录唯一对应右表的一条记录,反之亦然。这种情况很简单,
        就是在左表foreign key右表的基础上,将左表的外键字段设置成unique即可
        #一定是student来foreign key表customer,这样就保证了:
        #1 学生一定是一个客户,
        #2 客户不一定是学生,但有可能成为一个学生

   create table customer(
            id int primary key auto_increment,
            name varchar(20) not null,
            phone char(16) not null
            );
            create table student(
            id int primary key auto_increment,
            class_name varchar(20) not null,
            customer_id int unique, #该字段一定要是唯一的
            foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
            on delete cascade
            on update cascade
            );
            # 增加客户
            insert into customer(name,phone)
                (‘cc‘,12312312),
                (‘cc2‘,213231231),
                (‘cc3‘,66666666);
            # 增加学生
             insert into student(class_name,customer_id) values
             (‘class1‘,1),
             (‘class2‘,2),
             (‘class3‘,3);

 


原文地址:https://www.cnblogs.com/schut/p/9060992.html

时间: 2024-08-13 22:17:00

MySQL数据库篇之完整性约束和表关系的相关文章

MySQL数据库8(十一)表关系

表关系 表关系:表与表之间(实体)有什么样的关系,每种关系应该如何设计表结构. 一对一 一对一,一张表中的一条记录与另外一张表中最多有一条明确的关系,通常,此设计方案保证两张表中使用同样的主键即可.(唯一键可以为null,无法匹配) 学生表: 学生ID(PRI) 姓名 年龄 性别 籍贯 婚否 信仰 表的使用过程中,常用的信息会经常去查询,而不常用的信息会偶尔才会用到. 解决方案:将表拆分,常见的放一张表,不常见的放一张表. 常用表 学生ID(PRI) 姓名 年龄 性别 不常用表 学生ID(PRI

mysql数据库同步可以针对单张表同步吗?解决办法

mysql数据库同步可以针对单张表同步吗?第一个问题:mysql数据库同步可以针对单张表同步吗? 也就是说1.数据库A里面有a1,a2,a3三张表,数据库B里面只有一张a1表(A与B的a1的表结构式一样的)2.数据库B只同步数据库A中的a1表3.数据库A也要同步B的a1表 注:其实可以认为是数据库A与数据库B的a1表双向同步,即A数据库a1数据有改变,,B数据库a1也要相应改变,同理B数据库a1有改变,A数据库a1也要改变(Aa1<=>Ba1) 第二个问题:配置数据库同步linux与windo

Django使用Mysql数据库已经存在的数据表方法

源地址:https://www.jb51.net/article/140939.htm 使用scrapy爬取了网上的一些数据,存储在了mysql数据库中,想使用Django将数据展示出来,在网上看到都是使用Django的models和makemigration,migrate命令来创建新表,并使用. 可是我的数据已经存在了已经创建好,并且已经存储有数据了,不能再重新创建新表了. 了解Django的表明和models名称的映射关系就可以让Django使用已经存在的表. 假如在Django存在mod

Django中的app及mysql数据库篇(ORM操作)

Django常见命令 在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目.启动Django程序.创建新的APP.数据库迁移等. 创建Django项目 一把我们都新建一个文件夹来存放项目文件,切换到这个目录下,启动命令行工具.创建一个名为mysite的Django项目: django-admin startproject mysite 创建好项目之后,可以查看当前目录下多出一个名为mysite的文件夹,mysite的文件夹目录结构如下: mysite/ ma

mysql数据库---同时插入两个表以上的数据

mysql数据库问题,如何同一个操作添加两个表(a表,b表),并把b表的id添加到a表字段中,b表id自动增长 在数据库中创建存储过程,比如存储过程的名字叫做 test在java中和正常使用sql的方法一样, sql = "call test(B1,B2)", B1和B2是假设你将要插入b表中的数据 假设b表的结构是(ID int auto_incremet, B1 int, B2 int)假设a表的结构是(ID int) CREATE test(B1 int, B2 int) #假设

Mysql数据库常用分库和分表方式

1 分库 1.1 按照功能分库 按照功能进行分库.常见的分成6大库: 1 用户类库:用于保存了用户的相关信息.例如:db_user,db_system,db_company等. 2 业务类库:用于保存主要业务的信息.比如主要业务是笑话,用这个库保存笑话业务.例如:db_joke,db_temp_joke等. 3 内存类库:主要用Mysql的内存引擎.前台的数据从内存库中查找,速度快.例如:heap. 4 图片类库:主要保存图片的索引以及关联.例如:db_img_index,db_img_res.

MySQL 数据库中如何把A表的数据插入到B表?

web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节省大量代码. 以mysql数据库为例分情况一一说明: 两张表:insertTest和insertTest2,前者中有测试数据 create table insertTest(id int(4),name varchar(12));insert into insertTest values(100,'tom')

MySQL数据库篇之索引原理与慢查询优化之二

接上篇 7??  正确使用索引 一.索引未命中 并不是说我们创建了索引就一定会加快查询速度,若想利用索引达到预想的提高查询速度的效果, 我们在添加索引时,必须遵循以下问题: #1 范围问题,或者说条件不明确,条件中出现这些符号或关键字:>.>=.<.<=.!= .between...and....like. #2 尽量选择区分度高的列作为索引,区分度的公式是count(distinct col)/count(*),表示字段不重复的比例,比例越大我们扫描的记录数越少,唯一键的区分度是

MySQL数据库 外键,级联, 修改表的操作

1.外键: 用来建立两张表之间的关系 - 一对多 - 多对多 - 一对一 研究表与表之间的关系: 1.定义一张 员工部门表 id, name, gender, dep_name, dep_desc - 将所有数据存放在一张表中的弊端: 1.结构不清晰 ---> 不致命 2.浪费空间 ---> 不致命 3.可扩展性极差 ---> 不可忽视的弊端 - 类似于将所有python代码存放在一个py文件中,强耦合到一起了----> 解耦合 ----> 拆分表 - 拆分表解决以上问题.