Mysql数据库学习总结_第1页
Mysql数据库学习总结_第2页
Mysql数据库学习总结_第3页
Mysql数据库学习总结_第4页
Mysql数据库学习总结_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、mysql数据库学习总结 数据库的基本操作:创建 删除 查看 create database school; 用于创建数据库,并且数据库的名字不可以更改 show create database; show databases; 用来查看创建数据库的语句 drop database; 用于删除数据库表的基本操作:create table;用于创建表,table后面加表名称 create table studentid int;name varchar(10);sex boolean; show tables; 用于显示数据库中的所有表 describe student;这里显示了字段、数据类型

2、、是否为空、主外键、默认值和额外信息 show create table; 显示创建表时的详细信息 drop table student;删除表的操作完整性约束 是对字段进行限制,从而该字段达到我们期望的效果设置表的主键:主键能够标识表中的每条信息的唯一性。(primary key) 创建主键的目的在于快速查找到表中的某一条信息多字段主键:由多个属性组合而成 例如:primary key(id,course_id);设置表的外键; 设置表的外键的作用在于建立与父表的联系 比如表a中的id是外键,表b中的id是主键 那么就可以称表b为父表,表a为子表比如表b中id为123的学生删除后,表a中id

3、为123的记录也随着消失这样做的目的在于保证表的完整性。设置表的非空约束:设置表中的字段不为空设置表的唯一性约束 唯一性约束指表中该字段的值不能重复出现,也就是给表中某个字段加上unique设置表的属性值自动增加: auto_increment 主要用于为表中插入的新纪录自动生成唯一id一个表中只能由一个字段使用此约束,并且该字段必须为主键的一部分,约束的值ibixu是整型值。设置表中属性的默认值在表中插入一体哦新的记录时,如果没有为该字段赋值,那么数据库系统就会为该字段附上一条默认值。修改表修改表需要用到alter table修改表名: alter table student rename

4、person;rename 用来命名修改字段的数据类型 alter table person modify name varchar(20);将原来的varchar(xx)修改为vaarchar(20)修改字段名 alter table person change stu_name name varchar(25)这里的stu_name是原名,name是新名,不管修不修改数据类型,后面的数据类型都要写增加无完整性约束条件的字段 alter table person add sex boolean;此处的sex 后面值跟了数据类型,而没有完整性约束条件增加完整性约束体条件的字段 alter ta

5、ble 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后面删除字段 alter table pe

6、rson drop sex;修改字段到第一个位置alte table person modify id int first修改字段到指定的位置 alter table person modify name varchar(25) after id;我们要把name字段放到id后面,此处varchar(25)要写全修改表的存储引擎alter table user rename person;增加表的外键: alter table score add constraint fk foreign key(stu_id) references student(id);删除主键 alter table

7、person drop primary key删除了所有的主键删除表的外键约束 alter table student3 drop foreign key fk由于基本的表结构描述无法显示外键,所以在进行此操作前最好使用show create table查看表这里的fk就是刚刚设置的外键需要注意的是:如果想要删除有关联的表,那么必先删除外键删除外键后,原先的key变成普通键索引分类1. 普通索引:不附加任何限制条件,可创建在任何数据类型中2. 唯一性索引:使用unique参数可以设置索引为唯一性索引,在创建索引时,限制该索引为唯一性索引,主键就是一种唯一性索引3. 全文索引:使用fulltex

8、t参数可以设置索引为全文索引。全文索引只能创建在char、varchar或text类型的字段上。查询数据量较大的字符串类型字段时,效果明显。但只有myisam存储引擎支持全文检索4. 单列索引:在表中单个字段上创建索引5. 多列索引:在表中多个字段上创建的索引6. 空间索引:使用spatial参数可以设置索引为空间索引,空间索引只能建立在空间数据类型上比如geometry,并且不能为空,目前只有myisam存储引擎支持7. 创建普通索引mysql create table index1( - id int, - name varchar(20), - sex boolean, - index(

9、id) - );query ok, 0 rows affected (0.11 sec)此处在id字段上创建索引,show create table可查看创建唯一性索引mysql create table index2( - id int unique, - name varchar(20), - unique index index2_id(id asc) - );query ok, 0 rows affected (0.12 sec)此处使用id字段创建了一个名为index2_id的索引这里的id字段可以不设置唯一性约束,但这样一来索引就没有作用创建全文索引mysql create tab

