mysql数据库基础(2)索引主键复合主键外键

Posted hanlongyu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库基础(2)索引主键复合主键外键相关的知识,希望对你有一定的参考价值。

增、删、改、查

加“”表示为普通字符

mysql> insert into  t9 values("NULL",21,"game,flim");

+++++++++++++++++++++++++++++++

添加新字段

mysql> alter table  t4 add age int(2) default 19;        //默认在最后面

mysql> alter table  t4 add mail char(50) after name;   //加在name后面

mysql> alter table  t4 add stu_num char(10) first;      //加在最前面

 

修改字段类型 ,类型要与字段存储的数据匹配,不能有冲突.

mysql> alter table  t4 modify name varchar(3);

ERROR 1406 (22001): Data too long for column ‘name‘ at row 1

 

mysql> alter table  t4 modify name varchar(15);

Query OK, 1 row affected (0.44 sec)

 

修改字段位置

mysql> alter table  t4  modify age int(2) default  19

after name;

 

删除字段

mysql> alter table  t5 drop start;

mysql> select  * from t5;

mysql> desc  t5;

 

修改字段名

alter table  t4 change stu_num stu_id char(10);

 

修改表名

alter table   t9 rename stuinfo;

 

应用

mysql> create table t7(meeting datetime,party timestamp);

                                             不自动赋值      会自动赋值当前时间

mysql> insert into t7 values(now(),now());

Query OK, 1 row affected (0.07 sec)

 

mysql> select * from t7;

+---------------------+---------------------+

| meeting             | party               |

+---------------------+---------------------+

| 2018-10-16 09:31:50 | 2018-10-16 09:31:50 |

+---------------------+---------------------+

 

mysql> insert into t7(meeting) values(now());

 

mysql> select * from t7;

+---------------------+---------------------+

| meeting             | party               |

+---------------------+---------------------+

| 2018-10-16 09:31:50 | 2018-10-16 09:31:50 |

| 2018-10-16 09:33:12 | 2018-10-16 09:33:12 |

+---------------------+---------------------+

 

 

mysql> select * from t7;

+---------------------+---------------------+

| meeting             | party               |

+---------------------+---------------------+

| 2018-10-16 09:31:50 | 2018-10-16 09:31:50 |

| 2018-10-16 09:33:12 | 2018-10-16 09:33:12 |

| 2018-10-17 08:25:00 | 2018-10-16 09:34:01 |

| NULL                | 2018-10-17 08:25:00 |

+---------------------+---------------------+

 

 

mysql> desc teadb.t5;

+----------+----------+------+-----+---------+-------+

| Field    | Type     | Null | Key | Default | Extra |

+----------+----------+------+-----+---------+-------+

| name     | char(10) | YES  |     | NULL    |       |

| birthday | date     | YES  |     | NULL    |       |

| start    | year(4)  | YES  |     | NULL    |       |

| uptime   | time     | YES  |     | NULL    |       |

| party    | datetime | YES  |     | NULL    |       |

+----------+----------+------+-----+---------+-------+

 

 

mysql> insert into t5(name,start) values("tom",50);

 

mysql> select * from t5;

+------+------------+-------+----------+---------------------+

| name | birthday   | start | uptime   | party               |

+------+------------+-------+----------+---------------------+

| bob  | 1993-07-02 |  1993 | 08:30:00 | 2018-10-15 20:30:00 |

| tom  | NULL       |  2050 | NULL     | NULL                |

+------+------------+-------+----------+---------------------+

//year默认为4位数,1-69--->20   70-99--->19

 

 

mysql> create table t88(name char(3),level int(3) zerofill);    //显示宽度,0补足位

Query OK, 0 rows affected (0.25 sec)

 

mysql> insert into t88 values("tom",1131);

mysql> insert into t88 values("tom",113);

mysql> insert into t88 values("tom",11);

mysql> insert into t88 values("tom",3);

 

mysql> select * from t88;

+------+-------+

| name | level |

+------+-------+

| tom  |  1131 |

| tom  |   113 |

| tom  |   011 |

| tom  |   003 |

+------+-------+

 

 

 

 

 

mysql> create table t9(name char(10) not null,age tinyint(2) default 19,likes set("eat","sleep","film","game") not null default "game,eat");

//no null:不能为空值 ,默认可以为空   //detault:默认值

mysql> insert into t9(name) values("bob");

Query OK, 1 row affected (0.02 sec)

 

mysql> select * from t9;

+---------+---------+----------------+

| name | age  |    likes   |

+---------+---------+----------------+

| bob  |   19 | game,eat  |

+---------+---------+----------------+

 

 

mysql> desc t9;

