[MYSQL / Mariadb]数据库学习-表结构、键值(普通索引、主键、外键)

news2024/11/24 22:54:50

数据库学习-表结构、键值

  • 回顾数据类型
  • 表结构
    • 字段约束条件(限制字段赋值的)
      • 例,建表并设置约束条件
      • 设置default列为0
    • 修改表结构
      • 例添加新字段,并设置位置。
      • 修改字段类型
      • change 修改字段名
      • 注意:在修改字段名或字段类型时,如果此时相应列已有数据,会报错。!!!要修改的内容,不能与已有的值冲突!!!
      • drop 删字段(列)
      • 修改表名
  • MySQL的键值,key(标签)
    • 索引类型(排列方式)
    • 普通索引 ( 索引:类似书的目录,让列的值排队)
      • 在创建表的同时,添加索引
      • 查索引情况
      • 删索引
    • 主键
      • 建表时,定义主键的方式
      • 修改、删除主键。!!注:取消主键后的列,‘null’列为NO,YES 要手动改!!
      • 如果字段下面的值,有相同的,则无法创建主键!!!
    • 复合主键(表里的多列一起定义为主键,复合主键的值不允许同时一样!!)
      • 修改、删除主键
    • 主键与auto_increment(自动增长+1)连用。需要字段为主键,列的类型为数值类型!!!
      • 按唯一编号,去查找信息
      • 有自增长的主键,要先删掉自增长才能删主键。2、按上述表状态就算把表情空,再添加记录,编号也会按清空前的最后一位+1。3、要想重新排序,删字段再重建。
    • 外键
      • 建员工和工资表,指定外键工资列的id与员工表的对应,同步。
      • 验证是否有外键:
      • 添加不在条件范围内的,会报错!!
      • 验证,同步删除的效果
      • 修改,同步效果
      • 删外键:
      • 扩展,外键可存空值,这于表的设计思路不符,需要进行控制。把目标外键设置为主键,可避免为空和重复。

回顾数据类型

mysql> create table test9 ( name char(10), age tinyint(3) unsigned, address varchar(50), pay float(7,2), start year, birthday date,likes set('eat','game','film','music'), sex enum('boy','girl'));
Query OK, 0 rows affected, 2 warnings (0.46 sec)

mysql> desc test9;
+----------+----------------------------------+------+-----+---------+-------+
| Field    | Type                             | Null | Key | Default | Extra |
+----------+----------------------------------+------+-----+---------+-------+
| name     | char(10)                         | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned              | YES  |     | NULL    |       |
| address  | varchar(50)                      | YES  |     | NULL    |       |
| pay      | float(7,2)                       | YES  |     | NULL    |       |
| start    | year(4)                          | YES  |     | NULL    |       |
| birthday | date                             | YES  |     | NULL    |       |
| likes    | set('eat','game','film','music') | YES  |     | NULL    |       |
| sex      | enum('boy','girl')               | YES  |     | NULL    |       |
+----------+----------------------------------+------+-----+---------+-------+
8 rows in set (0.01 sec)



mysql> insert into test9 values('lisi',16,'shenzhen-baoan-xixiang',782.53,1997,date(now()),'game,film','boy');
Query OK, 1 row affected (0.06 sec)

mysql> select * from test9;
+------+------+------------------------+--------+-------+------------+-----------+------+
| name | age  | address                | pay    | start | birthday   | likes     | sex  |
+------+------+------------------------+--------+-------+------------+-----------+------+
| lisi |   16 | shenzhen-baoan-xixiang | 782.53 |  1997 | 2023-05-06 | game,film | boy  |
+------+------+------------------------+--------+-------+------------+-----------+------+
1 row in set (0.03 sec)

表结构

字段约束条件(限制字段赋值的)

请添加图片描述

NULL: 是否允许赋空值,默认允许,可设置
列名 类型 not null;
.
key: 键值
.
default : 默认值,向表里添加新行时,不给列赋值,会使用默认值赋值
列名 类型 default 值;
.

mysql> desc test11;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| money | float(8,3) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
1 row in set (0.01 sec)

mysql>  desc mysql.db;
+-----------------------+---------------+------+-----+---------+-------+
| Field                 | Type          | Null | Key | Default | Extra |
+-----------------------+---------------+------+-----+---------+-------+
| Host                  | char(255)     | NO   | PRI |         |       |
| Db                    | char(64)      | NO   | PRI |         |       |
| User                  | char(32)      | NO   | PRI |         |       |
| Select_priv           | enum('N','Y') | NO   |     | N       |       |
| Insert_priv           | enum('N','Y') | NO   |     | N       |       |
| Update_priv           | enum('N','Y') | NO   |     | N       |       |
| Delete_priv           | enum('N','Y') | NO   |     | N       |       |
| Create_priv           | enum('N','Y') | NO   |     | N       |       |
| Drop_priv             | enum('N','Y') | NO   |     | N       |       |

