Database field attributes and indexes

Field attributes
primary key, a unique value and since the growth of
a primary key
primary key: primary key, major key, a table can have only one field may be used key corresponding to only constraint of the field inside the data can not be repeated, it species called the primary key.
1. Increase the primary key
SQL operations in a variety of ways to increase the primary key table: roughly divided into three
Scenario 1: When creating a table, directly after the field, with key primary key (primary key itself does not allow nulls)
example : Create table course, set the primary key to the Cname
Create table course (
-> ID VARCHAR (20 is) Not null Comment 'program number',
-> the Cname VARCHAR (20 is) primary key Comment 'program name'
->) charset UTF8;
Here Insert Picture Descriptionadvantages: very straightforward; disadvantages: use only one field as the primary key.
Scheme 2: In the creation of a table, after all fields, to create a master key using the primary key (primary key field list) (if there is more than one field as the primary key, the composite can be primary key)

Composite primary key
Create Table SC (
Number char (10) Comment "student number",
Course char (10) Comment "course code",
Score tinyint unsigned default 60 Comment 'Results',
- increasing primary key constraint: student number and the course number should be corresponding, unique
Primary Key (Number, Course)
) charset UTF8;

Here Insert Picture Description

Scenario 3: When the table has been created, additional additional primary keys again, you can modify the table field properties, it can also be directly added.
Alter table table add primary key (field list)

Here Insert Picture DescriptionHere Insert Picture DescriptionPrerequisite: the corresponding data field in a table itself is an independent (non-duplicate)

2, the primary key constraint
data of the master key corresponding to the field should not be repeated once repeated, the data operation has failed (by and modified)
inserting data into Student
INSERT INTO test_student values ( 'itcast001', 'king', 'M'), ( ' itcast002 ',' Wu three ',' F ');
INSERT INTO SC values (' itcast001 ',' 3901001 ', 90), (' itcast002 ',' 3901002 ', 100);

insert into test_student values ( 'itcast002' , ' four Liu', 'F');
INSERT INTO SC values ( 'itcast001', '3901001', 100);
Here Insert Picture Descriptionoperation failed

3、更新主键&删除主键
没有办法更新主键,逐渐必须先删除后才能添加
Alter table 表名 drop primary key
删除一个主键
alter table ccc drop primary key;
Here Insert Picture Description
4、主键分类
在实际创建表的过程中,很少使用真是业务数据作为主键字段(业务主键,如学号,课程号);
大部分的时候是使用逻辑性的字段(字段没有业务含义,值是什么没有关系),将这种字段主键称之为逻辑主键。

create table my_student(
id int primary key auto_increment comment ‘逻辑主键:自增长’,
number char(10) not null comment ‘学号’,
name varchar(10) not null
)

二、自动增长

1、新增自增长:当对应的字段,不给值或者说给默认值,或者给null的时候,会自动的被系统触发,系统会从当前字段中已有的最大值再进行+1操作,得到一个新的在不同的字段
自增长特点:auto_increment
1、任何一个字段都要做自增长必须前提是本身是一个索引(key一栏有值)
2、自增长字段必须是数字(整型)
3、一张表最多只能有一个自增长

create table my_auto(
id int auto_increment comment ‘自动增长’ ,
name varchar(10) not null
)charset utf8;

Here Insert Picture Description错误
改正为

create table my_auto(
id int primary key auto_increment comment ‘自动增长’ ,
name varchar(10) not null
)charset utf8;

Here Insert Picture Description2、自增长的使用:
当自增长被给定的值为null或者默认值的时候会触发自动增长

insert into my_auto(name) values (‘邓丽君’);
insert into my_auto values(null,‘一一’);
insert into my_auto values(default ,‘张韬’);

Here Insert Picture DescriptionHere Insert Picture Description自增长如果对应的字段输入了值,那么自增长失效,但是下一次还是能够正确的自增长(从最大值+1)

insert into my_auto values (6,‘荷花’);
insert into my_auto values(null,‘莲花’);
Here Insert Picture Description如何确定下一次是什么自增长呢?可以通过查看表创建语句看到。

show create table my_auto;
Here Insert Picture Description3、修改自增长
1、自增长如果是涉及到字段改变,这个时候必须先删除自增长,因为一张表只能有一个自增长
2、修改当前自增长已经存在的值:修改只能比当前已有的自增长的最大值大,不能小(小不生效)。