10、le index3( - id int, - info varchar(20), - fulltext index index3_info(info) - )engine=myisam;query ok, 0 rows affected (0.07 sec)要注意创建全文索引时只能使用myisam存储引擎创建单列索引mysql create table index4( - id int, - subject varchar(30), - index index4_st(subject(10) - );query ok, 0 rows affected (0.12 sec)此处subject字段

11、长度是30,而索引长度则是10这么做的目的在于提高查询速度,对于字符型的数据不用查询全部信息创建多列索引mysql create table index5( - id int, - name varchar(20), - sex char(4), - index index5_ns(name,sex) - );query ok, 0 rows affected (0.10 sec)可以看出,这里使用了name字段和sex字段创建索引列创建空间索引mysql create table index6( - id int, - space geometry not null, - spatial i

12、ndex index6_sp(space) - )engine=myisam;query ok, 0 rows affected (0.07 sec)这里需要注意空间space字段不能为空,还有存储引擎在已经存在的表上创建索引创建普通索引mysql create index index7_id on example0(id);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0这里在现有表的id字段上创建了一条名为index7_id的索引创建唯一性索引mysql create unique index

13、index8_id on example1(course_id);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0此处只需要在index关键字前面加上unique即可至于表中的course_id字段,最要也设置唯一性约束条件创建全文索引mysql create fulltext index index9_info on example2(info);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0full

14、text关键字用来设置全文引擎,此处的表必须是myisam存储引擎创建单列索引mysql create index index10_addr on example3(address(4);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0此表中address字段的长度是20,这里只查询4字节,不需要全部查询创建多列索引mysql create index index11_na on example4(name,address);query ok, 0 rows affected (0.16 sec)r

15、ecords: 0 duplicates: 0 warnings: 0索引创建好之后,查询中必须有name字段才能使用创建空间索引mysql create spatial index index12_line on example5(space);query ok, 0 rows affected (0.07 sec)records: 0 duplicates: 0 warnings: 0这里需要注意存储引擎是myisam,还有空间数据类型用alter table语句来创建索引创建普通索引mysql alter table example6 add index index13_n(name(2

16、0);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0创建唯一性索引mysql alter table example7 add unique index index14_id(id);query ok, 0 rows affected (0.20 sec)records: 0 duplicates: 0 warnings: 0创建全文索引mysql alter table example8 add fulltext index index15_info(info);query ok, 0 rows

17、 affected (0.08 sec)records: 0 duplicates: 0 warnings: 0创建单列索引mysql alter table example9 add index index16_addr(address(4);query ok, 0 rows affected (0.16 sec)records: 0 duplicates: 0 warnings: 0创建多列索引mysql alter table example10 add index index17_in(id,name);query ok, 0 rows affected (0.16 sec)recor

18、ds: 0 duplicates: 0 warnings: 0创建空间索引mysql alter table example11 add spatial index index18_space(space);query ok, 0 rows affected (0.06 sec)records: 0 duplicates: 0 warnings: 0到此,三种操作方式,每种索引类别的建立就都列举了对于索引,重要的是理解索引的概念,明白索引的种类更多的是自己的使用经验最后来看看索引的删除删除索引mysql drop index index18_space on example11;query o

19、k, 0 rows affected (0.08 sec)records: 0 duplicates: 0 warnings: 0这里是刚刚创建的一条索引其中index18_space是索引名,example11是表名基本查询多字段查询: select id,name,birth from student;所有字段查询:select * from student;where指定查询select * from student where id = 901;in指定集合查询select * from student where birth in(1988,1990);not in 非范围查询:se

20、lect * 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

21、not like张;null查询select * from student where address is null;and多条件查询select * from student where name like 张 and birth1985;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_

22、contact(name)from student group by sex;select sex,count(name)from student group by sex;正则表达式查询select * from student where birth regexp1988|1990;limit限制查询结果数量select * 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 wher

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论