Mysql基础
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mysql基础相关的知识,希望对你有一定的参考价值。
mysql的历史度娘上一堆,就不再介绍了。
本文依照此路径学习Mysql数据库:数据库->表->数据
首先启动Mysql服务,然后通过控制台命令登入root账户输入密码回车
C:\Users>mysql -u root -p
Enter password: ******
闲话不说,直接进入Mysql命令的学习。
一:数据库
1.1:查看当前所有的数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema | -- 此数据库存放mysql的元数据,基础数据。
| mysql | -- mysql配置数据库,包括用户信息(账户名、密码、权限管理)等。
| performance_schema | -- mysql数据库软件的运行数据、日志信息、性能数据
+--------------------+
3 rows in set (0.00 sec)
1.2:创建数据库,并命名为isole
mysql> create database isole;
Query OK, 1 row affected (0.00 sec)
1.3:查看数据库的默认字符集
mysql> show create database isole;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| isole | CREATE DATABASE `isole` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
由上可知,此数据库的编码为latin1,我们都知道通常我们使用utf-8编码
1.4:修改数据库的默认字符集并且查看,注意修改的时候utf-8写成utf8
mysql> alter database isole default character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show create database isole;
+----------+----------------------------------------------------------------+
| Database | Create Database |
+----------+----------------------------------------------------------------+
| isole | CREATE DATABASE `isole` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
1 row in set (0.00 sec)
1.5:删除数据库
mysql> drop database isole;
Query OK, 0 rows affected (0.00 sec)
1.6:在创建数据库的时候指定编码的格式:
mysql> create database isole default character set utf8;
Query OK, 1 row affected (0.00 sec)
二:表的管理
2.1操作表首先要选择相关的数据库,此处我们选择isole数据库
mysql> use isole;
Database changed
mysql>
2.2创建表student 字段设sid sname sage三个
mysql> create table student( -- student是表的名称
-> sid int, -- int 是类型
-> sname varchar(30), -- (30)是字段长度
-> sage int -- sage是字段名称
-> );
Query OK, 0 rows affected (0.18 sec)
mysql>
2.3查看当前数据库中所有的表
mysql> show tables;
+-----------------+
| Tables_in_isole |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql>
2.4查看某个表字段的结构,此处以student为例
mysql> desc student; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | sid | int(11) | YES | | NULL | | | sname | varchar(30) | YES | | NULL | | | sage | int(11) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
2.5:student表增加字段sgender varchar(2)
mysql> alter table student add column sgender varchar(2); Query OK, 0 rows affected (0.34 sec) Records: 0 Duplicates: 0 Warnings: 0
2.6:student表修改字段sgender改成gender
mysql> alter table student change sgender gender varchar(2); Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0
2.7student表中的sid int修改字段类型为sid varchar(5)
mysql> alter table student modify sid varchar(5); Query OK, 0 rows affected (0.41 sec)
2.8student表删除字段gender
mysql> alter table student drop gender; Query OK, 0 rows affected (0.35 sec) Records: 0 Duplicates: 0 Warnings: 0
2.9修改表的名称student为teacher
mysql> alter table student rename teacher; Query OK, 0 rows affected (0.06 sec)
2.10删除表teacher
mysql> drop table teacher; Query OK, 0 rows affected (0.10 sec)
三:数据
先创建学生表,字段以及类型如下
mysql> create table student(
-> sid int,
-> sname varchar(20),
-> sgender varchar(2),
-> sage int
-> );
Query OK, 0 rows affected (0.18 sec)
3.1增加数据
3.1.1增加张三、李四的信息,插入对应字段的所有信息,此时可以不写出字段
mysql> insert into student values(1,‘张三‘,‘男‘,18); Query OK, 1 row affected (0.06 sec) mysql> insert into student values(2,‘李四‘,‘女‘,20); Query OK, 1 row affected (0.06 sec)
3.1.2增加王二的信息,此时不知道王二的年龄,注意字段与值必须一一对应!
mysql> insert into student(sid,sname,sgender) values(3,‘王二‘,‘女‘);
Query OK, 1 row affected (0.07 sec)
3.2修改数据
3.2.1修改所有的数据(建议少用,因为会影响整个表的每个学生),此处将所有学生的性别设置为‘女‘
mysql> update student set sgender=‘女‘;
Query OK, 1 row affected (0.09 sec)
Rows matched: 3 Changed: 1 Warnings: 0
3.2.2带条件的修改,修改id为1的学生,将其性别修改为男,年龄修改为22
mysql> update student set sgender=‘男‘,sage=22 where sid=1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1 Changed: 1 Warnings: 0
3.3删除数据
3.3.1删除所有数据
mysql>delete from student;
Query OK, 3 row affected (0.06 sec)
3.3.2带条件的删除 删除sid=3的学生信息
mysql> delete from student where sid=3;
Query OK, 1 row affected (0.06 sec)
注意:数据的删除有两种,一种delete删除,另外一种是truncate删除
区别:
delete from: 可以全表删除 1)可以带条件的删除 2)删除表的数据不删除表的约束 3)删除的数据可以回滚
truncate table:可以全表删除 1)不可待条件的删除 2)删除表的数据以及表的约束 3)删除的数据不能够回滚
四、查询数据
4.1查询表中的所有数据,*代表所有字段
mysql> select *from student;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 1 | 张三 | 男 | 22 |
| 2 | 李四 | 女 | 20 |
| 3 | 王二 | 女 | NULL |
+------+-------+---------+------+
3 rows in set (0.00 sec)
4.2查询某些列的数据,例如查询学生id和姓名
mysql> select sid,sname from student;
+------+-------+
| sid | sname |
+------+-------+
| 1 | 张三 |
| 2 | 李四 |
| 3 | 王二 |
+------+-------+
3 rows in set (0.00 sec)
4.3查询时去除重复的字段,例如只查询有多少种性别
mysql> select distinct sgender from student;
+---------+
| sgender |
+---------+
| 男 |
| 女 |
+---------+
2 rows in set (0.00 sec)
4.4查询时指定别名 AS
mysql> select sid as ‘编号‘,sname as ‘姓名‘ from student;
+------+------+
| 编号 | 姓名 |
+------+------+
| 1 | 张三 |
| 2 | 李四 |
| 3 | 王二 |
+------+------+
3 rows in set (0.00 sec)
4.5条件查询,and(且)和or(或)
查询id为2且姓名为‘李四‘的学生信息
mysql> select *from student where sid=2 and sname=‘李四‘;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 2 | 李四 | 女 | 20 |
+------+-------+---------+------+
1 row in set (0.00 sec)
查询id=3或者姓名为张三的学生信息
mysql> select * from student where sid=2 or sname=‘张三‘;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 1 | 张三 | 男 | 22 |
| 2 | 李四 | 女 | 20 |
+------+-------+---------+------+
2 rows in set (0.00 sec)
为王二同学录入年龄信息
mysql> update student set sage=12 where sname=‘王二‘; Query OK, 1 row affected (0.06 sec) Rows matched: 1 Changed: 1 Warnings: 0
查询student表中年龄小于等于20岁的学生
mysql> select *from student where sage<=20;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 2 | 李四 | 女 | 20 |
| 3 | 王二 | 女 | 12 |
+------+-------+---------+------+
2 rows in set (0.00 sec)
查询student 表中年龄在19-21之间的学生
-- 方法1: mysql> select *from student where sage>19 and sage<21; +------+-------+---------+------+ | sid | sname | sgender | sage | +------+-------+---------+------+ | 2 | 李四 | 女 | 20 | +------+-------+---------+------+ 1 row in set (0.00 sec) -- 方法2: mysql> select *from student where sage between 19 and 21; +------+-------+---------+------+ | sid | sname | sgender | sage | +------+-------+---------+------+ | 2 | 李四 | 女 | 20 | +------+-------+---------+------+ 1 row in set (0.00 sec)
-- 查询性别不是‘男‘的学生
mysql> select *from student where sgender <> ‘男‘;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 2 | 李四 | 女 | 20 |
| 3 | 王二 | 女 | 12 |
+------+-------+---------+------+
2 rows in set (0.00 sec)
模糊查询Like
-- % 表示任意个任意字符
-- _ 表示一个任意字符
查询姓‘李‘的学生的信息
mysql> select *from student where sname like ‘李%‘;
+------+-------+---------+------+
| sid | sname | sgender | sage |
+------+-------+---------+------+
| 2 | 李四 | 女 | 20 |
+------+-------+---------+------+
1 row in set (0.00 sec)
增加字段math并且录入成绩
mysql> alter table student add math float;
Query OK, 0 rows affected (0.33 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update student set math=87 where sid=1;
Query OK, 1 row affected (0.07 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set math=57 where sid=2;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> update student set math=97 where sid=3;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
聚合查询
常见的聚合函数:sum() avg() max() min() count()
mysql> select sum(math) as‘数学总分‘ from student;
+----------+
| 数学总分 |
+----------+
| 241 |
+----------+
mysql> select avg(math) as ‘数学平均分‘ from student;
+-------------------+
| 数学平均分 |
+-------------------+
| 80.33333333333333 |
+-------------------+
mysql> select max(math) as ‘数学最高分‘ from student;
+------------+
| 数学最高分 |
+------------+
| 97 |
+------------+
mysql> select min(math) as ‘数学最低分‘ from student;
+------------+
| 数学最低分 |
+------------+
| 57 |
+------------+
mysql> select count(*) as‘当前人数为‘ from student;
+------------+
| 当前人数为 |
+------------+
| 3 |
+------------+
查询排序 order by DESC /ASC
数学成绩的倒序
mysql> select *from student order by math desc;
+------+-------+---------+------+------+
| sid | sname | sgender | sage | math |
+------+-------+---------+------+------+
| 3 | 王二 | 女 | 12 | 97 |
| 1 | 张三 | 男 | 22 | 87 |
| 2 | 李四 | 女 | 20 | 57 |
+------+-------+---------+------+------+
数学成绩的正序
mysql> select *from student order by math asc;
+------+-------+---------+------+------+
| sid | sname | sgender | sage | math |
+------+-------+---------+------+------+
| 2 | 李四 | 女 | 20 | 57 |
| 1 | 张三 | 男 | 22 | 87 |
| 3 | 王二 | 女 | 12 | 97 |
+------+-------+---------+------+------+
数学成绩倒序,年龄正序(当数学成绩一样的时候以年龄正序排序)
mysql> select *from student order by math desc,sage asc;
+------+-------+---------+------+------+
| sid | sname | sgender | sage | math |
+------+-------+---------+------+------+
| 3 | 王二 | 女 | 12 | 97 |
| 1 | 张三 | 男 | 22 | 87 |
| 2 | 李四 | 女 | 20 | 57 |
分组查询 group by
mysql> select sgender,count(*)from student group by sgender;
+---------+----------+
| sgender | count(*) |
+---------+----------+
| 女 | 2 |
| 男 | 1 |
+---------+----------+
2 rows in set (0.00 sec)
分页查询 limit
起始行从0开始 公式:select *from student limit(当前页-1)*每页显示多少条,每页显示多少条;
mysql> select *from student limit 0,2;
+------+-------+---------+------+------+
| sid | sname | sgender | sage | math |
+------+-------+---------+------+------+
| 1 | 张三 | 男 | 22 | 87 |
| 2 | 李四 | 女 | 20 | 57 |
+------+-------+---------+------+------+
特别板块,判空条件(null / ‘ ‘)
关键字: is null / is not null / =" / <>" null代表没有该值,空字符串有值且对应ASCII码值是:32
-- 判断null
select *from student where <字段名称>is null;
--判断空
select *from student where 字段名称=";
-- 判断空字符串或者null
select *from student where 字段名称 is null or 字段名称=";
2016-11-23 21:00:47 isole于博客园
以上是关于Mysql基础的主要内容,如果未能解决你的问题,请参考以下文章
[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础
连接MySQL出现错误:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)(代码片段
使用 json rereiver php mysql 在片段中填充列表视图
关于mysql驱动版本报错解决,Cause: com.mysql.jdbc.exceptions.jdbc4Unknown system variable ‘query_cache_size(代码片段