alter table my_auto auto_increment=4;–向下修改(小) 不成功
Here Insert Picture Descriptionalter table my_auto auto_increment=10;–向上修改(大) 成功
Here Insert Picture Description思考:为什么自增长是从1开始?为什么每次都是自增1呢?
所有系统的变化(如字符集,校对集)都是由系统内部的变量进行控制的。
查看自增长对应的变量:
show variables like ‘auto_increment%’;
Here Insert Picture Description可知,步长为1,起始值也为1.
可以修改变量实现不同的效果:修改时对整个数据修改,而不是单张表(修改是会话级)

修改自增长步长
set auto_increment_increment=5;
Here Insert Picture Description测试效果:自动使用增长
插入记录:使用自增长
insert into my_auto values(null,‘刘洋’);
insert into my_auto values(null,‘刘杨’);
Here Insert Picture Description刚开始修改时系统没有反应过来,所以测试第二次。

4、删除自增长
自增长是字段的一个属性:可以通过modify来进行修改(保证字段没有auto_increment即可)
alter table 表名 modify 字段 类型;
例如:
alter table my_auto modify id int primary key;–错误:主键理论是单独存在的。
alter table my_auto modify id int;–有主键的时候,千万不要再添加主键。
Here Insert Picture Description三、唯一键
一张表往往有很多字段需要具有唯一性,数据不能重复:但是一张表中只能有一个主键,唯一键就可以解决表中有多个字段需要唯一性约束的问题。

唯一键的本质和主键差不多:唯一键默认的允许自动为空,而且可以多个为空(空字段不参与唯一性比较)

1、增加唯一键
基本与主键差不多:三种方案
方案1:在创建表的时候,字段后面直接跟unique/unique key

create table my_unique1(
number char(10) unique comment ‘学号: 唯一,允许为空’,
name varchar(20) not null
)charset utf8;

Here Insert Picture Description
方案2:在所有的字段之后增加unique key(字段列表);–复合唯一键

create table my_unique2(
number char(10) not null comment ‘学号’,
name varchar(20) not null,
unique key(number)
)charset utf8;

Here Insert Picture DescriptionHere Insert Picture Description之所以会显示为PRI,刚好是一个不为空的唯一键(主键性质一样),原因是没有主键。
方案3:

create table my_unique3(
id int primary key auto_increment,
number char(10) not null,
name varchar(20) not null)charset utf8;

–追加唯一键
alter table my_unique3 add unique key(number);
Here Insert Picture Description当主键存在的时候,unique不能变成主键
2、唯一键约束

唯一键与主键本质相同;唯一的区别就是唯一值默认允许为空,而且是多个为空。
insert into my_unique1 values(null,‘曾光’),(‘itcast001’,‘陈八’),(null,‘陈九’);
Here Insert Picture Descriptioninsert into my_unique1 values(‘itcast001’,‘陈十’);
Here Insert Picture Description如果唯一键也不允许为空,那么与逐渐的约束是一样的。
3、更新唯一键&删除唯一键
更新唯一键:先删除后新增(唯一键有多个,可以不删除)
删除唯一键
alter table 表名 drop unique key;–错误,唯一键有多个
alter table 表名 drop index 索引名字 --索引名字默认是字段名
alter table my_unique3 drop index number;
Here Insert Picture Description

四、索引
几乎所有的索引都是建立在字段之上
索引:系统根据某种算法,将已有的数据(未来可能新增的数据),单独建立一个文件,文件能够实现快速的匹配数据,并且能够快速的找到对应表中的记录。
索引的意义:
1、提升查询数据的效率
2、约束数据的有效性(唯一性等)

增加索引的条件:索引本身会产生索引文件(有时候可能比数据文件还大),会非常耗费磁盘空间。
如果某个字段需要作为查询的条件经常使用,那么可以使用索引(一定会想办法增加);
如果某个字段需要进行数据的有效性约束,也可能使用索引(主键,唯一键)

Mysql provides a variety of index
1, the primary key Key Index Primary
2, Key unique index UNIQUE
. 3, full-text index index FULLTEXT
. 4, the index of ordinary index

Full-text index: conduct for keywords inside the article index
full-text indexing biggest problem: how to determine the keywords that
English is easy; there are spaces between English words and word
Chinese is difficult; there are no spaces, and various Chinese can be freely combined (word : sphinx)

Guess you like

Origin blog.csdn.net/weixin_44097082/article/details/94667401