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数据库软件的运行数据、日志信息、性能数据
+--------------------+
 rows in set (0.00 sec)

1.2:创建数据库,并命名为isole

mysql> create database isole;
Query OK,  row affected (0.00 sec)

1.3:查看数据库的默认字符集

mysql> show create database isole;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| isole    |  DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
 row in set (0.00 sec)

 由上可知,此数据库的编码为latin1,我们都知道通常我们使用utf-8编码

1.4:修改数据库的默认字符集并且查看,注意修改的时候utf-8写成utf8

mysql> alter database isole default character set utf8;
Query OK,  row affected (0.00 sec)

mysql> show create database isole;
+----------+----------------------------------------------------------------+
| Database | Create Database                                                |
+----------+----------------------------------------------------------------+
| isole    |  DEFAULT CHARACTER SET utf8 */ |
+----------+----------------------------------------------------------------+
 row in set (0.00 sec)

1.5:删除数据库

mysql> drop database isole;
Query OK,  rows affected (0.00 sec)

1.6:在创建数据库的时候指定编码的格式:

mysql> create database isole default character set utf8;
Query OK,  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)是字段长度
    -> sage int    -- sage是字段名称
    -> );
Query OK,  rows affected (0.18 sec)

mysql>

2.3查看当前数据库中所有的表

mysql> show tables;
+-----------------+
| Tables_in_isole |
+-----------------+
| student         |
+-----------------+
 row in set (0.00 sec)

mysql>

2.4查看某个表字段的结构,此处以student为例

mysql> desc student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid   | int()     | YES  |     | NULL    |       |
| sname | varchar() | YES  |     | NULL    |       |
| sage  | int()     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

2.5:student表增加字段sgender varchar(2)

mysql> alter table student add column sgender varchar();
Query OK,  rows affected (0.34 sec)
Records:   Duplicates:   Warnings: 

2.6:student表修改字段sgender改成gender

mysql> alter table student change sgender gender varchar();
Query OK,  rows affected (0.13 sec)
Records:   Duplicates:   Warnings: 

2.7student表中的sid int修改字段类型为sid varchar(5)

mysql> alter table student modify sid varchar();
Query OK,  rows affected (0.41 sec)

2.8student表删除字段gender

mysql> alter table student drop gender;
Query OK,  rows affected (0.35 sec)
Records:   Duplicates:   Warnings: 

2.9修改表的名称student为teacher

mysql> alter table student rename teacher;
Query OK,  rows affected (0.06 sec)

2.10删除表teacher

mysql> drop table teacher;
Query OK,  rows affected (0.10 sec)

三:数据

先创建学生表,字段以及类型如下

mysql> create table student(
    -> sid int,
    -> sname varchar(),
    -> sgender varchar(),
    -> sage int
    -> );
Query OK,  rows affected (0.18 sec)

3.1增加数据

  3.1.1增加张三、李四的信息,插入对应字段的所有信息,此时可以不写出字段

mysql> insert into student values(,);
Query OK,  row affected (0.06 sec)

mysql> insert into student values(,);
Query OK,  row affected (0.06 sec)

  3.1.2增加王二的信息,此时不知道王二的年龄,注意字段与值必须一一对应!

mysql> insert into student(sid,sname,sgender) values(,'王二','女');
Query OK,  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= where sid=;
Query OK,  row affected (0.06 sec)
Rows matched:   Changed:   Warnings:  

3.3删除数据

  3.3.1删除所有数据

mysql>delete from student;
Query OK,  row affected (0.06 sec)

  3.3.2带条件的删除 删除sid=3的学生信息

mysql> delete from student where sid=;
Query OK,  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 |
+------+-------+---------+------+
|     | 张三  | 男      |    |
|     | 李四  | 女      |    |
|     | 王二  | 女      | NULL |
+------+-------+---------+------+
 rows in set (0.00 sec)

  4.2查询某些列的数据,例如查询学生id和姓名

mysql> select sid,sname from student;
+------+-------+
| sid  | sname |
+------+-------+
|     | 张三  |
|     | 李四  |
|     | 王二  |
+------+-------+
 rows in set (0.00 sec)

  4.3查询时去除重复的字段,例如只查询有多少种性别

