DDL相关: #创建数据库 MariaDB [(none)]> CREATE DATABASE testdb; Query OK, 1 row affected (0.00 sec) #查看数据库 MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | | testdb | +--------------------+ 5 rows in set (0.02 sec) #使用testdb数据库, MariaDB [(none)]> use testdb; Database changed #创建表students,定义id为无符号非空int型,并且id是主键,name是VARCHAR非空型,最多容纳20字符,age是无符号tinyint型 MariaDB [testdb]> CREATE TABLE students (id int UNSIGNED NOT NULL PRIMARY KEY, name VARCHAR(20) NOT NULL, age tinyint UNSIGNED); Query OK, 0 rows affected (0.04 sec) #查看表详细信息 MariaDB [testdb]> DESC students; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 3 rows in set (0.01 sec) #定义联合主键---此次创建id和name组合起来为主键,这样只要求id+name和其他行不同即可 MariaDB [testdb]> CREATE TABLE tbl2 (id int UNSIGNED NOT NULL, name VARCHAR(20) NOT NULL, age tinyint UNSIGNED, PRIMARY KEY(id,name)); Query OK, 0 rows affected (0.00 sec) MariaDB [testdb]> DESC tbl2; +-------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | PRI | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +-------+---------------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) #查看正规创建命令:\G使用竖向格式 MariaDB [testdb]> SHOW CREATE TABLE students\G; *************************** 1. row *************************** Table: students Create Table: CREATE TABLE `students` ( `id` int(10) unsigned NOT NULL, `name` varchar(20) NOT NULL, `age` tinyint(3) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) #查看表状态: MariaDB [testdb]> SHOW TABLE STATUS LIKE ‘students‘\G; *************************** 1. row *************************** Name: students Engine: InnoDB Version: 10 Row_format: Compact Rows: 0 Avg_row_length: 0 Data_length: 16384 Max_data_length: 0 Index_length: 0 Data_free: 10485760 Auto_increment: NULL Create_time: 2015-10-27 21:57:09 Update_time: NULL Check_time: NULL Collation: latin1_swedish_ci Checksum: NULL Create_options: Comment: 1 row in set (0.00 sec) #删除表: DROP TABLE [IF EXISTS] ‘tbl_name‘; 修改表: #使用ADD在name段之后添加gender段,类型为枚举类型 MariaDB [testdb]> ALTER TABLE students ADD gender ENUM(‘m‘,‘f‘) AFTER name; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> DESC students; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | id | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | gender | enum(‘m‘,‘f‘) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) 使用CHANGE修改修改表,注意之前是主键修改时不需要指明主键,UNSIGNED用于修饰int类型,所以必须放在int后 MariaDB [testdb]> ALTER TABLE students CHANGE id sid int UNSIGNED NOT NULL PRIMARY KEY; ERROR 1068 (42000): Multiple primary key defined MariaDB [testdb]> ALTER TABLE students CHANGE id sid int UNSIGNED NOT NULL; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> DESC students; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | sid | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | gender | enum(‘m‘,‘f‘) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) #增加唯一键: MariaDB [testdb]> ALTER TABLE students ADD UNIQUE KEY(name); Query OK, 0 rows affected (0.22 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [testdb]> DESC students; +--------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+-------+ | sid | int(10) unsigned | NO | PRI | NULL | | | name | varchar(20) | NO | UNI | NULL | | | gender | enum(‘m‘,‘f‘) | YES | | NULL | | | age | tinyint(3) unsigned | YES | | NULL | | +--------+---------------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) #添加索引:如果创建索引时没给名字,则索引名为字段名。 MariaDB [testdb]> ALTER TABLE students ADD INDEX(age); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 #查看索引 MariaDB [testdb]> SHOW INDEXES FROM students\G; *************************** 1. row *************************** Table: students Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: sid Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: students Non_unique: 0 Key_name: name Seq_in_index: 1 Column_name: name Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: students Non_unique: 1 Key_name: age Seq_in_index: 1 Column_name: age Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec) #删除索引: DROP INDEX name ON students; 查看是否删除成功: MariaDB [testdb]> SHOW INDEX FROM students\G; *************************** 1. row *************************** Table: students Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: sid Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: 1 row in set (0.00 sec) #删除字段: MariaDB [testdb]> ALTER TABLE students DROP age;
DML相关:
#INSERT向表中插入字段: MariaDB [testdb]> INSERT INTO students VALUES (1,‘Yang Guo‘,‘m‘),(2,‘Guo Xiang‘,‘f‘); Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 查看: MariaDB [testdb]> SELECT * from students; +-----+-----------+--------+ | sid | name | gender | +-----+-----------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | +-----+-----------+--------+ 2 rows in set (0.00 sec) MariaDB [testdb]> INSERT INTO students (sid,name) VALUES (3,‘Zhang Wuji‘),(4,‘Zhao Min‘); Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 MariaDB [testdb]> SELECT * from students; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | | 3 | Zhang Wuji | NULL | | 4 | Zhao Min | NULL | +-----+------------+--------+ 4 rows in set (0.00 sec) #SELECT 查询数据: MariaDB [testdb]> SELECT * FROM students WHERE sid<3; +-----+-----------+--------+ | sid | name | gender | +-----+-----------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | +-----+-----------+--------+ 2 rows in set (0.00 sec) #查询到后降序排序 MariaDB [testdb]> SELECT * FROM students ORDER BY sid DESC; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 4 | Zhao Min | NULL | | 3 | Zhang Wuji | NULL | | 2 | Guo Xiang | f | | 1 | Yang Guo | m | +-----+------------+--------+ 4 rows in set (0.00 sec) #偏移一个,跳过两个显示 MariaDB [testdb]> SELECT * FROM students ORDER BY sid LIMIT 1,2; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 2 | Guo Xiang | f | | 3 | Zhang Wuji | NULL | +-----+------------+--------+ 2 rows in set (0.00 sec) #where子句: MariaDB [testdb]> SELECT * FROM students WHERE sid>=2 and sid<=4; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 2 | Guo Xiang | f | | 3 | Zhang Wuji | NULL | | 4 | Zhao Min | NULL | +-----+------------+--------+ 3 rows in set (0.00 sec) #同上,使用between and MariaDB [testdb]> SELECT * FROM students WHERE sid BETWEEN 2 AND 4; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 2 | Guo Xiang | f | | 3 | Zhang Wuji | NULL | | 4 | Zhao Min | NULL | +-----+------------+--------+ 3 rows in set (0.00 sec) #找出Z开头的 MariaDB [testdb]> SELECT * FROM students WHERE name LIKE ‘Z%‘ -> ; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 3 | Zhang Wuji | NULL | | 4 | Zhao Min | NULL | +-----+------------+--------+ 2 rows in set (0.00 sec) #RLIKE使用正则表达式 MariaDB [testdb]> SELECT * FROM students WHERE name RLIKE ‘.*u.*‘; +-----+------------+--------+ | sid | name | gender | +-----+------------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | | 3 | Zhang Wuji | NULL | +-----+------------+--------+ 3 rows in set (0.00 sec) MariaDB [testdb]> SELECT * FROM students WHERE name RLIKE ‘.*[A-G]u.*‘; +-----+-----------+--------+ | sid | name | gender | +-----+-----------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | +-----+-----------+--------+ 2 rows in set (0.00 sec) #字段别名:as MariaDB [testdb]> SELECT sid as stuid, name as stuname FROM students; +-------+------------+ | stuid | stuname | +-------+------------+ | 1 | Yang Guo | | 2 | Guo Xiang | | 3 | Zhang Wuji | | 4 | Zhao Min | +-------+------------+ 4 rows in set (0.00 sec) #DELETE删除数据:必须写条件,否则将删除整个表中数据 MariaDB [testdb]> DELETE FROM students WHERE sid=3; Query OK, 1 row affected (0.00 sec) MariaDB [testdb]> SELECT * FROM students; +-----+-----------+--------+ | sid | name | gender | +-----+-----------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | | 4 | Zhao Min | NULL | +-----+-----------+--------+ 3 rows in set (0.00 sec) #UPDATE 修改数据 MariaDB [testdb]> UPDATE students SET gender=‘f‘ WHERE sid=4; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 MariaDB [testdb]> SELECT * FROM students; +-----+-----------+--------+ | sid | name | gender | +-----+-----------+--------+ | 1 | Yang Guo | m | | 2 | Guo Xiang | f | | 4 | Zhao Min | f | +-----+-----------+--------+ 3 rows in set (0.00 sec)
DCL相关:
#用户账号权限管理: 用户账号:‘user‘@‘host‘ user: 用户名 host: 此用户访问mysqld服务时允许通过哪些主机远程创建连接; host可以使用IP、网络地址、主机名、通配符(%和_); 禁止检查主机名:定义my.cng文件跳过反解主机名做授权,就是127.0.0.1和localhost不区分 [mysqld] skip_name_resolve = yes #创建某用户可以通过任意主机联系mysql MariaDB [testdb]> CREATE USER ‘wpuser‘@‘%‘ IDENTIFIED BY ‘wpp@ss‘; Query OK, 0 rows affected (0.00 sec) MariaDB [testdb]> use mysql; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [mysql]> SELECT USER,hOST,pASSWORD FROM user; +--------+-----------+-------------------------------------------+ | USER | hOST | pASSWORD | +--------+-----------+-------------------------------------------+ | root | localhost | | | root | node1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | node1 | | | wpuser | % | *D763A435C015D6AA05A435779E5D9CB9B6266278 | +--------+-----------+-------------------------------------------+ 7 rows in set (0.00 sec) 查看授权表: 权限级别:管理权限、数据库、表、字段、存储例程 priv_type: ALL [PRIVILEGES] db_name.tb_name: *.*: 所有库的所表; db_name.*: 指定库的所有表; db_name.tb_name: 指定库的指定表; db_name.routine_name:指定库的存储例程; MariaDB [mysql]> SHOW GRANTS FOR ‘wpuser‘@‘%‘\G; *************************** 1. row *************************** Grants for wpuser@%: GRANT USAGE ON *.* TO ‘wpuser‘@‘%‘ IDENTIFIED BY PASSWORD ‘*D763A435C015D6AA05A435779E5D9CB9B6266278‘ 1 row in set (0.00 sec) #授权:WITH GRANT OPTION表示授予此用户权限后,他可以把此权限转授(复制)给其他人,一般很少用 GRANT ALL PRIVILEGES ON *.* TO test@localhost IDENTIFIED BY ‘test‘ WITH GRANT OPTION; 示例:授权testuser SELECT和DELETE权限 MariaDB [(none)]> use testdb; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [testdb]> SHOW GRANTS FOR CURRENT_USER; +---------------------------------------------------------------------------------------------------------+ | Grants for testuser@% | +---------------------------------------------------------------------------------------------------------+ | GRANT USAGE ON *.* TO ‘testuser‘@‘%‘ IDENTIFIED BY PASSWORD ‘*00E247AC5F9AF26AE0194B41E1E769DEE1429A29‘ | | GRANT SELECT, DELETE ON `testdb`.* TO ‘testuser‘@‘%‘ | +---------------------------------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) #删除用户: DROP USER ‘username‘@‘host‘; #撤销授权: REVOKE priv_type, ... ON db_name.tb_name FROM ‘user‘@‘host‘; 收回testuser的DELETE权限 MariaDB [mysql]> SHOW GRANTS FOR ‘testuser‘@‘%‘\G; *************************** 1. row *************************** Grants for testuser@%: GRANT USAGE ON *.* TO ‘testuser‘@‘%‘ IDENTIFIED BY PASSWORD ‘*00E247AC5F9AF26AE0194B41E1E769DEE1429A29‘ *************************** 2. row *************************** Grants for testuser@%: GRANT SELECT ON `testdb`.* TO ‘testuser‘@‘%‘ 2 rows in set (0.00 sec) ERROR: No query specified 注意:MariaDB服务进程启动时会读取mysql库中的所有授权表至内存中; (1) GRANT或REVOKE等执行权限操作会保存于表中,MariaDB的服务进程会自动重读授权表; (2) 对于不能够或不能及时重读授权表的命令,可手动让MariaDB的服务进程重读授权表: mysql> FLUSH PRIVILEGES;
本文出自 “明天过后” 博客,请务必保留此出处http://leeyan.blog.51cto.com/8379003/1707010
原文:http://leeyan.blog.51cto.com/8379003/1707010