例,建表并设置约束条件

mysql> create table test10 (name char(10) not null, age tinyint(3) unsigned default "27", class char(10) not null default "class2021", up_time time );
Query OK, 0 rows affected, 1 warning (0.21 sec)

mysql> desc test10;
+---------+---------------------+------+-----+-----------+-------+
| Field   | Type                | Null | Key | Default   | Extra |
+---------+---------------------+------+-----+-----------+-------+
| name    | char(10)            | NO   |     | NULL      |       |
| age     | tinyint(3) unsigned | YES  |     | 27        |       |
| class   | char(10)            | NO   |     | class2021 |       |
| up_time | time                | YES  |     | NULL      |       |
+---------+---------------------+------+-----+-----------+-------+
4 rows in set (0.01 sec)



加表记录
mysql> insert into test10(name,up_time) values("bob",curtime());
Query OK, 1 row affected (0.04 sec)

mysql> insert into test10(name,up_time) values("bob",time(152325));
Query OK, 1 row affected (0.10 sec)


mysql> select * from test10;
+------+------+-----------+----------+
| name | age  | class     | up_time  |
+------+------+-----------+----------+
| bob  |   27 | class2021 | 20:52:27 |
| bob  |   27 | class2021 | 15:23:25 |
+------+------+-----------+----------+
2 rows in set (0.00 sec)



mysql> insert into test10 values("tom",null,"class2105",122345);

mysql> insert into test10 values("tom",22,"null",122345);



注:以下这两种情况,都不为空:
mysql> insert into test10(name) values("");  
#这里直接双引号,为0个字符,不是空
mysql> insert into test10(name) values("null");

mysql> insert into test10(name) values(null);
ERROR 1048 (23000): Column 'name' cannot be null


mysql> select * from test10;
+------+------+-----------+----------+
| name | age  | class     | up_time  |
+------+------+-----------+----------+
| bob  |   27 | class2021 | 20:52:27 |
| bob  |   27 | class2021 | 15:23:25 |
| tom  | NULL | class2105 | 12:23:45 |
| tom  |   22 | null      | 12:23:45 |
|      |   27 | class2021 | NULL     |
| null |   27 | class2021 | NULL     |
+------+------+-----------+----------+
6 rows in set (0.00 sec)

设置default列为0

mysql> create table test11 (
    -> name char(5) not null default "",
    -> age tinyint(3) default 25
    -> );
Query OK, 0 rows affected, 1 warning (0.13 sec)

mysql> desc test11;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| name  | char(5)    | NO   |     |         |       |
| age   | tinyint(3) | YES  |     | 25      |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)


