码迷,mamicode.com
首页 > 数据库 > 详细

MySQL数据库基础知识(二)

时间:2017-12-10 18:57:13      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:base   正则表达式   作用   学生   amp   from   let   写法   div   

21.查询数据

格式:
    select [字段列表]|* from 表名
    [where 搜索条件]
    [group by 分组字段 [having 子条件]]
    [order by 排序 asc|desc]
    [limit 分页参数]

mysql>select * from stu;

+----+----------+-----+-----+---------+
| id | name     | age | sex | classid |
+----+----------+-----+-----+---------+
|  1 | zhangsan |  20 | m   | lamp138  |
|  2 | lisi     |  20 | m   | lamp138  |
|  3 | wangwu   |  21 | w   | lamp138  |
|  4 | zhaoliu  |  25 | w   | lamp94  |
|  5 | uu01     |  26 | m   | lamp94  |
|  6 | uu02     |  28 | w   | lamp92  |
|  7 | qq02     |  24 | m   | lamp92  |
|  8 | uu03     |  32 | m   | lamp138  |
|  9 | qq03     |  23 | w   | lamp94  |
| 10 | aa       |  19 | m   | lamp138  |
| 11 | sad      |  35 | m   | lamp94  |
| 12 | tt       |  25 | m   | lamp92  |
| 13 | wer      |  25 | w   | lamp94  |
| 14 | xx       |  25 | m   | lamp92  |
| 15 | kk       |   0 | w   | lamp94  |
+----+----------+-----+-----+---------+

 

##where条件查询
1. 查询班级为lamp138期的学生信息
mysql> select * from stu where classid=‘lamp138‘;

2. 查询lamp138期的男生信息(sex为m)
mysql> select * from stu where classid=‘lamp138‘ and sex=‘m‘;

3. 查询id号值在10以上的学生信息
mysql> select * from  stu where id>10;

4. 查询年龄在20至25岁的学生信息
mysql> select * from stu where age>=20 and age<=25;
mysql> select * from stu where age between 20 and 25;

5. 查询年龄不在20至25岁的学生信息
mysql> select * from stu where age not between 20 and 25;
mysql> select * from stu where age<20 or age>25;

6. 查询id值为1,8,4,10,14的学生信息
select * from stu where id in(1,8,4,10,14);
mysql> select * from stu where id=1 or id=8 or id=4 or id=10 or id=14;

7. 查询lamp138和lamp94期的女生信息
mysql> select * from stu where classid in(‘lamp138‘,‘lamp94‘) and sex=‘w‘;
mysql> select * from stu where (classid=‘lamp138‘ or classid=‘lamp94‘) and sex=‘w

##LIKE 子句

这时我们就需要在 WHERE 子句中使用 SQL LIKE 子句。

LIKE 子句中使用百分号%字符来表示任意字符,类似于UNIX或正则表达式中的星号*。

如果没有使用百分号%, LIKE 子句与等号=的效果是一样的。

LIKE 通常与 % 一同使用,类似于一个元字符的搜索。
你可以使用 AND 或者 OR 指定一个或多个条件。
你可以在 DELETE 或 UPDATE 命令中使用 WHERE...LIKE 子句来指定条件。
可以使用regexp正则来代替 like

1.查询name字段值是以zh开头的所有信息
mysql> select * from stu where name like "zh%";
mysql> select * from stu where name regexp  "^zh"; --正则写法
+----+----------+------+-----+---------+
| id | name     | age  | sex | classid |
+----+----------+------+-----+---------+
| 14 | zhangle  |   29 | m   |       5 |
|  1 | zhangsan |   20 | w   |       1 |
|  4 | zhaoliu  |   21 | m   |       4 |
+----+----------+------+-----+---------+

2.查询姓名name中含有ang子串的所有信息
mysql> select * from stu where name like "%ang%";
mysql> select * from stu where name regexp  "ang";
+----+-----------+------+-----+---------+
| id | name      | age  | sex | classid |
+----+-----------+------+-----+---------+
|  1 | zhangsan  |   20 | w   |       1 |
|  3 | wangwu    |   22 | w   |       5 |
| 10 | xiaozhang |   19 | w   |       1 |
| 13 | wangwen   |   27 | w   |       2 |
| 14 | zhangle   |   29 | m   |       5 |
+----+-----------+------+-----+---------+

