一、SQL语言
1、DDL (Data Definition Language) 数据库定义语言
2、DML(Data Manipulation Language) 数据库操作语言
3、DQL (Data Query Language) 数据库查询语言
4、DCL(Data Control Language) 数据库控制语言
5、TCL(Transaction Control Language) 事务控制语言
二、示例
1、create创库创表,查询表结构(desc)
>create table department1(id int auto_increment primary key,name varchar(40) not null,dept_sfz int unique);
2、alter配合modify 和 change(modify只能修改属性,change用来修改列名)
> alter table department1 change name dept_name varchar(20) not null;
> alter table student10 modify id int not null;(改变id的自增) > alter table department1 modify dept_name varchar(10)
> alter table department1 add dept_sex varchar(10) default "m";
3、 Insert into 插数据
> insert into department1 (id,name,dept_sfz) values -> (2,'we',23), -> (4,'ty',56);
4、Drop 删列、删表 delete删除记录
> alter table department1 drop dept_sex;(删列) > drop table department1;(删表) > delete from user where Host='172.24.46.%';(删记录)
5、Rename用于重命名对象
> alter table users_user rename users_user01;
6、授权(授权的同时也新建了用户)
> grant all privileges on *.* to 'userdb'@'localhost' identified by '密码'; > grant select,insert,update,delete on jumpserver.* to zjz@'192.168.0.%'; > FLUSH PRIVILEGES
7、 Revoke 取消授权
> revoke all on *.* from zjz@localhost;
8、查看用户权限
> show grantsG;(查看当前用户自己的权限) > show grants for zjz@'localhost';(查看他人权限) mysql> use mysql Database changed mysql> show tables; +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | ......省略 | user | +---------------------------+ 31 rows in set (0.00 sec) mysql> desc user;(查看user表的表结构) +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | | User | char(32) | NO | PRI | | | .....省略 | authentication_string | text | YES | | NULL | | | password_expired | enum('N','Y') | NO | | N | | | password_last_changed | timestamp | YES | | NULL | | | password_lifetime | smallint(5) unsigned | YES | | NULL | | | account_locked | enum('N','Y') | NO | | N | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ 45 rows in set (0.00 sec) mysql> select Host,User,authentication_string from user;(只查询其中三项) +-----------+---------------+-------------------------------------------+ | Host | User | authentication_string | +-----------+---------------+-------------------------------------------+ | localhost | root | *F14917E81FAE96E012F66F66C219841437552773 | | localhost | mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | | localhost | mysql_monitor | *5C01CCC1CBAF86AAF36B9C35AF94E326204B13F2 | | % | grafana | *702F9ED0D2BE8929F53D852D6D6D03657760940A | | localhost | admin | *CC3C9B71332E123B33EF5EF3D816857B5A3EDCE0 | +-----------+---------------+-------------------------------------------+ 6 rows in set (0.01 sec)
9.查看当前的登录用户和数据库
mysql> select user();(等价于mysql> SELECT current_user;) mysql> select database(); +----------------+ +------------+ | user() | | database() | +----------------+ +------------+ | root@localhost | | mysql | +----------------+ +------------+ 1 row in set (0.01 sec) 1 row in set (0.00 sec)
10.数据的导入导出
导出数据库 mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql #/usr/local/mysql/bin/ mysqldump -uroot -p abc > abc.sql 敲回车后会提示输入密码 只导出表结构 mysqldump -u用户名 -p密码 -d 数据库名 > 数据库名.sql #/usr/local/mysql/bin/ mysqldump -uroot -p -d abc > abc.sql 导入数据库 首先建空数据库 mysql>create database abc; 导入数据库 方法一: (1)选择数据库 mysql>use abc; (2)设置数据库编码 mysql>set names utf8; (3)导入数据(注意sql文件的路径) mysql>source /home/abc/abc.sql; 方法二: mysql -u用户名 -p密码 数据库名 < 数据库名.sql #mysql -uabc_f -p abc < abc.sql https://www.cnblogs.com/lonmyblog/p/9235136.html
11、设置中文字符集
character-set-server = utf8 #定义服务端所使用的字符集为UTF8
init_connect = SET NAMES utf8 #定义client与server之间传递字符的编码规则为utf8
亲测有效(my.cnf里面改)
https://www.cnblogs.com/lvthinks/p/12502006.html (有关mysql5.7的配置文件解释)
12、远程登陆
# mysql -uroot -h 192.168.40.141 -p