MySQL数据库应用(5)SQL语言(1)

一、SQL结构化查询语言

    1、什么是SQL?

        SQL,英文全称Structured Query Language,中文意思是结构化查询语言,它是一种对关系数据库中的数据进行定义和操作的语言方法。

    2、SQL的分类

        SQL结构化查询语言包含6个部分:

        1)数据查询语言(DQL):

        DQL全称Data Query Language,其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出。保留字SELECT是DQL(也是所有SQL)用得最多的动词,其他DQL常用的保留字有WHERE,ORDER BY,GROUP BY和HAVING。这些DQL保留字常与其他类型的SQL语句一起使用。

mysql> select user,host,password from mysql.user order by user asc;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
| root | localhost             | *8B27C4BCAD7F539091156319763740D798B44466 |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
| root | ::1                   |                                           |
+------+-----------------------+-------------------------------------------+
6 rows in set (0.11 sec)

mysql> select user,host,password from mysql.user order by user desc;
+------+-----------------------+-------------------------------------------+
| user | host                  | password                                  |
+------+-----------------------+-------------------------------------------+
| root | localhost             | *8B27C4BCAD7F539091156319763740D798B44466 |
| root | localhost.localdomain |                                           |
| root | 127.0.0.1             |                                           |
| root | ::1                   |                                           |
|      | localhost             |                                           |
|      | localhost.localdomain |                                           |
+------+-----------------------+-------------------------------------------+
6 rows in set (0.00 sec)

        2)数据操作语言(DML)

        DML全称Data Manipulation Language,其语句包括动词INSERT,UPDATE和DELETE。它们分别用于添加,修改和删除表中的行(数据)。也称为动作查询语言。具体语句例如:      delete from mysql.user where user=‘root’ and host=‘127.0.0.1’;

        3)事务处理语言(TPL)

        它的语言能确保被DML语句影响的表的所有行及时得以更新。TPL语句包括KEGIN TRANSACTION,COMMIT和ROLLBACK。

        4)数据控制语言(DCL)

        DCL全称(Data Control Language),它的语句通过GRANT或REVOKE获得许可,确定单个用户和用户组对数据库对象的访问。某些RDBMS可用GRANT或REVOKE控制对表单个列的访问。

        5)数据定义语言(DDL)

        全称,(Data Definition Language),其语句包括动词CREATE和DROP。在数据库中创建新表或删除表(CREAT TABLE 或 DROP TABLE);为表加入索引等。DDL包括许多与人数据库目录中获得数据有关的保留字。它也是动作查询的一部分。

        6)指针控制语言(CCL)

        全称CURSOR Control Language,它的语句,像DECLARE CUROR,FETCH INTO和UPDATE WHERE CURRENT用于对一个或多个表单独行的操作。

        小结:SQL语句最常见的分类一般是3类:

      DDL——数据定义语言(CREATE,ALTER,DROP)—————————运维

       DML——数据操纵语言(SELECT,INSERT,DELETE,UPDATE)———开发

       DCL——数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)——运维

 

二、MySQL数据库常见管理应用

    1、创建数据库

        命令语法:create database <数据库名>  <=注意库名不能数据开头

        在MySQL默认字符集情况下建立数据库测试如下:

        a.建立一个名为oldboy的数据库

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

mysql> show create database oldboy \G
*************************** 1. row ***************************
       Database: oldboy
Create Database: CREATE DATABASE `oldboy` /*!40100 DEFAULT CHARACTER SET latin1 */
1 row in set (0.00 sec)

 

        b.建立一个名为oldboy_的GBK字符集数据库

mysql> create database oldboy_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
Query OK, 1 row affected (0.11 sec)

mysql> show create database oldboy_gbk \G
*************************** 1. row ***************************
       Database: oldboy_gbk
Create Database: CREATE DATABASE `oldboy_gbk` /*!40100 DEFAULT CHARACTER SET gbk */
1 row in set (0.00 sec)

        c.建立一个名为oldboy_utf8的UTF8数据库

mysql> create database oldboy_utf8  CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
mysql> show create database oldboy_utf8 ;
+-------------+----------------------------------------------------------------------+
| Database    | Create Database                                                      |
+-------------+----------------------------------------------------------------------+
| oldboy_utf8 | CREATE DATABASE `oldboy_utf8` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+-------------+----------------------------------------------------------------------+
1 row in set (0.00 sec)

         d.创建不同字符集格式的数据库命令

 提示:如果编译的时候指定了特定的字符集,则以后创建对应的数据库就不需要指定字符集了。详细看安装实战。

 

    2、显示数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)
mysql> show databases like '%old%';
+------------------+
| Database (%old%) |
+------------------+
| oldboy           |
| oldboy_gbk       |
| oldboy_utf8      |
+------------------+
3 rows in set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| NULL       |
+------------+
1 row in set (0.00 sec)

mysql> use oldboy
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| oldboy     |
+------------+
1 row in set (0.00 sec)

 

    3、删除数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy             |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.00 sec)

mysql> drop database oldboy;
Query OK, 0 rows affected (0.36 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| oldboy_gbk         |
| oldboy_utf8        |
| performance_schema |
| test               |
+--------------------+
6 rows in set (0.00 sec)
#潜意识查看帮助
mysql> help drop database;
Name: 'DROP DATABASE'
Description:
Syntax:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name

DROP DATABASE drops all tables in the database and deletes the
database. Be very careful with this statement! To use DROP DATABASE,
you need the DROP privilege on the database. DROP SCHEMA is a synonym
for DROP DATABASE.

*Important*: When a database is dropped, user privileges on the
database are not automatically dropped. See [HELP GRANT].

IF EXISTS is used to prevent an error from occurring if the database
does not exist.

URL: http://dev.mysql.com/doc/refman/5.5/en/drop-database.html

    4、连接数据库

      命令:use  数据名

    5、查看当前所连接的数据库

      命令:select database();    #相当于Linux下的pwd

 

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.32    |
+-----------+
1 row in set (0.00 sec)

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2019-09-19 12:56:36 |
+---------------------+
1 row in set (0.00 sec)

     6、当前数据库包含的表信息

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)

mysql> show tables from oldboy_utf8;
Empty set (0.00 sec)

mysql> show tables in oldboy_gbk;
Empty set (0.00 sec)

 

posted @ 2019-09-23 20:52  黯然亦销魂丶  阅读(436)  评论(0编辑  收藏  举报