3.查询姓名是任意四位字符构成的信息。
mysql> select * from stu where name like "____";
mysql> select * from stu where name regexp "^[a-z0-9]{4}$";
+----+------+------+-----+---------+
| id | name | age  | sex | classid |
+----+------+------+-----+---------+
|  2 | lisi |   25 | m   |       2 |
|  5 | uu01 |   27 | w   |       1 |
|  6 | uu02 |   25 | m   |       2 |
|  7 | uu03 |   28 | w   |       2 |
|  8 | uu05 |   22 | m   |       4 |
+----+------+------+-----+---------+

创建一个表结构和另一个表一样的结构

create table t3 like t1;
那么现在t3表的结构和t1表的结构是一样的,但是要注意数据是没有的

 

##MySQL的统计函数(聚合函数):max() min() count() sum() avg()

1.获取学生表中最大、最小以及平均年龄是多少?
mysql> select max(age),min(age),avg(age) from stu;

2.获取学生表中男生m的数量
mysql> select count(*) from stu where sex=‘m‘;

##GROUP BY 语句 分组

GROUP BY 语句根据一个或多个列对结果集进行分组。

统计班级信息,按性别分组,并统计每组人数;
mysql> select sex,count(*) from stu  group by sex;

1.统计每个班级的人数
nysql> select classid,count(*) from stu  group by classid;

2.统计每个班级的,男生和女生各多少人数。
mysql> select classid,sex,count(*) from stu  group by classid,sex;

##ORDER BY 排序 -- asc 默认升序 desc 降序

我们知道从 MySQL 表中使用 SQL SELECT 语句来读取数据。

如果我们需要对读取的数据进行排序,我们就可以使用 MySQL 的ORDER BY子句来设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。

你可以使用任何字段来作为排序的条件,从而返回排序后的查询结果。
你可以设定多个字段来排序。
你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升序排列。
你可以添加 WHERE...LIKE 子句来设置条件。

1.按年龄升序排序查询学生信息
mysql> select * from stu order by age;
mysql> select * from stu order by age asc;  --默认asc升序 可省略

2.年龄降序排序
mysql> select * from stu order by age desc;

3.查询学生信息,按班级做升序排序,相同班级按年龄降序排序
mysql> select * from stu order by classid asc,age desc;

##LIMIT 关键字 查询部分数据

-- 例如: .... limit m; 查询数据只显示前m条

-- 例如: .... limit m,n; 排除前m条,然后再查询出前n条

1.查询前5条信息
mysql> select * from stu limit 5;

2.排除前2条后再获取4条信息
mysql> select * from stu limit 2,4;

22.修改数据

 格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n... where 条件 

    -- 将id为11的age改为35,sex改为m值
    mysql> update stu set age=35,sex=‘m‘ where id=11;
   
    -- 将id值为12和14的数据值sex改为m,classid改为lamp92
    mysql> update stu set sex=‘m‘,classid=‘lamp92‘ where id=12 or id=14 --等价于下面
    mysql> update stu set sex=‘m‘,classid=‘lamp92‘ where id in(12,14);

23.删除操作

格式:delete from 表名 [where 条件]

     -- 删除stu表中id值为100的数据
    mysql> delete from stu where id=100;

    -- 删除stu表中id值为20到30的数据
    mysql> delete from stu where id>=20 and id<=30;

    -- 删除stu表中id值为20到30的数据(等级于上面写法)
    mysql> delete from stu where id between 20 and 30;

    -- 删除stu表中id值大于200的数据
    mysql> delete from stu where id>200;

24.导入和导出

注意: 在操作导入和导出数据的时候 不要登录到mysql服务中,要退出mysql服务进行操作

-- 将lamp138数据库里面的所有的表导出
C:\>mysqldump -u root -p lamp138 > C:\lamp138.sql
Enter password:

