当前位置:文档之家› Mysql数据库学习总结

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;删除表的操作完整性约束就是对字段进行限制,从而该字段达到我们期望的效果设置表的主键:主键能够标识表中的每条信息的唯一性。

(primary key)创建主键的目的在于快速查找到表中的某一条信息多字段主键:由多个属性组合而成例如:primary key(id,course_id);设置表的外键;设置表的外键的作用在于建立与父表的联系比如表A中的id就是外键,表B中的id就是主键那么就可以称表B为父表,表A为子表比如表B中id为123的学生删除后,表A中id为123的记录也随着消失这样做的目的在于保证表的完整性。

设置表的非空约束:设置表中的字段不为空设置表的唯一性约束唯一性约束指表中该字段的值不能重复出现,也就就是给表中某个字段加上unique 设置表的属性值自动增加:auto_increment 主要用于为表中插入的新纪录自动生成唯一ID一个表中只能由一个字段使用此约束,并且该字段必须为主键的一部分,约束的值ibixu 就是整型值。

设置表中属性的默认值在表中插入一体哦新的记录时,如果没有为该字段赋值,那么数据库系统就会为该字段附上一条默认值。

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

全文索引只能创建在char、varchar或text类型的字段上。

查询数据量较大的字符串类型字段时,效果明显。

但只有MyISAM 存储引擎支持全文检索4.单列索引:在表中单个字段上创建索引5.多列索引:在表中多个字段上创建的索引6.空间索引:使用spatial参数可以设置索引为空间索引,空间索引只能建立在空间数据类型上比如geometry,并且不能为空,目前只有MyISAM存储引擎支持7.创建普通索引mysql>create table index1(-> id int,-> name varchar(20),-> sex boolean,->index(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 table 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字段长度就是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 index 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 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: 0fulltext关键字用来设置全文引擎,此处的表必须就是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)Records: 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(20));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 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)Records: 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 OK, 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 非范围查询: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;正则表达式查询Select * from student where birth regexp’1988|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 where c_name=’英语’;Min()求最小值函数Select c_name,min(grade) from score where c_name=’中文’;Concat拼接函数Select Concat(c_name,’(’,stu_id,’)’)from score order by stu_id;连接查询内连接查询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表中所有指定字段的信息(左连接的意思就是查出来就是来连接在一起的两个表的左面的表的数据)右连接查询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语句实现删除数据将查询结果插入到表中Insert into person(id,name)select * from person;复制一张表Create table per as select * from person;更新数据Update + 表名代表要更新的表,set后面设置需要更新的内容删除字段删除指定记录需要跟上心智条件否则将记录一条一条的删除。

相关主题