+-------+-------------------------------------------------------+-------+-----+------------+--------+

| Field | Type                             | Null | Key | Default | Extra |

+-------+-------------------------------------------------------+------+------+------------+-------+

| name | char(10)                          | NO |   | NULL   |     |

| age  | tinyint(2)                         | YES |   | 19     |     |

| likes  | set(‘eat‘,‘sleep‘,‘film‘,‘game‘)         | NO |   | game  |     |

+-------+-------------------------------------------------------+------+-----+-----------+---------+

//不输入会默认补内容

 

 

添加字段 默认在最后

mysql> alter table t4 add nanjing int(2) default 22;

 

mysql> select * from t4;

+--------+------+------+------+---------+---------+

| stu_id | name | age  | mail | homedir | nanjing |

+--------+------+------+------+---------+---------+

| NULL   | jack |   19 | NULL | USA     |      22 |

| NULL   | lll  |   19 | NULL | han     |      22 |

+--------+------+------+------+---------+---------+

 

 

索引

索引是指给字段排队,类似书的目录

优点:可以加快查询数据速度

缺点:减慢写入的速度,增加减少都需要重新排列。排队信息需要文件来存储,占内存

平时我们看不到排序,查询时直接会提示数据在几行。

创建表的时候添加索引:  //可同时添加多个索引

mysql> create table t12( name char(10),class char(7),age int(2) , index(name),index(class));

 

mysql> desc t12;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(10) | YES  | MUL | NULL    |       |

| class | char(7)  | YES  | MUL | NULL    |       |

| age   | int(2)   | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

 

 

mysql> show index from t12G;       //查看索引

 

mysql> drop index name on t12;         //删除索引

mysql> desc t12;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(10) | YES  |     | NULL    |       |

| class | char(7)  | YES  | MUL | NULL    |       |

| age   | int(2)   | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

 

mysql> create index name on t12(name);    //在已有的表里添加索引,索引名与表名一致

mysql> desc t12;

+-------+----------+------+-----+---------+-------+

| Field | Type     | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| name  | char(10) | YES  | MUL | NULL    |       |

| class | char(7)  | YES  | MUL | NULL    |       |

| age   | int(2)   | YES  |     | NULL    |       |

+-------+----------+------+-----+---------+-------+

 

创建主键的格式1

mysql> create table t22(name char(10),stu_num char(9),primary key(stu_num));

创建主键的格式2(同上)

mysql> create table t23(name char(10),stu_num char(9)primary key);

+---------+----------+------+-----+---------+-------+

| Field   | Type     | Null | Key | Default | Extra |

+---------+----------+------+-----+---------+-------+

| name    | char(10) | YES  |     | NULL    |       |

| stu_num | char(9)  | NO   | PRI | NULL    |       |

+---------+----------+------+-----+---------+-------+

 

mysql> select * from t22;

+------+-----------+

| name | stu_num   |

+------+-----------+

| tom  | nsd180705 |           //设值主键字段(stu_num)值不于允许重复,不能为空

+------+-----------+

 

复合主键(表中多个字段一起做主键,表中多行设符合主键不能重复不能为空)

mysql> create table t24(cip char(15),sport smallint(2),status enum("deny","all"),primary key(cip,sport));

mysql> select * from t24;

+-------------+-------+--------+

| cip         | sport | status |

+-------------+-------+--------+

| 192.168.4.1 |    21 | deny   |

| 192.168.4.1 |    22 | deny   |

| 192.168.4.2 |    22 | deny   |

| 192.168.4.3 |    22 | all    |

+-------------+-------+--------+

 

删除主键,不删除内容,还是不能为空,表将不受约束

mysql> alter table t24 drop primary key;               //删除

 

在已有表里创建复合主建(如果表中有重复的数据不能创建,需要先改表)

mysql> alter table t24 add primary key(cip,sport);      //创建

 

