Mysql数据库学习总结
.
Mysql数据库学习总结
数据库的基本操作:创建删除查看
Create database school;
用于创建数据库,并且数据库的名字不可以更改
Show create database;show databases;
用来查看创建数据库的语句
Drop database;
用于删除数据库
表的基本操作:
Create table;
用于创建表,table后面加表名称
Create table student{
Id int;
Name varchar(10);
Sex Boolean;
}
Show tables;
用于显示数据库中的所有表
Describe student;
这里显示了字段、数据类型、是否为空、主外键、默认值和额外信息
Show create table;
显示创建表时的详细信息
Drop table student;
删除表的操作
文档Word
.
完整性约束是对字段进行限制,从而该字段达到我们期望的效果设置表的主键:主键能够标识表中的每条信息的唯一性。(primary key)创建主键的目的在于快速查找到表中的某一条信息
多字段主键:由多个属性组合而成; )例如:primary key(id,course_id
;
设置表的外键设置表的外键的作用在于建立与父表的联系
是主键idA中的是外键,表B中的id 比如表那么就可以称表为子表B为父表,表A
的记录也随着消失为的学生删除后,表中比如表Bid为123A中id123 这样做的目的在于保证
表的完整性。: 设置表的非空约束设置表中的字段不为空设置表的唯一性约束unique 唯一性约束指表中该字段的值不能重复出现,也就是给表中某个字段加上设置表的属性值自动增加:ID
auto_increment 主要用于为表中插入的新纪录自动生成唯一
ibixu并且该字段必须为主键的一部分,约束的值一个表中只能由一个字段使用此约束,是整型值。设置表中属性的默认值那么数据库系统就会为该字段如果没有为该字段赋值,在表中插入一体哦新的记录时,附上一条默认值。修改表
修改表需要用到alter table
文档Word
.
修改表名:Alter table student rename person;
Rename 用来命名修改字段的数据类型Alter table person modify name varchar(20); vaarchar(20) varchar(xx)修改为将原来的修改字段名name varchar(25)
Alter table person change stu_name
是新名,不管修不修改数据类型,后面的数据类型都namestu_name这里的是原名,要写增加无完整性约束条件的字段Alter table person add sex Boolean;
后面值跟了数据类型,而没有完整性约束条件sex 此处的增加完整性约束体条件的字段;Alter table person add age int not null 字段,接着在后面加上了约束条件增加了一条age 增加额外的完整性约束条件;Alter table person add primary key first 这样同样也用于多字段设置在表头添加字段Alter table person add num int primary key first;
节能添加到表头默认情况下添加到表尾,在添加语句后面加上first 在指定位置添加字段Alter table person add birth date after name;
后面这里添加一条新字段在name文档Word
.
删除字段Alter table person drop sex;
修改字段到第一个位置first table person modify id intAlte
修改字段到指定的位置Alter table person modify name varchar(25) after id;
(25)要写全name我们要把字段放到id后面,此处varchar 修改表的存储引擎Alter table user rename person;
增加表的外键:student(id);
(stu_id) referencesconstraint fk foreign key altertable score add
删除主键KEY
person DROP PRIMARY ALTER TABLE删除了所有的主键删除表的外键约束fk
keyforeignalter table student3 drop
查看show create table由于基本的表结构描述无法显示外键,所以在进行此操作前最好使用表fk就是刚刚设置的外键这里的需要注意的是:如果想要删除有关联的表,那么必先删除外键变成普通键删除外键后,原先的key 索引分类 1.普通索引:不附加任何限制条件,可创建在任何数据类型中参数可以设置索引为唯一性索引,在创建索引时,限制该索unique2.唯一性索引:使用引为唯一性索引,主键就是一种唯一性索引文档Word
.
varchar全文索引只能创建在char、参数可以设置索引为全文索引。3.全文索引:使用fulltextMyISAM查询数据量较大的字符串类型字段时,效果明显。但只有或text类型的字段上。存储引擎支持全文检索 4.单列索引:在表中单个字段上创建索引多列索引:在表中多个字段上创建的索引5.
空间索引只能建立在空间数据类6.空间索引:使用spatial参数可以设置索引为空间索引,型上比如geometry,并且不能为空,目前只有MyISAM存储引擎支持创建普通索引7.
index1(
tablecreate mysql>
,
id int->), (20-> name varchar sex boolean,
->
(id) index->
);
->
sec)
rows affected (Query OK, 0.110 show create table此处在id可查看字段上创建索引,创建唯一性索引index2( table mysql>create,
unique-> id int
),
20 name varchar( ->) index2_id(id indexASC->unique
);
->
sec)
0.12Query OK, 0 rows affected ( index2_id的索引字段创建了一个名为此处使用id 字段可以不设置唯一性约束,但这样一来索引就没有作用这里的id创建全文索引文档Word
.
index3( table mysql>create
,
id int->),
( 20-> info varchar index3_info(info)
fulltext index->MyISAM; =->)engine
sec)
rows affected (00.07Query OK,
存储引擎要注意创建全文索引时只能使用MyISAM创建单列索引index4( create
table mysql>,
-> id int
), varchar(30 subject ->)) index index4_st(subject(10->
);
->
sec)
0.120 rows affected (Query OK,
10
而索引长度则是字段长度是30,此处subject 这么做的目的在于提高查询速度,对于字符型的数据不用查询全部信息创建多列索引
index5( table mysql>create,
int-> id
), 20varchar( -> name
), (4-> sex char
index5_ns(name,sex) index->
);
->
sec)
0.10Query OK, 0 rows affected (文档Word
.
字段和sex字段创建索引列可以看出,这里使用了name
创建空间索引
index6( table>create mysql,
int-> id
, null space geometry ->not) space index-> spatial index6_sp(
MyISAM;
=-> )engine
sec) 0.07Query OK, 0 rows affected (
字段不能为空,还有存储引擎这里需要注意空间space
在已经存在的表上创建索引创建普通索引example0(id); index7_id on>createindex mysql sec) rows affected (0.07Query OK, 000 Warnings: Records: 0Duplicates:
的索引id字段上创建了一条名为index7_id这里在现有表的
创建唯一性索引example1(course_id); index8_id on unique index>mysql create sec)
0.16Query OK, 0 rows affected ( 0Warnings: 00Records: Duplicates:
文档Word
.
index此处只需要在关键字前面加上即可unique 字段,最要也设置唯一性约束条件至于表中的course_id
创建全文索引example2(info); on>mysql create fulltext index index9_info
sec) rows affected (0.07Query OK, 00 Records: 0 Duplicates: 0Warnings:
MyISAMfulltext关键字用来设置全文引擎,此处的表必须是存储引擎
创建单列索引)); 4create index index10_addr on example3(address(mysql> sec) 0.160Query OK, rows affected ( 0 Duplicates: 0Warnings: Records: 0
字段的长度是20,这里只查询4字节,不需要全部查询address此表中
创建多列索引example4(name,address); oncreate index index11_na mysql> sec) 0.16Query OK, 0 rows affected ( 0Warnings: Records: 0Duplicates: 0字段才能使用索引创建好之后,查询中必须有name
创建空间索引); example5(space spatial index index12_line on>mysql create sec) 0.07Query OK, 0 rows affected ( 0Warnings: 00Records: Duplicates:
文档Word
.
,还有空间数据类型这里需要注意存储引擎是MyISAM
alter table用语句来创建索引创建普通索引)); 20>alter table example6 add index
index13_n(name(mysql sec)
rows affected (0.16Query OK, 00 Records: 0Duplicates: 0 Warnings:
创建唯一性索引index14_id(id); indextablealter example7 add unique mysql>
sec) rows affected (0.20Query OK, 0 Warnings: Duplicates: 00Records: 0
创建全文索引index15_info(info); indexaddalter table fulltext example8 mysql>
sec) 0.080 rows affected (Query OK,
Warnings: 0Duplicates: 00Records:
创建单列索引)); 4index index16_addr(address( >alter table example9 add mysql sec) 0.160 rows affected (Query OK,
0Warnings: 0Duplicates: Records: 0
创建多列索引index17_in(id,name);
index example10 add tablealter>mysql
文档Word
.
sec) 0.16Query OK, 0 rows affected ( Warnings: 0 Duplicates: 0Records: 0
创建空间索引); index index18_space( table space example11 add spatial mysql>alter sec) rows affected (0.060Query OK,
Warnings: 0 Duplicates: 0Records: 0
到此,三种操作方式,每种索引类别的建立就都列举了对于索引,重要的是理解索引的概念,明白索引的种类更多的是自己的使用经验最后来看看索引的删除
删除索引example11; on dropindex index18_space mysql>
sec) 0.08Query OK, 0 rows affected ( Warnings: 0Duplicates: 0Records: 0这里是刚刚创建的一条索引是表名index18_space其中是索引名,example11 基本查询多字段查询:Select id,name,birth from student;
所有字段查询:Select * from student;
指定查询WhereSelect * from student where id = 901;
文档Word
.
In指定集合查询
Select * from student where birth in(1988,1990);
Not in 非围查询:
Select * from student where birth not in(1990,1988);
Between and指定围查询:
Select * from student where bitrth between 1986 and 1988;
Not between and不在指定围的查询
Select * from student where id not between 904 and 906;
Like字符串匹配查询
Select * from student where name like ‘';
Not like不匹配查询
Select * from student where name not like';
Null查询
Select * from student where address is null;
And多条件查询
Select * from student where name like ‘' and birth>1985;
Or多条件查询
Select * from student where id = 905 or birth=1988;
Distinct查询结果不重复
Select distinct sex from student;
Order by查询结果排序
Select * from order by birth;
Group by分组查询
Select sex,group_contact(name)from student group by sex;
Select sex,count(name)from student group by sex;
文档Word
.
正则表达式查询; 'Select * from student where birth regexp'1988|1990 限制查询结果数量LimitSelect * from student limit 2;
函数查询Select count(*)from score;
求和函数Sum()Select sum(grade)from score;
Avg()求平均值函数Select avg(grade)from score where c_name='计算机';Max()求最大值函数';英语'Select c_name,max(grade)from score where c_name= 求最小值函数Min() ';'Select c_name,min(grade) from score where c_name=中文Concat拼接函数)from score order by
stu_id; (',stu_id,')'Select Concat(c_name,'连接查询连接查询Select num,name,from emp,dep where emp.id=dep.id;外连接查询左连接查询Select num from emp left join dep on emp.id=dep.id;
此处不仅查询出了两表中id字段相匹配的信息,并且通过leftjoin查询emp表中所有指定()
左连接的意思是查出来是来连接在一起的两个表的左面的表的数据字段的信息右连接查询
文档Word
.
Select num from emp right join dep on emp.id=dep.id;
复合条件连接查询Select num,name,emp.id,sex,age,address from emp,dep where emp.id=dep.id
and age>=25;
复合条件连接查询是在进行连接查询的时候加入限制条件修改数据Insert语句实现插入数据语句实现更新数据Update 语句实现删除数据Delete 将查询结果插入到表中select * from person; (id,name)Insert into person 复制一表Create table per as select * from person;
更新数据set后面设置需要更新的容Update + 表名代表要更新的表,删除字