DML和常用SQL举例

2018-06-28  本文已影响25人  任总

1、服务器端命令:

2、数据库管理(DDL):

(1)、获取命令帮助:

(2)、创建库命令:

查看支持的所有字符集:SHOW CHARACTER SET
查看支持的所有排序规则:SHOW COLLATION

示例: 
MariaDB [(none)]> CREATE DATABASE testdb;
Query OK, 1 row affected (0.00 sec)

(3)、修改库命令:

(4)、删除库命令:

示例:

MariaDB [(none)]> DROP DATABASE testdb;#删除testdb库
MariaDB [shooldb]> DROP TABLE tl1; #删除tl1表


(5)、查看库命令:

示例:
MariaDB [(none)]> SHOW DATABASES LIKE 'testdb';
+-------------------+
| Database (testdb) |
+-------------------+
| testdb            |
+-------------------+
1 row in set (0.00 sec)

2、表管理:

(1)查看命令:

  • DESC tabla 显示表字段
示例:
MariaDB [testdb]> desc students;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(40) | NO   | PRI |         |       |
| id      | int(10)     | YES  |     | NULL    |       |
| address | varchar(40) | YES  |     | NULL    |       |
| class   | int(10)     | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| students         |
+------------------+
1 row in set (0.00 sec)

(2)创建表命令:

create_defination里面包括:

  • 字段:col_name data_type
示例:MariaDB [testdb]> CREATE TABLE students(
>name VARCHAR(40),#姓名字段类型为字符型 限40个字符
>id INT(10),   #id字段类型为整形 限定10个字符
>Address VARCHAR(40), #地址字段类型为字符型 限40个字符
>Class INT(10),      #班级字段类型为整形 限定10个字符
>primary key(name)); #name字段作为主键
Query OK, 0 rows affected (0.06 sec)

(3)修改表命令:

示例:
MariaDB [testdb]> ALTER TABLE students ADD INDEX(id,name);#给students的id和name字段添加索引
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [testdb]> ALTER TABLE students DROP class;#删除students表的class字段
Query OK, 0 rows affected (0.14 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [testdb]> ALTER TABLE students MODIFY id int(10) UNSIGNED NOT NULL;#修改students表中的id字段为无符号的非空整型字符
Query OK, 0 rows affected (0.14 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [testdb]> desc students;#查询students表
+---------+------------------+------+-----+---------+-------+
| Field   | Type             | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| name    | varchar(40)      | NO   | PRI |         |       |
| id      | int(10) unsigned | NO   | MUL | NULL    |       |
| address | varchar(40)      | YES  |     | NULL    |       |
+---------+------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

(4)索引管理:

索引是特殊的数据结构;

索引:要有索引名称;

3、数据的操纵(DML)

(1)插入命令INSERT INTO:

示例:
MariaDB [testdb]> INSERT INTO students(name,id,Address,Class)VALUES
>('zhao',001,'beijing',2017),
>('qian',002,'shanghai',2018),
>('sun',003,'chengdu',2017),
>('li',004,'tianjing',2018),
>('zhou',005,'chongqing',2017);

(2)、查询命令SELECT:

示例:
MariaDB [testdb]> SELECT * FROM students;#查询students表所有字段内容(生产环境不能这样操作)
+------+------+-----------+-------+
| name | id   | Address   | Class |
+------+------+-----------+-------+
| li   |    4 | tianjing  |  2018 |
| qian |    2 | shanghai  |  2018 |
| sun  |    3 | chengdu   |  2017 |
| zhao |    1 | beijing   |  2017 |
| zhou |    5 | chongqing |  2017 |
+------+------+-----------+-------+
示例:
MariaDB [testdb]> SELECT name,id FROM students;#查询id,name字段的列
+------+------+
| name | id   |
+------+------+
| li   |    4 |
| qian |    2 |
| sun  |    3 |
| zhao |    1 |
| zhou |    5 |
+------+------+
 示例:
 MariaDB [testdb]> SELECT name,id,Address FROM students WHERE id>3;查找ID大于3的行
+------+------+-----------+
| name | id   | Address   |
+------+------+-----------+
| li   |    4 | tianjing  |
| zhou |    5 | chongqing |
+------+------+-----------+
 示例:
 
MariaDB [testdb]> SELECT name,id,Address FROM students ORDER BY id ASC;#查找以ID字段升序排列表
+------+------+-----------+
| name | id   | Address   |
+------+------+-----------+
| zhao |    1 | beijing   |
| qian |    2 | shanghai  |
| sun  |    3 | chengdu   |
| li   |    4 | tianjing  |
| zhou |    5 | chongqing |
+------+------+-----------+

(3)删除命令DELETE:

示例:
    MariaDB [testdb]> DELETE FROM students WHERE name='li';#把name为li的行删除

(4)修改命令UPDATE:

示例:MariaDB [testdb]> UPDATE students SET name='zhang' WHERE id=1;   #把ID为1的name改为zhang  

3、数据库控制DCL

(1)用户授权命令GRANT

若授权的用户不存在,GRANT可自动生成指定的授权用户,其语法为:

示例:
GRANT ALL ON testdb.* TO ‘test’@’192.168.%.%’ INENTIFIED BY ‘123’;

(2)撤销授权REVOKE

示例:
REVOKE ALL ON testdb.*  FROM 'test'@'192.168.%.%';
REVOKE ALL ON test.* FROM 'test'@'192.168.%.%';
上一篇下一篇

猜你喜欢

热点阅读