1、创建数据库
MariaDB [(none)]> CRRATE DATABASE test1; #创建一个名为test1的数据库
MariaDB [(none)]> CRRATE DATABASE IF NOT EXISTS test2; #创建一个名为test2的数据库
MariaDB [(none)]> SHOW DATABASES; #显示存在的数据库
2、删除数据库
MariaDB [(none)]> DROP DATABASE test1; #删除名为test1的数据库
MariaDB [(none)]> DROP DATABASE IF EXISTS test1;
3、创建表
格式:CREAT TABLE tb_name(col1,col2,col3,...);
MariaDB [(none)]> CREATE TABLE test2.teacher(Name CHAR(20) NOT NULL,Age TINYINT UNSIGNED,Gender CHAR(1) NOT NULL); #在数据库test2中创建表teacher
MariaDB [(none)]> USE test2;
MariaDB [(test2)]> CREATE TABLE student(Name CHAR(20) NOT NULL,Age TINYINT UNSIGNED,Gender CHAR(1) NOT NULL); #另一种方式在数据库test2中创建表student
MariaDB [(test2)]> SHOW TABLES FROM test2; #查看数据库test2中的表
4、查看表结构
MariaDB [(test2)]> DESCRIPTION student;
MariaDB [(test2)]> DESC teacher; #DESCRIPTION可缩写为DESC
5、删除表
MariaDB [(test2)]> DROP TABLE teacher;
MariaDB [(test2)]> DROP TABLE IF EXISTS teacher;
6、修改表
MariaDB [(test2)]> ALTER TABLE student ADD course VARCHAR(80); #添加一个字段course
MariaDB [(test2)]> DESC student;
MariaDB [(test2)]> ALTER TABLE student CHANGE course Course VARCHAR(80) AFTER Name; #修改course为Course,并放在Name后
MariaDB [(test2)]> DESC student;
7、插入数据信息
MariaDB [(test2)]> INSERT INTO student (Name,Gender) VALUE ('Tom','M'),('Jerry','F');
MariaDB [(test2)]> SELECT * FROM student;
MariaDB [(test2)]> INSERT INTO student VALUE ('Jack','Math',16,'M');
8、修改更新数据信息
MariaDB [(test2)]> UPDATE student SET Course='Physics' WHERE Name='Tom'; #向名为Tom的行的Course字段插入Physics
MariaDB [(test2)]> SELECT Name,Course FROM student WHERE Course='Physics'; #选择student表中课程为物理的Name与Course
MariaDB [(test2)]> DELETE FROM student WHERE Coures='Physics'; #删除课程为物理的所有行
9、创建、删除用户
CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
DROP USER 'username'@'host';
MariaDB [(none)]> CREATE USER 'Lucy'@'localhost' INDENTIFIED BY '123456';
MariaDB [(none)]> SHOW GRANTS FOR 'Lucy'@'localhost'; #查看用户的授权
MariaDB [(none)]> CREATE USER 'Lily'@'192.168.1.50'
MariaDB [(none)]> CREATE USER 'Bob'@'%' #其中%为通配符,表示任意多的字符,下划线_表示任意单个字符
10、授权
GRANT pri1,pri2,pri3,... ON dbname.tbname TO 'username'@'host' [IDENTIFIED BY 'password'];
REVOKE pri1,pri2,pri3,... ON dbname.tbname FROM 'username'@'host'; #取消权限
MariaDB [(none)]> GRANT ALL PRIVILEGES ON test2.* TO 'Lucy'@'%'; #授与用户'Lucy'@'%'在test2库所有表的全部的权限
MariaDB [(none)]> SHOW GRANTS FOR 'Lucy'@'%';