---- 将lamp138数据库中的stu表导出
C:\>mysqldump -u root -p lamp138 stu > C:\lamp138_stu.sql
Enter password:

-- 将lamp138库中的所有表导入
   
C:\>mysql -u root -p lamp138 < C:\lamp138.sql
Enter password:

-- 将lamp138库中stu表导入
C:\>mysql -u root -p lamp138 < C:\lamp138_stu.sql
Enter password:

*********************
-- 将所有的数据库进行导出
mysqldump -uroot -p密码  --all-databases  --events > /tmp/bak.sql       所有数据库备份

 

25.MySQL的权限管理

格式:
	grant 权限 on 数据库.数据表 to ‘用户名‘@‘登录主机‘  identified  by ‘密码‘
	
	刷新权限
	flush privileges;

举例:
	grant select,insert,update,delete on *.* to  ‘wjs‘@‘%‘ identified by ‘12345‘;       

    安全的做法
   	grant select,insert,update,delete on pass.* to ‘root‘@‘localhost‘  identified by ‘12345‘;   
    只针对localhost主机里面的  pass数据库里面的数据表进行增删该查    

删除用户
drop user ‘xxoo‘@‘%‘  

 

26.索引的作用:

索引是数据库中用来提高搜索性能的。我们通常在做数据库优化的时候通常先做索引的优化,数据量少的时候没效果,数据越多效果越明显。

 

查看表中都有哪些索引

show index from 表名\G

索引的分类:

常规索引(index)

最基本的索引,没有任何限制

 

添加表字段的常规索引
    create index 索引名 on 表名(字段名)
    alter table 表名 add index 索引名(字段名)    

在创建表的时候也可以进行添加
    create table t2(
            id int(10) unsigned auto_increment primary key,
            name varchar(255), index  索引名(字段名)
        );

删除表字段常规索引**
    drop index 索引名 on 表名
    alter table 表名 drop index 索引名

唯一索引(unique)

唯一索引是可以给每个字段进行添加的,添加完了之后字段里面的值就不可以重复了,主键索引和唯一索引类似,
但是数据表里的主键索引只能加在一个字段里(一般都加在id上),id是自增的,索引不会有重复的时候出现

 

添加表字段的唯一索引
    create unique index 索引名 on 表名(字段名)
    alter table 表名 add unique 索引名(字段名)

在创建表的时候也可以进行添加
    create table t2(
            id int(10) unsigned auto_increment primary key,
            name varchar(255), unique  索引名(字段名)
        );

删除唯一索引
    drop index 索引名 on 表名

 

主键索引(primary key)

主键索引是关系数据库中最常见的索引类型,主要作用是确定数据表里一条特定的数据记录的位置。
我们可以在字段后添加PRIMARY KEY来对字段设置为主键索引。

注意:
	1.最好为每张表指定一个主键,但不是必须指定。
	2.一个表只能指定一个主键,而且主键的值不能为空
	3.主键可以有多个候选索引(例如NOT NULL,AUTO_INCREMENT)

添加表字段的主键索引
    alter table 表名 add  primary key (字段名)
添加自增
    alter table 表名 modify id int(4) auto_increment
删除主键索引
    如果字段有了auto_increment 和 primary key的时候,需要删除主键的话,
    先删除自增,然后再删除主键    
    删除自增
    alter table 表名 change 字段名  字段名 类名
    删除主键
    alter table 表名 drop primary key 

全文索引(fulltext)

全文索引在MySQL中是一个FULLTEXT类型索引,但FULLTEXT索引只能用于MyISAM表,
并且只可以在CHAR、VARCHAR或TEXT类型的列上创建,也允许创建在一个或多个数据列上。

添加表字段的全文索引
    alter table 表名 add fulltext 索引名(字段名)
删除全文索引
    drop index (索引名) on 表名
    alter table 表名 drop index 索引名

 

注意:如果在创建索引的时候,不添加索引名的话 默认会把字段名当做索引名

MySQL数据库基础知识(二)

标签:base   正则表达式   作用   学生   amp   from   let   写法   div   

原文地址:http://www.cnblogs.com/streamerfire/p/8017655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!