mysql> insert into test11 values ('bob',null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test11(name) values ('bob');

mysql> select * from test11;
+------+------+
| name | age  |
+------+------+
| bob  | NULL |
| bob  |   25 |
+------+------+
2 rows in set (0.00 sec)



修改表结构

请添加图片描述

请添加图片描述

例添加新字段,并设置位置。

不定义位置,默认在行尾。
定义到开头,加 first
定义到xx行后,为 after xx

mysql> alter table test11 add address char(20);
Query OK, 0 rows affected (0.45 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from test11;
+--------+------+---------+
| name   | age  | address |
+--------+------+---------+
| bob    | NULL | NULL    |
| bob    |   25 | NULL    |
| 小明   |   25 | NULL    |
+--------+------+---------+
3 rows in set (0.00 sec)


mysql> alter table test11 add hobby char(20) after age, add sex enum('boy','girl') first;
# 不定义位置,默认在行尾。
# 定义到开头,加 first
# 定义到xx行后,为 after  xx  
Query OK, 0 rows affected (0.70 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test11;
+---------+--------------------+------+-----+---------+-------+
| Field   | Type               | Null | Key | Default | Extra |
+---------+--------------------+------+-----+---------+-------+
| sex     | enum('boy','girl') | YES  |     | NULL    |       |
| name    | char(5)            | NO   |     |         |       |
| age     | tinyint(3)         | YES  |     | 25      |       |
| hobby   | char(20)           | YES  |     | NULL    |       |
| address | char(20)           | YES  |     | NULL    |       |
+---------+--------------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

修改字段类型

请添加图片描述

mysql> alter table test11 modify hobby set('music','film','sleep','write');
Query OK, 3 rows affected (0.48 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> desc test11;
+---------+-------------------------------------+------+-----+---------+-------+
| Field   | Type                                | Null | Key | Default | Extra |
+---------+-------------------------------------+------+-----+---------+-------+
| sex     | enum('boy','girl')                  | YES  |     | NULL    |       |
| name    | char(5)                             | NO   |     |         |       |
| age     | tinyint(3)                          | YES  |     | 25      |       |
| hobby   | set('music','film','sleep','write') | YES  |     | NULL    |       |
| address | char(20)                            | YES  |     | NULL    |       |
+---------+-------------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

change 修改字段名

请添加图片描述

mysql> alter table test11 change  hobby likes set('music','film','sleep','write');
Query OK, 0 rows affected (0.33 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test11;
+---------+-------------------------------------+------+-----+---------+-------+
| Field   | Type                                | Null | Key | Default | Extra |
+---------+-------------------------------------+------+-----+---------+-------+
| sex     | enum('boy','girl')                  | YES  |     | NULL    |       |
| name    | char(5)                             | NO   |     |         |       |
| age     | tinyint(3)                          | YES  |     | 25      |       |
| likes   | set('music','film','sleep','write') | YES  |     | NULL    |       |
| address | char(20)                            | YES  |     | NULL    |       |
+---------+-------------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

注意:在修改字段名或字段类型时,如果此时相应列已有数据,会报错。!!!要修改的内容,不能与已有的值冲突!!!

mysql> insert into test11 values ('girl','lili',36,'music','shenzhen');
Query OK, 1 row affected (0.09 sec)

mysql> select * from test11;
+------+--------+------+-------+----------+
| sex  | name   | age  | likes | address  |
+------+--------+------+-------+----------+
| NULL | bob    | NULL | NULL  | NULL     |
| NULL | bob    |   25 | NULL  | NULL     |
| NULL | 小明   |   25 | NULL  | NULL     |
| girl | lili   |   36 | music | shenzhen |
+------+--------+------+-------+----------+
4 rows in set (0.00 sec)

mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| sex     | enum('boy','girl')                     | YES  |     | NULL    |       |
| name    | char(5)                                | NO   |     |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('music','film','sleep','write')    | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)


# 这里要改喜好字段,因为喜欢列已经有了music的值,所以更改的内容里,不能与其冲突。再改不能把music去掉。
mysql> alter table test11 modify likes set('write','read','game');
ERROR 1265 (01000): Data truncated for column 'likes' at row 4

mysql> alter table test11 modify likes set('write','read','game','music');
Query OK, 4 rows affected (0.27 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| sex     | enum('boy','girl')                     | YES  |     | NULL    |       |
| name    | char(5)                                | NO   |     |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('write','read','game','music')     | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

drop 删字段(列)

请添加图片描述

mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| sex     | enum('boy','girl')                     | YES  |     | NULL    |       |
| name    | char(5)                                | NO   |     |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('write','read','game','music')     | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

mysql> alter table test11 drop sex;
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| name    | char(5)                                | NO   |     |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('write','read','game','music')     | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

修改表名

mysql>  alter table test11 rename about_key;
Query OK, 0 rows affected (0.14 sec)

mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| about_key     |
| st1           |
| st2           |

MySQL的键值,key(标签)

普通索引(index)
主键(primary key)
外键(foreign key)
唯一索引(unique)
全文索引(full text)
.
查看标签:desc 库.表
查看标签详细信息: show index from 库.表

索引类型(排列方式)

二叉树:btree
B+tree
hash

请添加图片描述
请添加图片描述

普通索引 ( 索引:类似书的目录,让列的值排队)

索引:
优:加快查找速度
缺:占用硬盘空间,写数据的速度会慢

请添加图片描述
请添加图片描述
请添加图片描述

mysql> create index haha on test11(name);
Query OK, 0 rows affected (0.29 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| name    | char(5)                                | NO   | MUL |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('write','read','game','music')     | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)


在创建表的同时,添加索引

mysql> create table test12 (name char(5),id char(20), index(name));
Query OK, 0 rows affected (0.21 sec)

mysql> desc test12;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(5)  | YES  | MUL | NULL    |       |
| id    | char(20) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

查索引情况

mysql> create index hehe on test11(age);
Query OK, 0 rows affected (0.19 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> show index from test11 \G;
*************************** 1. row ***************************
        Table: test11
   Non_unique: 1
     Key_name: haha
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 3
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
*************************** 2. row ***************************
        Table: test11
   Non_unique: 1
     Key_name: hehe
 Seq_in_index: 1
  Column_name: age
    Collation: A
  Cardinality: 3
     Sub_part: NULL
       Packed: NULL
         Null: YES
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
2 rows in set (0.01 sec)

删索引

mysql> drop index hehe on test11;
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show index from test11 \G;
*************************** 1. row ***************************
        Table: test11
   Non_unique: 1
     Key_name: haha
 Seq_in_index: 1
  Column_name: name
    Collation: A
  Cardinality: 3
     Sub_part: NULL
       Packed: NULL
         Null:
   Index_type: BTREE
      Comment:
Index_comment:
      Visible: YES
   Expression: NULL
1 row in set (0.00 sec)

ERROR:
No query specified

mysql> desc test11;
+---------+----------------------------------------+------+-----+---------+-------+
| Field   | Type                                   | Null | Key | Default | Extra |
+---------+----------------------------------------+------+-----+---------+-------+
| name    | char(5)                                | NO   | MUL |         |       |
| age     | tinyint(3)                             | YES  |     | 25      |       |
| likes   | set('write','read','game','music')     | YES  |     | NULL    |       |
| address | enum('shenzhen','guangzhou','beijing') | YES  |     | NULL    |       |
+---------+----------------------------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

主键

请添加图片描述

请添加图片描述

建表时,定义主键的方式

1、选项创建
mysql> create table test013(
    -> name char(10) primary key,
    -> age int(3)
    -> );
Query OK, 0 rows affected, 1 warning (0.10 sec)

mysql> desc test013;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(10) | NO   | PRI | NULL    |       |
| age   | int(3)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)


2、命令创建
mysql> create table test014 (
    -> name char(5),
    -> age int(3),
    -> sex enum('girl','boy'),
    -> primary key(name)
    -> );
Query OK, 0 rows affected, 1 warning (0.33 sec)

mysql> desc test014;
+-------+--------------------+------+-----+---------+-------+
| Field | Type               | Null | Key | Default | Extra |
+-------+--------------------+------+-----+---------+-------+
| name  | char(5)            | NO   | PRI | NULL    |       |
| age   | int(3)             | YES  |     | NULL    |       |
| sex   | enum('girl','boy') | YES  |     | NULL    |       |
+-------+--------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改、删除主键。!!注:取消主键后的列,‘null’列为NO,YES 要手动改!!

请添加图片描述

mysql> alter table test013 drop primary key;
Query OK, 0 rows affected (0.33 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test013;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(10) | NO   |     | NULL    |       |
| age   | int(3)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)


mysql> alter table test013 modify name char(10);
Query OK, 0 rows affected (0.32 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test013;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(10) | YES  |     | NULL    |       |
| age   | int(3)   | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)



2、修改主键:
mysql> alter table test013 add primary key(age);
Query OK, 0 rows affected (0.20 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test013;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(10) | YES  |     | NULL    |       |
| age   | int(3)   | NO   | PRI | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

如果字段下面的值,有相同的,则无法创建主键!!!

mysql> select * from test010;
+------+------+-----------+----------+
| name | age  | class     | up_time  |
+------+------+-----------+----------+
| bob  |   27 | class2021 | 20:52:27 |
| bob  |   27 | class2021 | 15:23:25 |
| tom  | NULL | class2105 | 12:23:45 |
| tom  |   22 | null      | 12:23:45 |
|      |   27 | class2021 | NULL     |
| null |   27 | class2021 | NULL     |
+------+------+-----------+----------+
6 rows in set (0.03 sec)

mysql> desc test010;
+---------+---------------------+------+-----+-----------+-------+
| Field   | Type                | Null | Key | Default   | Extra |
+---------+---------------------+------+-----+-----------+-------+
| name    | char(10)            | NO   |     | NULL      |       |
| age     | tinyint(3) unsigned | YES  |     | 27        |       |
| class   | char(10)            | NO   |     | class2021 |       |
| up_time | time                | YES  |     | NULL      |       |
+---------+---------------------+------+-----+-----------+-------+
4 rows in set (0.00 sec)

mysql> alter table test010 add primary key(name);
ERROR 1062 (23000): Duplicate entry 'bob' for key 'PRIMARY'
mysql> alter table test010 add primary key(age);
ERROR 1138 (22004): Invalid use of NULL value

复合主键(表里的多列一起定义为主键,复合主键的值不允许同时一样!!)

mysql> create table test015 (ip varchar(15), port smallint, status enum('open','no'), primary key(ip,port) );
Query OK, 0 rows affected (0.15 sec)

mysql> desc test015;
+--------+-------------------+------+-----+---------+-------+
| Field  | Type              | Null | Key | Default | Extra |
+--------+-------------------+------+-----+---------+-------+
| ip     | varchar(15)       | NO   | PRI | NULL    |       |
| port   | smallint(6)       | NO   | PRI | NULL    |       |
| status | enum('open','no') | YES  |     | NULL    |       |
+--------+-------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)


mysql> insert into test015 values ('192.168.12.12',22,'no');
ERROR 1062 (23000): Duplicate entry '192.168.12.12-22' for key 'PRIMARY'
mysql> insert into test015 values ('192.168.12.12',43,'no');
Query OK, 1 row affected (0.06 sec)

mysql> insert into test015 values ('192.168.12.12',23,'open');
Query OK, 1 row affected (0.01 sec)

mysql> insert into test015 values ('192.168.12.12',80,'no');
Query OK, 1 row affected (0.01 sec)


mysql> select * from test015;
+---------------+------+--------+
| ip            | port | status |
+---------------+------+--------+
| 192.168.12.12 |   22 | open   |
| 192.168.12.12 |   23 | open   |
| 192.168.12.12 |   43 | no     |
| 192.168.12.12 |   80 | no     |
+---------------+------+--------+
4 rows in set (0.00 sec)

修改、删除主键

mysql> alter table test015 drop primary key;
Query OK, 4 rows affected (0.21 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> desc test015;
+--------+-------------------+------+-----+---------+-------+
| Field  | Type              | Null | Key | Default | Extra |
+--------+-------------------+------+-----+---------+-------+
| ip     | varchar(15)       | NO   |     | NULL    |       |
| port   | smallint(6)       | NO   |     | NULL    |       |
| status | enum('open','no') | YES  |     | NULL    |       |
+--------+-------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table test015 add primary key(ip,port);
Query OK, 0 rows affected (0.27 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc test015;
+--------+-------------------+------+-----+---------+-------+
| Field  | Type              | Null | Key | Default | Extra |
+--------+-------------------+------+-----+---------+-------+
| ip     | varchar(15)       | NO   | PRI | NULL    |       |
| port   | smallint(6)       | NO   | PRI | NULL    |       |
| status | enum('open','no') | YES  |     | NULL    |       |
+--------+-------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

主键与auto_increment(自动增长+1)连用。需要字段为主键,列的类型为数值类型!!!

mysql> create table test016 (
    -> id int primary key  auto_increment,
    -> name char(5),age tinyint(3),class char(10));
Query OK, 0 rows affected, 1 warning (0.16 sec)

mysql> desc test016;
+-------+------------+------+-----+---------+----------------+
| Field | Type       | Null | Key | Default | Extra          |
+-------+------------+------+-----+---------+----------------+
| id    | int(11)    | NO   | PRI | NULL    | auto_increment |
| name  | char(5)    | YES  |     | NULL    |                |
| age   | tinyint(3) | YES  |     | NULL    |                |
| class | char(10)   | YES  |     | NULL    |                |
+-------+------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)



mysql> insert into test016(name,age,class) values 

mysql> insert into test016(name,age,class) values 

mysql> insert into test016(name,age,class) values )

mysql> select * from test016;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  1 | bob  |   26 | class2305 |
|  2 | tom  |   26 | class2305 |
|  3 | lisi |   35 | class2305 |
+----+------+------+-----------+
3 rows in set (0.00 sec)



2、自定义编号值
mysql> insert into test016  values (5,'haha',35,'class2305');
Query OK, 1 row affected (0.06 sec)

mysql> insert into test016  values (null,'haha',35,'class2305');
Query OK, 1 row affected (0.02 sec)
# 赋值空,会继续按照自+1进行

mysql> select * from test016;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  1 | bob  |   26 | class2305 |
|  2 | tom  |   26 | class2305 |
|  3 | lisi |   35 | class2305 |
|  5 | haha |   35 | class2305 |
|  6 | haha |   35 | class2305 |
+----+------+------+-----------+
5 rows in set (0.01 sec)

按唯一编号,去查找信息

mysql> select * from test016 where id=3;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  3 | lisi |   35 | class2305 |
+----+------+------+-----------+
1 row in set (0.01 sec)


mysql> select * from test016 where id>=3;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  3 | lisi |   35 | class2305 |
|  5 | haha |   35 | class2305 |
|  6 | haha |   35 | class2305 |
+----+------+------+-----------+
3 rows in set (0.06 sec)

有自增长的主键,要先删掉自增长才能删主键。2、按上述表状态就算把表情空,再添加记录,编号也会按清空前的最后一位+1。3、要想重新排序,删字段再重建。

mysql> delete from test016;
Query OK, 5 rows affected (0.07 sec)

mysql> insert into test016  values (null,'haha',35,'class2305');
Query OK, 1 row affected (0.01 sec)

mysql> select * from test016  ;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  7 | haha |   35 | class2305 |
+----+------+------+-----------+
1 row in set (0.00 sec)



mysql> alter table test016 drop id;
Query OK, 1 row affected (0.32 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> select * from test016  ;
+------+------+-----------+
| name | age  | class     |
+------+------+-----------+
| haha |   35 | class2305 |
+------+------+-----------+
1 row in set (0.00 sec)

mysql> alter table test016 add
    -> id int primary key  auto_increment first;
Query OK, 0 rows affected (0.40 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> insert into test016  values (null,'haha',35,'class2305');
Query OK, 1 row affected (0.00 sec)

mysql> select * from test016  ;
+----+------+------+-----------+
| id | name | age  | class     |
+----+------+------+-----------+
|  1 | haha |   35 | class2305 |
|  2 | haha |   35 | class2305 |
+----+------+------+-----------+
2 rows in set (0.00 sec)

外键

请添加图片描述

请添加图片描述

建员工和工资表,指定外键工资列的id与员工表的对应,同步。

mysql> create table test017 (
    -> name char(10),
    -> id int(10) primary key auto_increment)
    -> engine=innodb;
Query OK, 0 rows affected, 1 warning (0.22 sec)

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

mysql> insert into  test017(name)  values ('bob'),('tom'),('lisi');
Query OK, 3 rows affected (0.09 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from test017;
+------+----+
| name | id |
+------+----+
| bob  |  1 |
| tom  |  2 |
| lisi |  3 |
+------+----+
3 rows in set (0.00 sec)


1、指定外键:
mysql> alter table test017 rename test017_yg;
Query OK, 0 rows affected (0.05 sec)

mysql> create table test017_gz (
    -> gz_id int,
    -> pay float(8,2),
    -> foreign key(gz_id)  references test017_yg(id) on update cascade  on delete cascade
    -> )engine=innodb;
Query OK, 0 rows affected, 1 warning (0.26 sec)


2、查看结构:
mysql> desc test017_gz;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| gz_id | int(11)    | YES  | MUL | NULL    |       |
| pay   | float(8,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)



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

验证是否有外键:

mysql> show create table test017_gz\G;

*************************** 1. row ***************************
       Table: test017_gz
Create Table: CREATE TABLE `test017_gz` (
  `gz_id` int(11) DEFAULT NULL,
  `pay` float(8,2) DEFAULT NULL,
  KEY `gz_id` (`gz_id`),
  CONSTRAINT `test017_gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `test017_yg` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.09 sec)

ERROR:
No query specified

添加不在条件范围内的,会报错!!

mysql> select * from test017_yg;
+------+----+
| name | id |
+------+----+
| bob  |  1 |
| tom  |  2 |
| lisi |  3 |
+------+----+
3 rows in set (0.00 sec)

mysql> insert into test017_gz values (4,15566);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db1`.`test017_gz`, CONSTRAINT `test017_gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `test017_yg` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
mysql>

验证,同步删除的效果

mysql> delete from test017_yg where id=2;
Query OK, 1 row affected (0.03 sec)

mysql> select * from test017_yg;
+------+----+
| name | id |
+------+----+
| bob  |  1 |
| lisi |  3 |
+------+----+
2 rows in set (0.00 sec)

mysql> select * from test017_gz;
+-------+----------+
| gz_id | pay      |
+-------+----------+
|  NULL | 11863.55 |
|  NULL | 17580.85 |
|  NULL | 16455.66 |
|     1 | 15600.00 |
|     3 | 15566.00 |
+-------+----------+
5 rows in set (0.00 sec)


修改,同步效果

mysql> update test017_yg set id=7 where name='bob';
Query OK, 1 row affected (0.07 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from test017_yg;
+------+----+
| name | id |
+------+----+
| lisi |  3 |
| bob  |  7 |
+------+----+
2 rows in set (0.00 sec)

mysql> select * from test017_gz;
+-------+----------+
| gz_id | pay      |
+-------+----------+
|  NULL | 11863.55 |
|  NULL | 17580.85 |
|  NULL | 16455.66 |
|     7 | 15600.00 |
|     3 | 15566.00 |
+-------+----------+
5 rows in set (0.00 sec)

删外键:

mysql> alter table test017_gz drop foreign key test017_gz_ibfk_1;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test017_gz;
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                        |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test017_gz | CREATE TABLE `test017_gz` (
  `gz_id` int(11) NOT NULL,
  `pay` float(8,2) DEFAULT NULL,
  PRIMARY KEY (`gz_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> insert into test017_gz values(9,45222);
Query OK, 1 row affected (0.00 sec)

mysql> select * from test017_gz;
+-------+----------+
| gz_id | pay      |
+-------+----------+
|     3 | 45222.00 |
|     7 | 45222.00 |
|     9 | 45222.00 |
+-------+----------+
3 rows in set (0.00 sec)

mysql> select * from test017_yg;
+------+----+
| name | id |
+------+----+
| lisi |  3 |
| bob  |  7 |
+------+----+
2 rows in set (0.00 sec)

扩展,外键可存空值,这于表的设计思路不符,需要进行控制。把目标外键设置为主键,可避免为空和重复。

mysql> delete from test017_gz;
Query OK, 5 rows affected (0.05 sec)

mysql> desc test017_gz;
+-------+------------+------+-----+---------+-------+
| Field | Type       | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| gz_id | int(11)    | YES  | MUL | NULL    |       |
| pay   | float(8,2) | YES  |     | NULL    |       |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> alter table test017_gz add primary key(gz_id);
Query OK, 0 rows affected (0.22 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table test017_gz;
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table      | Create Table                                                                                                                                                                                                                                                                                                   |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test017_gz | CREATE TABLE `test017_gz` (
  `gz_id` int(11) NOT NULL,
  `pay` float(8,2) DEFAULT NULL,
  PRIMARY KEY (`gz_id`),
  CONSTRAINT `test017_gz_ibfk_1` FOREIGN KEY (`gz_id`) REFERENCES `test017_yg` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

验证:

mysql> insert into test017_gz values(7,45222);
Query OK, 1 row affected (0.08 sec)

mysql> insert into test017_gz values(null,45222);
ERROR 1048 (23000): Column 'gz_id' cannot be null
mysql> insert into test017_gz values(3,45222);
Query OK, 1 row affected (0.01 sec)

mysql> select * from test017_gz;
+-------+----------+
| gz_id | pay      |
+-------+----------+
|     3 | 45222.00 |
|     7 | 45222.00 |
+-------+----------+
2 rows in set (0.00 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/518627.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java中的 stop the world是什么?

一、概述; 从字面上讲,就是停止这个世界,看到这个字眼,就觉得这是可怕的事情,那到底什么是stop-the-world? stop-the-world,简称 STW,指的是 GC 事件发生过程中,会产生…

面试题30天打卡-day26

1、什么是 AOP?Spring AOP 和 AspectJ AOP 有什么区别?有哪些实现 AOP 的方式? AOP(Aspect-Oriented Programming,面向切面编程)是一种编程思想,可以在不修改原有业务逻辑代码的情况下&#xf…

srs 直播连麦环境搭建

一、简介 二、修改conf/rtc.conf 三、两个客户端加入房间 四、合流 4.1分别拉流尝试 4.2合流推流 4.3拉取合流 一、简介 直播连麦是指在one2one或one2many进行音视频通话,此时把他们的音视频流合在一起,通过rtmp等协议推送给大量用户做直播。 因此首选需…

Java面试(3)基础语法

基础语法 1. 标识符和关键字的区别是什么? 在我们编写程序的时候,需要大量地为程序、类、变量、方法等取名字,于是就有了 标识符 。简单来说, 标识符就是一个名字 。 有一些标识符,Java 语言已经赋予了其特殊的含义…

python带你用最简单嘚词云图分析出最热话题

前言 嗨喽,大家好呀~这里是爱看美女的茜茜呐 平常我们爬的评论、弹幕等等,数量又多又密,根本看不过来, 这时候数据分析的作用来了, 今天我们就试试用Python根据这些数据,来绘制词云图进行热词分析。 目录…

JUC-线程Callable使用与FutureTask源码阅读

JUC-线程Callable使用与FutureTask源码阅读 Callable简单使用 带返回值的线程(实现implements Callable<返回值类型>)&#xff0c;使用示例 import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.Fut…

STC32G12K128单片机的 moubus-rtu 主机测试工程

简介 STC32G12K128 是STC 推出的一款32位的 C251 的单片机。最近拿到一块官方申请的 屠龙刀-STC32G开发板&#xff0c;就用它的提供的库函数&#xff0c;查考安富莱提供的 modbus 例程移植了一个 modbus-rtu 主站的工程。 modbus-rtu host 移植注意点 modbus-rtu 功能配置 …

MapReduce序列化【用户流量使用统计】

目录 什么是序列化和反序列化&#xff1f; 序列化 反序列化 为什么要序列化&#xff1f; 序列化的主要应用场景 MapReduce实现序列化 自定义bean对象实现Writable接口 1.实现Writable接口 2.无参构造 3.重写序列化方法 4.重写反序列化方法 5.顺序一致 6.重写toStri…

您应该查看的5个ChatGPT WordPress插件

要创建免费网站&#xff1f;从易服客建站平台免费开始 500M免费空间&#xff0c;可升级为20GB电子商务网站 您应该查看的5个ChatGPT WordPress插件 发布于 2023年4月1日 ChatGPT 席卷了数字世界。作为内容创建者或营销者&#xff0c;您可能希望通过在您的网站上使用ChatGPT…

JimuReport - 积木报表(一款免费Web报表工具)

一款免费的数据可视化报表&#xff0c;含报表和大屏设计&#xff0c;像搭建积木一样在线设计报表&#xff01;功能涵盖&#xff0c;数据报表、打印设计、图表报表、大屏设计等&#xff01; Web 版报表设计器&#xff0c;类似于excel操作风格&#xff0c;通过拖拽完成报表设计。…

扫雷【C语言】

用C语言实现一个9X9的扫雷 test.c 测试部分 game.c 游戏实现部分 game.h 游戏声明部分 菜单部分 游戏部分 游戏部分包括创建一个扫雷的区域&#xff0c;在其中埋雷&#xff0c;玩家进行扫雷&#xff0c;判断扫雷是否成功 这里我i们定义行列&#xff0c;便于以后将其…

2023.05.12-使用Transformers Agents来一键调用千万个AI模型

1. 简介 简单来说&#xff0c;就是以前想要实现某一个AI功能&#xff0c;需要自己去网上搜索对应的模型、下载对应的权重才能使用。现在可以把中间的这些个环节都砍了&#xff0c;我们只需要告诉模型我们想要对某段文字或者某张图片进行什么操作&#xff0c;transformer就会自…

【经验贴】项目风险管理的有效方法

你遇见过“最奇葩”的项目风险是什么&#xff1f; 中级项目经理小李&#xff1a;我比较幸运? 项目一开始发现客户的需求不太明确&#xff0c;就识别出可能会有范围无限蔓延的风险&#xff0c;制定了一系列的应对措施&#xff0c;不出所料出了问题&#xff0c;最终还是将风险遏…

springboot整合redis,MongoDB,Elasticsearch(ES)

目录 springboot整合redis 连接Redis 字符串操作 哈希表操作 列表操作 集合操作 有序集合操作 lettcus与jedis的区别 springboot整合MongoDB 新增数据 查询数据 更新数据 删除数据 springboot整合Elasticsearch&#xff08;ES&#xff09; 创建ElasticsearchRepo…

区间预测 | MATLAB实现QRCNN-BiLSTM卷积双向长短期记忆神经网络分位数回归时间序列区间预测

区间预测 | MATLAB实现QRCNN-BiLSTM卷积双向长短期记忆神经网络分位数回归时间序列区间预测 目录 区间预测 | MATLAB实现QRCNN-BiLSTM卷积双向长短期记忆神经网络分位数回归时间序列区间预测效果一览基本介绍模型描述程序设计参考资料 效果一览 基本介绍 1.Matlab实现基于QRCNN…

Redis数据热迁移

1、redis数据热迁移&#xff0c;先通过redis-shake工具实现。 2、再通过redis-full-check检查迁移后的数据是否想同。 3、redis-shake版本下载&#xff1a; https://github.com/tair-opensource/RedisShake/releases 4、redis-full-check版本下载&#xff1a; https://github.c…

更简单的存和取bean

注解&#xff08;更简单存bean和取bean&#xff09;更简单的存bean&#xff08;使用类注解&#xff09;方法注解更简单的获取bean 注解&#xff08;更简单存bean和取bean&#xff09; 注解&#xff1a;声明和表示这个类或方法有某个能力。servlet的Web注解&#xff1b;实现路由…

2000多套微信小程序源码-史上最全的不同行业的源码集合

前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 很多伙伴学习小程序不知怎么开始&#xff0c;我准备了2000多套小程序源码&#xff0c;基本覆盖各个行业&#xff0c;大家有需要的可以借鉴学习~ 提示&#xff1a;以下是本篇文章正文内容&#xff0c;下面…

“数字人交互,与虚拟的自己互动”——用PaddleAvatar打造数字分身,探索人机交互的未来

“数字人交互&#xff0c;与虚拟的自己互动”——用PaddleAvatar打造数字分身&#xff0c;探索人机交互的未来 你是否曾经幻想过与自己的虚拟人交互&#xff1f;现在&#xff0c;使用PaddleAvatar&#xff0c;您可以将自己的图像、音频和视频转化为一个逼真的数字人视频&#…

Spring Cloud Alibaba--Nacos集群配置

文章目录 一、Nacos持久化配置二、Nacos集群配置三、避坑指南集群端口号冲突问题内存不足&#xff0c;无法启动的问题 一、Nacos持久化配置 Nacos默认自带的是嵌入式数据库derby&#xff0c;Nacos采用了集中式存储的方式来支持集群化部署&#xff0c;目前只支持MySQL的存储。 …