标签:sql语句 客户端 数据库 mysql mariadb
MariaDB的交互命令接口:mysql>
可运行的命令有两类:
客户端命令:在mysql客户端本地直接运行的命令
通过help获取完整列表:
mysql> help
服务器端命令:由客户端将命令通过协议封闭成报文发往mysqld,由mysqld运行,并取回运行结果至客户端;
即SQL语句,这些语句必须有语句结束符,默认为分号;
客户端命令:
quit, \q:退出客户端
help, \h:获取帮助信息
clear, \c: 中止正在编写的语句;类似bash中的Ctrl+c的功能;
go, \g:语句结束符
ego, \G: 语句结束符,但竖排显示每行数据;
delimter, \d: 定义语句结束符;
use, \u: 设定要操作默认数据库
sql语句:
获取帮助:help KEYWORD
DDL(data definitionlanguage):
主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,大多在建立表时使用。
DML(data manipulation language):
主要命令SELECT、UPDATE、INSERT、DELETE,命令是用来对数据库里的数据进行操作的语言。
DCL(Data Control Language):
数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。
数据库管理操作:
创建数据库:CREATE DATABASE
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS]db_name [create_specification] ... create_specification: [DEFAULT] CHARACTER SET [=] charset_name |[DEFAULT] COLLATE [=] collation_name MariaDB [(none)]> CREATE DATABASEtest_MariaDB ;
查看已有数据库:SHOW DATABASES
查看某库创建时使用的语句:
SHOWCREATE DATABASE db_name;
MariaDB [(none)]> SHOW CREATE DATABASEtest_MariaDB; +--------------+-------------------------------------------------------------------------+ | Database | Create Database | +--------------+-------------------------------------------------------------------------+ | test_MariaDB | CREATE DATABASE`test_MariaDB` /*!40100 DEFAULT CHARACTER SET latin1 */ | +--------------+-------------------------------------------------------------------------+ 1 row in set (0.00 sec)
查看MySQL支持的所有字符集:
SHOW CHARACTER SET;
MariaDB [test_MariaDB]> SHOW CHARACTER SET; +----------+-----------------------------+---------------------+--------+ | Charset | Description | Default collation | Maxlen | +----------+-----------------------------+---------------------+--------+ | big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 | ....... | swe7 | 7bit Swedish | swe7_swedish_ci | 1 | | ascii | US ASCII | ascii_general_ci | 1 | | ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
查看MySQL支持的所有Collate:
SHOW COLLATION;
MariaDB [(none)]> SHOW COLLATION; +--------------------------+----------+-----+---------+----------+---------+ | Collation | Charset | Id | Default | Compiled | Sortlen | +--------------------------+----------+-----+---------+----------+---------+ | big5_chinese_ci | big5 | 1 | Yes | Yes | 1 | | big5_bin | big5 | 84 | | Yes | 1 | | dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 | | dec8_bin | dec8 | 69 | | Yes | 1 | ......
删除数据库:DROP DATABASE
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
修改数据库:
ALTER {DATABASE | SCHEMA} [db_name] alter_specification ... ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORYNAME alter_specification: [DEFAULT] CHARACTER SET[=] charset_name | [DEFAULT] COLLATE [=]collation_name
表的管理操作:
创建表:CREATE TABLE 创建表时,可以同时创建索引
CREATE TABLE [IF NOT EXISTS] tbl_name (col1 type1 dec1, col2 type2dec2, ...)
通过表选项:
设置主键PRIMARY KEY,唯一键UNIQUE KEY,外键FOREIGN KEY
选择使用的存储引擎:ENGINE = engine_name
例如:学号、姓名、年龄、性别
学号:StuID, INT
姓名:Name, 定长字符,30
年龄:Age, TINYINT
性别:Gender, 枚举,ENUM(‘M‘,‘F‘)
示例:
MariaDB [test_MariaDB]> CREATE TABLE IF NOT EXISTS students(StuID INT UNSIGNED NOT NULL PRIMARY KEY, Name CHAR(30) NOT NULL, Age TINYINTUNSIGNED, Gender ENUM(‘M‘,‘F‘)); Query OK, 0 rows affected (0.76 sec)
注意:MySQL支持插件存储引擎
查看支持的所有存储引擎
SHOW ENGINES;
存储引擎:表类型
创建每个表时,都可以单独指明其表类型;但,建议同一个库内的所有表,应该使用同一种类型;
SHOW TABLE STATUS [{FROM | IN} db_name] [LIKE‘pattern‘ | WHERE expr] LIKE用于作模糊匹配,支持通配符: _: 匹配任意单个字符; %: 匹配任意长度的任意字符;
索引管理:
创建索引:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name ON tbl_name(index_col_name,...)
删除索引:
DROP INDEX index_name ON tbl_name
查看表结构:
DESC tbl_name
MariaDB [test_MariaDB]> desc students; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra| +--------+---------------------+------+-----+---------+-------+ | StuID | int(10)unsigned | NO | PRI | NULL | | | Name | char(30) | NO | | NULL | | | Age | tinyint(3) unsigned| YES | | NULL | | | Gender | enum(‘M‘,‘F‘) | YES | | NULL | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
查看数据库的表:
SHOW [FULL] TABLES [{FROM |IN} db_name] [LIKE ‘pattern‘ | WHERE expr]
删除表:DROP TABLE
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
DML(data manipulation language):
操作表中的数据:
插入数据:
INSERT [INTO] tbl_name[(col_name,...)] {VALUES | VALUE} ({expr |DEFAULT},...),(...),... MariaDB [test_MariaDB]> INSERT INTO studentsVALUES(01,‘Roger‘,18,‘M‘); Query OK, 1 row affected (0.07 sec)
删除数据:
DELETE FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] WHERE子句:过滤条件 布尔型表达式: MariaDB [test_MariaDB]> delete from students where stuid=03; Query OK, 1 row affected (0.05 sec)
SELECT单表查询:
SELECT [DISTINCT] [SQL_CACHE | SQL_NO_CACHE] col_name AS Alias, col_name AS Alias, ... [FROM table_references [WHERE where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] WHERE后的条件表达式: >, <, >=, <=, ==, != BETWEEN ... AND ... LIKE ‘pattern‘ _ :匹配任意单个字符 % :匹配任意个数字符 RLIKE ‘pattern‘ 使用正则表达式做匹配; 组合条件:或 与非 OR AND NOT MariaDB[test_MariaDB]> select * from students; +-------+-------+------+--------+ | StuID | Name | Age | Gender | +-------+-------+------+--------+ | 1 | Roger | 18 | M | | 2 | Roger | 30 | M | | 3 | Roger | 70 | M | | 4 | Roger | 70 | M | +-------+-------+------+--------+ 4 rows in set (0.00 sec)
更新数据 UPDATE:
UPDATE table_reference SET col_name1={expr1|DEFAULT}[, col_name2={expr2|DEFAULT}] ... [WHERE where_condition] [ORDER BY ...] [LIMIT row_count] MariaDB [test_MariaDB]> UPDATE students SET name=‘Wang‘ WHERE stuid=3; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0
用户及权限管理:
用户账号:
用户账号有两部分构成 :‘username‘@‘host‘
host可使用IP、网络地址、使用通配符(_和%);
创建用户账号,并赋予密码;
CREATE USER ‘username‘@‘host‘ IDENTIFIED BY [PASSWORD] ‘password‘;
MariaDB [(none)]> create user ‘wang‘@‘%‘ identified by ‘123456‘; Query OK, 0 rows affected (0.08 sec)
删除用户:
DROP USER ‘username‘@‘host‘;
MariaDB [(none)]> DROP USER ‘test‘@‘%‘; Query OK, 0 rows affected (0.00 sec)
授权:
GRANT priv1,... ON db_name.tbl_name TO ‘username‘@‘host‘ [IDENTIFIED BY [PASSWORD] ‘password‘];
如果用户事先不存在,则创建此账号并授权;
ALL [PRIVILEGES]:表示所有权限,参照下图
示例
MariaDB [(none)]> GRANT ALL ON test_MariaDB.students TO ‘wang‘@‘%‘; Query OK, 0 rows affected (0.03 sec)
查看用户已经获得的授权:
SHOW GRANTS [FOR user]
收回授权:REVOKE
REVOKE priv1,... ON db_name.tbl_name FROM ‘username‘@‘host‘;
若有错误,敬请指正!
谢谢!
关于MariaDB的学习,将会持续更新。。。。
标签:sql语句 客户端 数据库 mysql mariadb
原文地址:http://rogerwang.blog.51cto.com/5326353/1706150