mysql> select distinct sgender from student;
+---------+
| sgender |
+---------+
| 男      |
| 女      |
+---------+
 rows in set (0.00 sec)

  4.4查询时指定别名  AS

mysql> select sid as '编号',sname as '姓名' from student;
+------+------+
| 编号 | 姓名 |
+------+------+
|     | 张三 |
|     | 李四 |
|     | 王二 |
+------+------+
 rows in set (0.00 sec)

  4.5条件查询,and(且)和or(或)

查询id为2且姓名为'李四'的学生信息

mysql>  and sname='李四';
+------+-------+---------+------+
| sid  | sname | sgender | sage |
+------+-------+---------+------+
|     | 李四  | 女      |    |
+------+-------+---------+------+
 row in set (0.00 sec)

查询id=3或者姓名为张三的学生信息

mysql>  or  sname='张三';
+------+-------+---------+------+
| sid  | sname | sgender | sage |
+------+-------+---------+------+
|     | 张三  | 男      |    |
|     | 李四  | 女      |    |
+------+-------+---------+------+
 rows in set (0.00 sec)

为王二同学录入年龄信息

mysql> update student  where sname='王二';
Query OK,  row affected (0.06 sec)
Rows matched:   Changed:   Warnings: 

查询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>  and sage<;
+------+-------+---------+------+
| sid  | sname | sgender | sage |
+------+-------+---------+------+
|     | 李四  | 女      |    |
+------+-------+---------+------+
 row in set (0.00 sec)

-- 方法2:

mysql>  and ;
+------+-------+---------+------+
| sid  | sname | sgender | sage |
+------+-------+---------+------+
|     | 李四  | 女      |    |
+------+-------+---------+------+
 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 |
+------+-------+---------+------+
|     | 李四  | 女      |    |
+------+-------+---------+------+
 row in set (0.00 sec)

增加字段math并且录入成绩

mysql> alter table student add math float;
Query OK,  rows affected (0.33 sec)
Records:   Duplicates:   Warnings: 

mysql> update student  ;
Query OK,  row affected (0.07 sec)
Rows matched:   Changed:   Warnings: 

mysql> update student  ;
Query OK,  row affected (0.08 sec)
Rows matched:   Changed:   Warnings: 

mysql> update student  ;
Query OK,  row affected (0.05 sec)
Rows matched:   Changed:   Warnings: 

聚合查询

常见的聚合函数:sum()  avg()  max()   min()  count()

mysql> select sum(math) as'数学总分' from student;
+----------+
| 数学总分 |
+----------+
|       |
+----------+
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 |
+------+-------+---------+------+------+
|     | 王二  | 女      |    |    |
|     | 张三  | 男      |    |    |
|     | 李四  | 女      |    |    |
+------+-------+---------+------+------+
数学成绩的正序
mysql> select *from student order by math asc;
+------+-------+---------+------+------+
| sid  | sname | sgender | sage | math |
+------+-------+---------+------+------+
|     | 李四  | 女      |    |    |
|     | 张三  | 男      |    |    |
|     | 王二  | 女      |    |    |
+------+-------+---------+------+------+
数学成绩倒序,年龄正序(当数学成绩一样的时候以年龄正序排序)
mysql> select *from student order by math desc,sage asc;
+------+-------+---------+------+------+
| sid  | sname | sgender | sage | math |
+------+-------+---------+------+------+
|     | 王二  | 女      |    |    |
|     | 张三  | 男      |    |    |
|     | 李四  | 女      |    |    |

分组查询 group by

mysql> select sgender,count(*)from student group by sgender;
+---------+----------+
| sgender | count(*) |
+---------+----------+
| 女      |         |
| 男      |         |
+---------+----------+
 rows in set (0.00 sec)

分页查询 limit

起始行从0开始 公式:select *from student limit(当前页-1)*每页显示多少条,每页显示多少条;

mysql> ,;
+------+-------+---------+------+------+
| sid  | sname | sgender | sage | math |
+------+-------+---------+------+------+
|     | 张三  | 男      |    |    |
|     | 李四  | 女      |    |    |
+------+-------+---------+------+------+

特别板块,判空条件(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于博客园

上一篇:ASP.NET MVC 如何解决“上下文的模型已在数据库创建后发生更改”问题


下一篇:纯CSS3绘制的黑色图标按钮组合