MySQL - 【玩转MySql】数据库语言

▷ 目录

MySQL - 【玩转MySql】数据库语言_第1张图片

  • DDL 数据定义语言
    • create
      • 数据库的创建:当数据库已经存在时是否会覆盖?
      • 上述创建数据库时重名会报错,如何正常返回不执行不报错?
      • 创建数据表
        • 命令行创建
        • 执行 sql 脚本创建( source filepath
    • drop
      • 删除数据库
    • alter
      • 修改字符集(数据库+表)
      • 操作列
        • ADD(name property)
        • DROP(name)
        • MODIFY
        • CHANGE(old_name new_name new_property)
    • rename
      • 重命名数据库(不推荐)
        • 数据库备份与恢复
      • 重命名表
  • DML 数据操控语言
    • 引入
    • DISTINCT
      • IFNULL(pa,pb)
      • AS
    • WHERE
      • LIKE
    • GROUP BY
      • 聚合函数
        • SUM => 0
        • AVG  => 0
        • MAX
        • MIN
        • COUNT
    • HAVING
    • ORDER BY
      • 多规则
    • LIMIT
    • SQL语句书写顺序与执行顺序
  • DQL 数据查询语言


<一> DDL(Data Defination Language)

  • 选中待使用数据库:use database_X;
  • 查看正在使用的是哪个数据库:select database();

MySQL - 【玩转MySql】数据库语言_第2张图片

  • 数据库的创建:当数据库已经存在时是否会覆盖?NO!
mysql> create database self_use;
Query OK, 1 row affected (0.00 sec)

mysql> create database self_use;
ERROR 1007 (HY000): Can't create database 'self_use'; database exists
mysql>
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| self_use           |
| sys                |
| world              |
+--------------------+
7 rows in set (0.00 sec)



mysql> create database testdb1 charset utf8;
Query OK, 1 row affected (0.02 sec)

mysql> show create database;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql> show create database testdb;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| testdb   | CREATE DATABASE `testdb` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create database testdb1;
+----------+------------------------------------------------------------------+
| Database | Create Database                                                  |
+----------+------------------------------------------------------------------+
| testdb1  | CREATE DATABASE `testdb1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
  •  上述创建数据库时重名会报错,如何正常返回不执行不报错?
mysql> warnings;
Show warnings enabled.
mysql> create database if not exists self_use;
Query OK, 1 row affected, 1 warning (0.00 sec)

Note (Code 1007): Can't create database 'self_use'; database exists
mysql>
  •   创建数据表
    • 命令行创建
      • mysql> create table test(
            -> name varchar(20) primary key,
            -> age int
            -> )
            -> charset utf8
            -> engine innodb;
    • 执行 sql脚本 创建
      • MySQL - 【玩转MySql】数据库语言_第3张图片

  •  删除数据库(drop直接清空!,删除数据表同理 )
mysql> drop database self_use;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 rows in set (0.00 sec)

mysql>
mysql> select database();
+------------+
| database() |
+------------+
| self_use   |
+------------+
1 row in set (0.00 sec)

mysql> drop database self_use;
Query OK, 1 row affected (0.01 sec)

mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)
  • alter 修改数据库字符集
  1. 如何在创建数据库时指定字符集? CHARSET utf8
  2. MySQL创建数据库缺省字符集是什么? utf8
  3. 如何查看已创建的数据库信息? SHOW CREATE DATABASE db1;
  4. 如何修改数据库的字符集? ALTER DATABASE db1 CHARSET utf8; 
修改字符集(数据库+表):
	ALTER DATABASE db1 CHARSET utf8;
	ALTER TABLE table1 CHARSET utf8;

操作列:
	☆ 增列:ALTER TABLE table1 ADD info varchar(100);
	☆ 删列:ALTER TABLE table1 DROP info;
	    只改列属性:ALTER TABLE table1 MODIFY info varchar(50);
        ☆ 改列名和属性:ALTER TABLE table1 CHANGE old new 新属性;
            old可以喝new一致,此时仅修改列属性

改名(数据库+表):
	RENAME DATABASE old TO new;(这个语法在mysql 5.1.7中被添加进来,后来又被取消了)
            【必须要重命名数据库时,可以备份原数据库,导入新数据库】
	RENAME TABLE old TO new;
  • 数据库备份
mysqldump -uroot -hlocalhost -p123456 mydb1 > D:/mydb1_backup.sql

mysqldump -uroot -hlocalhost -p mydb1 > D:/mydb1_backup.sql
  • 数据库恢复
mysql -uroot -hlocalhost -p123456 testdb < d:/mydb1_backup.sql

mysql -uroot -hlocakhost -p testdb < d:/mydb1_backup.sql


<二> DML(Data manipulation Language)

1> 引入

注意:delete from table1 与 truncate table table1 的区别?

  1.  前者是逐行删除,后者是删除整个表后新创建一个相同空表
  2.  前者数据可以找回,后者不可找回

MySQL - 【玩转MySql】数据库语言_第4张图片

数据查询:

//查询所有数据
select * from city;

//查询数据条数
select count(*) from city;

//查看前10条数据
select * from city limit 10;

//查看后10条数据
select * from city order by id desc limit 10;

多条数据添加:

INSERT INTO student VALUES (123,'Alex','male'),(456,'Frank','female');

表的复制:

create table Table_name_A as select * from Table_name_B;

2> DISTINCT + ifnull

DISTINCT去重查询:
    distinct:不同的、明显的adj
        select DISTINCT name from table1;

滤空函数:IFNULL(参数一,参数二)
    为什么出现:NULL值与任何数据相加结果为NULL
    使用方法:
        select column1+IFNULL(column2,0) as total from table1;

mysql> select name,ifnull(age,0) age from test;
+------+-----+
| name | age |
+------+-----+
| A    |   1 |
| B    |   0 |
| c    |   0 |
+------+-----+
3 rows in set (0.00 sec)

mysql> select * from test;
+------+------+
| name | age  |
+------+------+
| A    |    1 |
| B    | NULL |
| c    | NULL |
+------+------+
3 rows in set (0.00 sec)


起别名:AS 
    AS可以省略

3>  WHERE

注意:BETWEEN...AND...为闭区间

WHERE判断条件:
	> >= < <= = != <>
	AND OR NOT
	BETWEEN...AND...
	IN(...,...,...)
	IS [NOT] NULL
            select * from table1 where resume IS NOT NULL;

WHERE模糊查询:
        LIKE
            _:任意一个字符
            %:任意0~n个字符
                select * from table1 where name LIKE "张%";

4> GROUP BY

mysql> select * from test;
+------+------+
| name | age  |
+------+------+
| A    |    1 |
| B    | NULL |
| c    | NULL |
+------+------+
3 rows in set (0.00 sec)

mysql> select name,age,count(*) from test group by age;
+------+------+----------+
| name | age  | count(*) |
+------+------+----------+
| B    | NULL |        2 |
| A    |    1 |        1 |
+------+------+----------+
2 rows in set (0.01 sec)

5> HAVING

注意:

  • 分组后筛选使用HAVING,分组前使用WHERE,二者所操作的表不同!
  • HAVING后可以使用聚合函数,WHERE后不可以!
mysql> select * from test;
+------+------+
| name | age  |
+------+------+
| A    |    1 |
| B    | NULL |
| c    | NULL |
+------+------+
3 rows in set (0.00 sec)

mysql> select name,age,count(*) from test group by age;
+------+------+----------+
| name | age  | count(*) |
+------+------+----------+
| B    | NULL |        2 |
| A    |    1 |        1 |
+------+------+----------+
2 rows in set (0.01 sec)

mysql> select name,age,count(*) count from test group by age having count=2;
+------+------+-------+
| name | age  | count |
+------+------+-------+
| B    | NULL |     2 |
+------+------+-------+
1 row in set (0.00 sec)

mysql>

7> ORDER BY

排序:OEDER BY
    select * from table1 ORDER BY id DESC,name ASC;
        首先按照规则一排序,相同时候则比较第二个规则

8> LIMIT

限制行数:LIMIT index,length
    从index开始查length行

 9> SQL语句书写顺序与执行顺序

-- 书写顺序
select -> from -> where -> group by -> having -> order by -> limit

-- 执行顺序
from -> where -> group by -> having -> select -> order by -> limit


<三> DQL(Data Query Language)

  MySQL - 数据完整性与多表查询



附加(MySQL查询结果两种查看方式,CMD下):

;和\g 效果一致;

\G 使用非表格样式;

 MySQL - 【玩转MySql】数据库语言_第5张图片

 

 

 

你可能感兴趣的:(MySQL,mysql,DDL,DML,DCL)