DDL:数据定义语言;主要用于管理数据库组件,例如表,索引,视图,用户,存储过程;
CREATE,ALTER,DROP
DML:数据操纵语言,主要用管理表中的数据,实现数据的增删改查
INSERT;DELETE;UPDATE;SELECT
DCL:GRANT,REVOKE
获取命令帮助:
mysql>help keyword
比如:mysql>help CREATE
mysql>help CREATE DATABASE
数据库的创建:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
查看支持的所有字符集:show character set;
查看支持的所有排序规则:SHOW COLLATION;
数据库的创建也可以直接在数据的路径下之间创建个目录,不建议此举,因为创建的目录虽然可以当做数据库,不过无法设定字符集之类的操作
步骤:
mkdir /mydata/data/twodb
cd /mydata/data
chown -R mysql:mysql twodb
mysql
show databases
删除数据库:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
修改数据库:
ALTER {DATABASE | SCHEMA} [db_name]
[DEFAULT] CHARACTER SET [=] charset_name
[DEFAULT] COLLATE [=] collation_name
表的管理:
创建:(help create table)
CREATE TABLE [IF NOT EXISTS] tbl_name (create_defination) [table_options]
create_defination:一般以逗号隔开
字段:col_name字段名,data_type数据类型
键:
primary key(col1,col2..)把哪些字段定义为主键
unique key(col1,col2...)把哪些字段定义为唯一键
foreign key(column):定义外键
索引:
KEY|INDEX [index_name] (col1,col2...)
表创建示例:
create table students(id int unsigned not null , name char(30) not null, age tinyint unsigned, gender enum('f','m'),primary key(id,name));
create table students(id int unsigned not null primary key, name char(30) not null, age tinyint unsigned, gender enum('f','m'));
create table grade_talbe(id int null,grade int not null, subject char(30),FOREIGN KEY(id) REFERENCES student_info(id) on delete cascade on update cascade); 创建外键,其中tudent_info(id)为外表对应的字段id
int unsigned:指无符号整型,即没有负数,两者位置得放在一起
进入数据库:use onedb;
查看表:desc students;
删除表drop table students;
table_options:
ENGINE[=]engine_name
查看数据库支持的所有存储引擎类型:
mysql>show engines;
查看某表的存储引擎类型:
mysql>show tables status [like "tbl_name"]
修改表:(help alter table)
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...]
alter_specification:
字段:
添加:ADD [COLUMN] col_name data_type [FIRST | AFTER col_name ]
比如:alter table students add class varchar(100) not null;
alter table students add class varchar(100) not null after name;这个指放到字段name的后面
删除:
比如:alter table students drop class;
修改:
CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]修改字段名
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]修改数据类型
比如:alter table students modify class varchar(200) after age;
键:
添加:ADD {primary|unique|foreign} key (col1,col2...)
删除:
主键:DROP PRIMARY KEY
外键:DROP FOREIGN KEY fk_symbol
例如:alter table students drop primary key;
alter table students add unique key (name);
索引:
添加:ADD {INDEX|KEY} [index_name] (col1,col2..)
删除:DROP {INDEX|KEY} [index_name]
例如:alter table students drop index name;
alter table students add index class(class); 第一个class表示索引的名字,括号里面的是字段
查看索引名字:
show indexes from students;
删除表:
drop table [if exists] tbl_name [,tbl_name2]...
索引管理:
创建:CREATE [ONLINE|OFFLINE] [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name [BTREE|HASH] ON tbl_name (col1,col2...)
比如:create index name_and_class on students (name,class);
删除:drop index index_name on tbl_name
比如:drop index name_and_class on students;
DDL:insert;delete;update;select
insert into:
insert [into] tbl_name [(col1..)]{values|value} (val1..),(val2..)
比如:insert into students values (1,'huang er lin','c15f36',23,'m','c15f3629');
insert into students (id,name) values (2,'huang er cun'),(3,'huang jia jin');允许空值的字段可以不用输入,这语句时在id,name字段输入值
注意:字符型要用引号;数值型不能用引号;
select:
(1)select * from tbl_name:查看表的所有数据
(2)select col1,col2,... from tbl_name:查看表的指定字段
显示时,字段可以用别名显示:select col_name as col_alias from tbl_name
示例:select id,name from students;
select id as ID,name from students;
(3)select col1,... from tbl_name where clause;
where clause:用来指明挑选条件
挑选条件的操作符:>,<,==,<=,>=,!=
组合条件:and;or;not
操作符2:betweend..and..
like 'pattern'
通配符:
%:任意长度的任意字符
_:任意单个字符
like 'pattern'支持正则表达式
is null:为空
is not null:为非空
比如: select * from students where id=1;
select * from students where age >=20 and age<= 80;这句也等于:select * from students where age between 30 and 80;
select * from students where name=="huang er lin";
select name from students where name like "%cun%";
select * from students where age is null;挑选年龄为空的字段
select * from students where age is not null;挑选字符非空的字段
(4)select col1.. from tbl_name [where clause] order by col_name1,col_name2... [asc|desc];
asc:升序
desc:降序
比如:select id,name from students order by name;
delete:
DELETE FROM tbl_name [WHERE where_condition] [ORDER BY ...] [LIMIT row_count
DELETE FROM tbl_name [WHERE where_condition]
DELETE FROM tbl_name [ORDER BY ...] [LIMIT row_count]
比如:delete from students where age is null;
delete from students order by age desc limit 20 这表示删除年龄最高的20位
注意注意:如果没有指定挑选条件的话,即where,那就是删除整个表的数据,这非常危险
update:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...[WHERE where_condition] [ORDER BY ...] [LIMIT row_count]
比如:update students set age=age-15 where name not like 'stu%';
一次性创建50条数据并插入students表中并且返回命令:]#:for i in {1..50}; do AGE=$[$RANDOM%100]; mysql -e "insert into onedb.students (id,name,age) values ($i,"stu$i",$AGE);"; done
用户账号权限管理:
用户账号:
'username'@'host'
host:此用户访问当前mysql服务器时,允许其通过哪些主机远程创建连接;
表示方式:ip,网络地址,主机名,通配符(%和_)
创建用户账号:
create user 'username'@'host' [identified by 'passwd'];
flush privileges;刷新权限
删除用户账号:
drop user 'user'@'host'[,user@host]...
授权:
授权级别:管理权限,数据库,表,字段,存储例程
grant priv_type...on [object_type] db_name.tbl_name to 'user'@'host' [identified by 'passwd'];
priv_type:ALL=[privileges]
也可以是DDL,DML的语句
db_name.tbl_name:指定库的特定表
*.*:所有库的所有表;
db_name.*:指定库的所有表
db_name.routine_name:指定库上的存储过程或存储函数;
[object_type]:
table
function
procedure
例如:grant select on onedb.students to 'hel666'@'192.168.%.%' identified by '159357';
查看用户拥有那些权限:show grants for 'hel666'@'192.168.%.%';
查看当前用户拥有哪些权限:show grants for current_user;
回收权限:
revoke priv_type..on db_name.tbl_name from 'user'@'host'
注意:MariaDB服务进程启动时,会读取mysq库的所有授权表至内存中;
(1)GRANT或REVOKE命令等执行的权限操作会保存至表中,MariaDB此时一般会自动重读授权表,权限修改会立即生效
(2)其他方式实现的权限修改,要向立即生效,必须手动使用flush privileges命令;