首页 > 代码库 > mysql常用操作与练习题
mysql常用操作与练习题
创建数据库:
create database kaliboy_gbk DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
create database kaliboy_utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
显示数据库:
show databases;
show databases like ‘%kaliboy%‘;
连接显示数据库:
use kaliboy;
查看连接显示数据库:
select database();
删除数据库:
drop database kaliboy;
select version();
select user();
select now();
删除mysql 系统多余账号:
语法:drop user "user"@"主机域"
drop user "kali"@"localhost"
delete from mysql.user where="root" and host="localhost";
通过grant命令创建用户并授权
grant命令简单语法如下:
grant all privileges on dname.* to username@localhost identified by ‘passwd‘;
授权命令 对应权限 目标:库和表 用户名和客户端主机 用户密码
实例:
grant all privileges on test.* to kali@localhost identified by ‘kali123‘;
flush privileges;
查看权限:
show grants for kali@localhost;
create 和grant 配合法
首先创建用户username及密码passwd,授权主机localhost
create USER ‘wiki‘@‘localhost‘ IDENTIFIED by ‘passwd‘;
然后授权localhost主机上通过用户username管理dbname数据库的所有权限无需密码
grant all privileges ON dbname.* to ‘username‘@‘localhost‘;
收回权限:
revoke insert on test.* from ‘kaliboy‘@‘localhost‘;
mysql 交互式参数-e
创建数据库表:
create table subject_comment_manager(
subject_comment_manager_id bigint(12) NOT NULL auto_increment COMMENT ‘主键‘,
subject_type tinyint(2) NOT NULL COMMENT ‘素材类型‘,
subject_primary_key varchar(255) NOT NULL COMMENT ‘素材的主题‘,
subject_title varchar(255) NOT NULL COMMENT ‘素材的名称‘,
edit_user_nick varchar(64) default NULL COMMENT ‘修改人‘,
edit_user_time timestamp NULL DEFAULT NULL COMMENT ‘修改时间‘,
edit_comment varchar(255) default NULL COMMENT ‘修改理由‘,
state tinyint(1) NOT NULL default ‘1‘ COMMENT ‘0 代表关闭,1代表正常‘,
PRIMARY KEY (‘subject_comment_manager_id‘), 主键
key ‘IDX_PRIMARYKEY‘(‘subject_primary_key‘(32)), 索引
KEY ‘IDX_SUBJECT_TITLE‘(‘subject_title‘(32)) 索引
)ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
数据库索引:
create table student(
id int(4) not null AUTO_INCREMENT,
name char(20) not null,
age tinyint(2) not null default ‘0‘,
dept varchar(16) default null,
priary key(id), 主键
key index_name(name) 索引
);
删除主键:
alter table student drop primary key;
添加主键:
alter table student change id id int primary key auto_increment;
删除普通索引
alter table student drop index index_name
添加普通索引
alter table student add index index_name(name);
指定前n个字符创建索引
create index index_dept on student(dept(8));
查看索引:
show index from student\G
创建联合索引:
create index ind_name_dept on student(name,dept);
创建唯一索引(非主键)
create unique index index_name
往表中插入数据:
create table test(
id int(4) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL,
PRIMARY KEY(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
inert into test(id,name) values(1,‘wiki‘);
inert into test values(2,‘teachar‘),(3,‘student‘);
查询数据:
使用explain查询select查询语句执行计划
修改表中数据:
update test set name="高圆圆" where id="3";
flush-log 二进制bin日志切割
防止数据库误操作:
登录加-U 参数
练习题:
多实例登录 -S 指定 mysql.sock路径
mysql -u root -p -S /tmp/mysql.sock
查看数据库版本及登录用户是什么.
select version();
select user();
创建GBK字符集的数据库kaliboy,并查看已建库的完整语句
学会用 help查看 help create database, show character set
create database kaliboy CHARACTER SET GBK COLLATE gbk_chinese_ci;
show create database kaliboy;
创建用户kaliboy,使之可以管理数据库kaliboy
grant all on kaliboy.* to kaliboy@"localhost" Identified by "kali123";
flush privileges;
查看用户的授权
show grants for kaliboy@localhost;
查看当前数据库里有哪些用户
select host,user from mysql.user;
进去kaliboy 数据库
use kaliboy;
创建innodb引擎字符集为GBK表test,字段为ID和name varchar(16),查看建表结构及SQL语句
create table test(
-> id int(4),
-> name varchar(16))
-> ENGINE=innodb default charset=gbk;
create table position(id int(4), name varchar(10), limit varchar(20)) ENGINE=innodb default charset=gbk;
show create table test\G;
插入一条数据 1 kaliboy
insert into test values(1,‘kaliboy‘);
批量插入数据2,花花公子.3,etiantian
insert into test values(2,‘脚本小子‘),(3,‘tiantian‘),(4,‘xiaoqian‘),(5,"安全工程师");
查询插入的所有记录,查询名字为kaliboy的记录 查询ID 大于1 的记录
select * from test;
select * from test where name="kaliboy";
select * from test where id>1;
把数据id 等于1 的名字kaliboy更改为 kaligirl;
update test set name="kaligirl" where id=1;
在字段name前插入age字段,类型int(4)
alter table test add age int(4) after id;
备份kaliboy库 与mysql库
mysqldump -u root -p123456 --events -B kaliboy mysql>kali.sql
删除表中的所有数据,并查看
use kaliboy;
delete from mysql;
select * from mysql;
truncate table mysql;
删除表test和kaliboy 数据库并查看
drop database test;
Linux命令行恢复以上删除的数据
mysql -u root -p123456<kali.sql
把GBK字符集修改为UTF8
修改数据库的编码格式: alter database user character set gbk;
echo $LANG
mysql密码丢了,如何找回来实战
/etc/init.d/mysqld stop
mysqld_safe --defaults-file=/etc/my.cnf --skip-grant-table &
update mysql.user set password=password("123456") where user="root";
mysql内中文数据乱码的原理及如何防止乱码
在把ID 列设置为主键,在name字段上创建普通索引
alter table tast add primary key(id);
在手机字段上对前8个字符创建普通索引
alter table test add index show(shou(8));
查看创建的索引及索引类型信息
show index from test\G;
删除name,shouji列的索引
alter table test drop index name;
对name 列的前6个字符以及手机列的前8个字符组建联合索引
alter table test add index lianhe(name(6),shou(8));
查询手机号以135开头的,名字为kaliboy的记录
select * from test where name="kaliboy" and shou like "135%";
查询上述语句的执行计划(是否使用联合索引等)
explain select * from test where name="kaliboy" shou like "135%";
创建主键索引
alter table test change id id int primary key auto_increment;
删除索引:
alter table test drop index index_name;
添加索引:
alter table test add index index_name(name);
对前几位字符创建索引:
语法
create index index_(列名) on (表名)(列名(8))
create index index_dept on test(dept(8));
查看数据库大小:
要想知道每个数据库的大小的话,步骤如下:
1、进入information_schema 数据库(存放了其他的数据库的信息)
use information_schema;
2、查询所有数据的大小:
select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables;
3、查看指定数据库的大小:
比如查看数据库home的大小
select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘;
4、查看指定数据库的某个表的大小
比如查看数据库home中 members 表的大小
select concat(round(sum(data_length/1024/1024),2),‘MB‘) as data from tables where table_schema=‘home‘ and table_name=‘members‘;
mysql常用操作与练习题