Mysql数据库学习总结Word格式文档下载.docx
- 文档编号:7181179
- 上传时间:2023-05-08
- 格式:DOCX
- 页数:13
- 大小:19.20KB
Mysql数据库学习总结Word格式文档下载.docx
《Mysql数据库学习总结Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《Mysql数据库学习总结Word格式文档下载.docx(13页珍藏版)》请在冰点文库上搜索。
完整性约束
是对字段进行限制,从而该字段达到我们期望的效果
设置表的主键:
主键能够标识表中的每条信息的唯一性。
(primarykey)
创建主键的目的在于快速查找到表中的某一条信息
多字段主键:
由多个属性组合而成
例如:
primarykey(id,course_id);
设置表的外键;
设置表的外键的作用在于建立与父表的联系
比如表A中的id是外键,表B中的id是主键
那么就可以称表B为父表,表A为子表
比如表B中id为123的学生删除后,表A中id为123的记录也随着消失
这样做的目的在于保证表的完整性。
设置表的非空约束:
设置表中的字段不为空
设置表的唯一性约束
唯一性约束指表中该字段的值不能重复出现,也就是给表中某个字段加上unique
设置表的属性值自动增加:
auto_increment主要用于为表中插入的新纪录自动生成唯一ID
一个表中只能由一个字段使用此约束,并且该字段必须为主键的一部分,约束的值ibixu是整型值。
设置表中属性的默认值
在表中插入一体哦新的记录时,如果没有为该字段赋值,那么数据库系统就会为该字段附上一条默认值。
修改表
修改表需要用到altertable
修改表名:
Altertablestudentrenameperson;
Rename用来命名
修改字段的数据类型
Altertablepersonmodifynamevarchar(20);
将原来的varchar(xx)修改为vaarchar(20)
修改字段名
Altertablepersonchangestu_namenamevarchar(25)
这里的stu_name是原名,name是新名,不管修不修改数据类型,后面的数据类型都要写
增加无完整性约束条件的字段
AltertablepersonaddsexBoolean;
此处的sex后面值跟了数据类型,而没有完整性约束条件
增加完整性约束体条件的字段
Altertablepersonaddageintnotnull;
增加了一条age字段,接着在后面加上了约束条件
增加额外的完整性约束条件
Altertablepersonaddprimarykeyfirst;
这样同样也用于多字段设置
在表头添加字段
Altertablepersonaddnumintprimarykeyfirst;
默认情况下添加到表尾,在添加语句后面加上first节能添加到表头
在指定位置添加字段
Altertablepersonaddbirthdateaftername;
这里添加一条新字段在name后面
删除字段
Altertablepersondropsex;
修改字段到第一个位置
Altetablepersonmodifyidintfirst
修改字段到指定的位置
Altertablepersonmodifynamevarchar(25)afterid;
我们要把name字段放到id后面,此处varchar(25)要写全
修改表的存储引擎
Altertableuserrenameperson;
增加表的外键:
altertablescoreaddconstraintfkforeignkey(stu_id)referencesstudent(id);
删除主键
ALTERTABLEpersonDROPPRIMARYKEY
删除了所有的主键
删除表的外键约束
altertablestudent3dropforeignkeyfk
由于基本的表结构描述无法显示外键,所以在进行此操作前最好使用showcreatetable查看表
这里的fk就是刚刚设置的外键
需要注意的是:
如果想要删除有关联的表,那么必先删除外键
删除外键后,原先的key变成普通键
索引分类
1.普通索引:
不附加任何限制条件,可创建在任何数据类型中
2.唯一性索引:
使用unique参数可以设置索引为唯一性索引,在创建索引时,限制该索引为唯一性索引,主键就是一种唯一性索引
3.全文索引:
使用fulltext参数可以设置索引为全文索引。
全文索引只能创建在char、varchar或text类型的字段上。
查询数据量较大的字符串类型字段时,效果明显。
但只有MyISAM存储引擎支持全文检索
4.单列索引:
在表中单个字段上创建索引
5.多列索引:
在表中多个字段上创建的索引
6.空间索引:
使用spatial参数可以设置索引为空间索引,空间索引只能建立在空间数据类型上比如geometry,并且不能为空,目前只有MyISAM存储引擎支持
7.创建普通索引
mysql>
createtableindex1(
->
idint,
namevarchar(20),
sexboolean,
index(id)
);
QueryOK,0rowsaffected(0.11sec)
此处在id字段上创建索引,showcreatetable可查看
创建唯一性索引
createtableindex2(
idintunique,
uniqueindexindex2_id(idASC)
QueryOK,0rowsaffected(0.12sec)
此处使用id字段创建了一个名为index2_id的索引
这里的id字段可以不设置唯一性约束,但这样一来索引就没有作用
创建全文索引
createtableindex3(
infovarchar(20),
fulltextindexindex3_info(info)
)engine=MyISAM;
QueryOK,0rowsaffected(0.07sec)
要注意创建全文索引时只能使用MyISAM存储引擎
创建单列索引
createtableindex4(
subjectvarchar(30),
indexindex4_st(subject(10))
此处subject字段长度是30,而索引长度则是10
这么做的目的在于提高查询速度,对于字符型的数据不用查询全部信息
创建多列索引
createtableindex5(
sexchar(4),
indexindex5_ns(name,sex)
QueryOK,0rowsaffected(0.10sec)
可以看出,这里使用了name字段和sex字段创建索引列
创建空间索引
createtableindex6(
spacegeometrynotnull,
spatialindexindex6_sp(space)
这里需要注意空间space字段不能为空,还有存储引擎
在已经存在的表上创建索引
创建普通索引
createindexindex7_idonexample0(id);
Records:
0Duplicates:
0Warnings:
0
这里在现有表的id字段上创建了一条名为index7_id的索引
createuniqueindexindex8_idonexample1(course_id);
QueryOK,0rowsaffected(0.16sec)
此处只需要在index关键字前面加上unique即可
至于表中的course_id字段,最要也设置唯一性约束条件
createfulltextindexindex9_infoonexample2(info);
fulltext关键字用来设置全文引擎,此处的表必须是MyISAM存储引擎
createindexindex10_addronexample3(address(4));
此表中address字段的长度是20,这里只查询4字节,不需要全部查询
createindexindex11_naonexample4(name,address);
索引创建好之后,查询中必须有name字段才能使用
createspatialindexindex12_lineonexample5(space);
这里需要注意存储引擎是MyISAM,还有空间数据类型
用altertable语句来创建索引
altertableexample6addindexindex13_n(name(20));
altertableexample7adduniqueindexindex14_id(id);
QueryOK,0rowsaffected(0.20sec)
altertableexample8addfulltextindexindex15_info(info);
QueryOK,0rowsaffected(0.08sec)
altertableexample9addindexindex16_addr(address(4));
altertableexample10addindexindex17_in(id,name);
altertableexample11addspatialindexindex18_space(space);
QueryOK,0rowsaffected(0.06sec)
到此,三种操作方式,每种索引类别的建立就都列举了
对于索引,重要的是理解索引的概念,明白索引的种类
更多的是自己的使用经验
最后来看看索引的删除
删除索引
dropindexindex18_spaceonexample11;
这里是刚刚创建的一条索引
其中index18_space是索引名,example11是表名
基本查询
多字段查询:
Selectid,name,birthfromstudent;
所有字段查询:
Select*fromstudent;
Where指定查询
Select*fromstudentwhereid=901;
In指定集合查询
Select*fromstudentwherebirthin(1988,1990);
Notin非范围查询:
Select*fromstudentwherebirthnotin(1990,1988);
Betweenand指定范围查询:
Select*fromstudentwherebitrthbetween1986and1988;
Notbetweenand不在指定范围的查询
Select*fromstudentwhereidnotbetween904and906;
Like字符串匹配查询
Select*fromstudentwherenamelike‘’;
Notlike不匹配查询
Select*fromstudentwherenamenotlike张’;
Null查询
Select*fromstudentwhereaddressisnull;
And多条件查询
Select*fromstudentwherenamelike‘张’andbirth>
1985;
Or多条件查询
Select*fromstudentwhereid=905orbirth=1988;
Distinct查询结果不重复
Selectdistinctsexfromstudent;
Orderby查询结果排序
Select*fromorderbybirth;
Groupby分组查询
Selectsex,group_contact(name)fromstudentgroupbysex;
Selectsex,count(name)fromstudentgroupbysex;
正则表达式查询
Select*fromstudentwherebirthregexp’1988|1990’;
Limit限制查询结果数量
Select*fromstudentlimit2;
函数查询
Selectcount(*)fromscore;
Sum()求和函数
Selectsum(grade)fromscore;
Avg()求平均值函数
Selectavg(grade)fromscorewherec_name=’计算机’;
Max()求最大值函数
Selectc_name,max(grade)fromscorewherec_name=’英语’;
Min()求最小值函数
Selectc_name,min(grade)fromscorewherec_name=’中文’;
Concat拼接函数
SelectConcat(c_name,’(’,stu_id,’)’)fromscoreorderbystu_id;
连接查询
内连接查询
Selectnum,name,fromemp,depwhereemp.id=dep.id;
外连接查询
左连接查询
Selectnumfromempleftjoindeponemp.id=dep.id;
此处不仅查询出了两表中id字段相匹配的信息,并且通过leftjoin查询emp表中所有指定字段的信息(左连接的意思是查出来是来连接在一起的两个表的左面的表的数据)
右连接查询
Selectnumfromemprightjoindeponemp.id=dep.id;
复合条件连接查询
Selectnum,name,emp.id,sex,age,addressfromemp,depwhereemp.id=dep.idandage>
=25;
复合条件连接查询是在进行连接查询的时候加入限制条件
修改数据
Insert语句实现插入数据
Update语句实现更新数据
Delete语句实现删除数据
将查询结果插入到表中
Insertintoperson(id,name)select*fromperson;
复制一张表
Createtableperasselect*fromperson;
更新数据
Update+表名代表要更新的表,set后面设置需要更新的内容
删除指定记录需要跟上心智条件
否则将记录一条一条的删除
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mysql 数据库 学习 总结
![提示](https://static.bingdoc.com/images/bang_tan.gif)