主键+自增长(自己存亦遵循约束规则//空值,重复)//自增长从最大的开始

//删除也从历史最大值开始记录

mysql> create table t25(stu_id int primary key auto_increment,name char(5),age tinyint(2) unsigned);                                   自加1

 

mysql> desc t25;

+--------+---------------------+------+-----+---------+----------------+

| Field  | Type                | Null | Key | Default | Extra          |

+--------+---------------------+------+-----+---------+----------------+

| stu_id | int(11)             | NO   | PRI | NULL    | auto_increment |

| name   | char(5)             | YES  |     | NULL    |                |

| age    | tinyint(2) unsigned | YES  |     | NULL    |                |

+--------+---------------------+------+-----+---------+----------------+

 

mysql> insert into t25(name,age)values("bob",19);

mysql> insert into t25(name,age)values("tom",18);

 

mysql> select * from t25;

+--------+------+------+

| stu_id | name | age  |

+--------+------+------+

|      1 | bob  |   19 |

|      2 | tom  |   18 |                   //自加一

+--------+------+------+

 

mysql> alter table t88 add id int(2) zerofill primary key auto_increment first;

 

mysql> select * from t88;

+----+------+-------+

| id | name | level |

+----+------+-------+

| 01 | tom  |  1131 |

| 02 | tom  |   113 |

| 03 | tom  |   011 |

| 04 | tom  |   003 |

+----+------+-------+

 

 

先珊自增字段才能珊主键

mysql> alter table t25 modify stu_id int(2) not null;         //先删除自增长段

mysql> desc t25;

+--------+---------------------+------+-----+---------+-------+

| Field  | Type                | Null | Key | Default | Extra |

+--------+---------------------+------+-----+---------+-------+

| stu_id | int(2)              | NO   | PRI | NULL    |       |  //原来有auto_increment

| name   | char(5)             | YES  |     | NULL    |       |

| age    | tinyint(2) unsigned | YES  |     | NULL    |       |

+--------+---------------------+------+-----+---------+-------+

mysql> alter table t25 drop primary key;              //珊主键

mysql> desc t25;

+--------+---------------------+------+-----+---------+-------+

| Field  | Type                | Null | Key | Default | Extra |

+--------+---------------------+------+-----+---------+-------+

| stu_id | int(2)              | NO   |     | NULL    |       |

| name   | char(5)             | YES  |     | NULL    |       |

| age    | tinyint(2) unsigned | YES  |     | NULL    |       |

+--------+---------------------+------+-----+---------+-------+

 

 

 

外键

定义:让当前表字段的值在另一个表中字段值的范围内选择,保证数据的一致性。

使用外键的条件:

表的存储引擎必须是innodb

字段类型要一致

被参照字段必须要是索引类型的一种(primary key)

 

 

 

mysql> create table ygtab(yg_id int(2)primary key auto_increment,naem char(10)) engine=innodb;

 

mysql> insert into ygtab(naem)values("bob");

mysql> insert into ygtab(naem)values("tom");

 

mysql> create table gztab(gz_id int(2),pay float(7,2),foreign key(gz_id) references ygtab(yg_id)on update cascade on delete cascade)engine=innodb;

//regerences:引用  foreign key:外键

mysql> desc gztab;

+-------+------------+------+-----+---------+-------+

| Field | Type       | Null | Key | Default | Extra |

+-------+------------+------+-----+---------+-------+

| gz_id | int(2)     | YES  | MUL | NULL    |       |

| pay   | float(7,2) | YES  |     | NULL    |       |

+-------+------------+------+-----+---------+-------+

 

 

 

准备案例:

mysql> insert into gztab values(1,28000);

mysql> insert into gztab values(2,38000);

mysql> select * from ygtab;

+-------+------+

| yg_id | naem |

+-------+------+

|     1 | bob  |

|     2 | tom  |

+-------+------+

mysql> select * from gztab;

+-------+----------+

| gz_id | pay      |

+-------+----------+

|     1 | 28000.00 |

|     2 | 38000.00 |

+-------+----------+

 

 

 

//改参考的员工表字段,自己表应该也同部更新  //单向同步

mysql> update ygtab set yg_id=8 where yg_id=2;

 

mysql> select * from gztab;

+-------+----------+

| gz_id | pay      |

+-------+----------+

|     1 | 28000.00 |

|     8 | 38000.00 |

+-------+----------+

2 rows in set (0.00 sec)

 

mysql> select * from ygtab;

+-------+------+

| yg_id | naem |

+-------+------+

|     1 | bob  |

|     8 | tom  |

+-------+------+

 

 

mysql> delete from ygtab where yg_id=8;          //删除员工表信息

 

mysql> select * from gztab;                     //工资表信息自动删除

+-------+----------+

| gz_id | pay      |

+-------+----------+

|     1 | 28000.00 |

+-------+----------+

1 row in set (0.00 sec)

//有利有弊

//有bug,gz_id 需要做主键不然可以连发工资,因为参考表有记录,被参考的表无法删除,需要先删除外键,再删除表(主键还在)

 

 

以上是关于mysql数据库基础(2)索引主键复合主键外键的主要内容,如果未能解决你的问题,请参考以下文章

Oracle查看表索引、主键、外键、约束

Mysql主键外键和索引的区别

主键外键和索引的区别?

主键外键和索引的区别?

主键外键和索引的区别?

主键外键索引说明