您好,欢迎来到小侦探旅游网。
搜索
您的当前位置:首页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: 0

fulltext关键字用来设置全文引擎,此处的表必须是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后面设置需要更新的内容 删除字段

删除指定记录需要跟上心智条件 否则将记录一条一条的删除

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- xiaozhentang.com 版权所有 湘ICP备2023022495号-4

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务