标签:ldb 表结构 路径 foreign lint 最小 添加 数值 index
查看当前所在库;
select database();
创建gamedb库;
create database gamedb;
切换到gamedb库;
use gamedb;
查看gamedb库里的表;
show tables;
创建gamedbinfo表;
create table gamedb.gamedbinfo(name char(10),age int);
use gamedb;
show tables;
查看gamedbinfo表结构;
desc gamedb.gamedbinfo; (desc后跟绝对路径,如果查询当前库里的表结构,则可以使用相对路径,如:desc gamedbinfo;)
查看 gamedb库里gamedbinfo表所有内容;
select * from gamedb.gamedbinfo;
插入表记录;
insert into gamedb.gamedbinfo values("Tom",22),("Maria",33);
修改表记录;
update gamedb.gamedbinfo set age=19 where name="Tom"; (修改Tom的记录)
update gamedb.gamedbinfo set age=19 (修改所有记录)
查看表所有内容;
select * from gamedb.gamedbinfo;
删除表记录;
delete from gamedb.gamedbinfo where name="Tom"; (删除Tom的记录)
delete from gamedb.gamedbinfo; (删除表中所有记录)
删除表;
drop table gamedb.gamedbinfo;
删除库;
drop database gamedb;
添加字段;
alter table T4 add email varchar(30) default "stu@tedu.cn";
alter table T4 add tel char(11);
alter table T4 add stu_num char(9) first;
alter table T4 add class char(7) default "nsd1808" after name;
修改字段类型,约束条件
alter table T4 modify stu_num varchar(10);
alter table T4 modify age tinyint default 25;
alter table T4 modify age tinyint default 25 after name;
alter table T4 modify likes set(‘eat‘,‘sleep‘,‘game‘,‘zhang‘,‘it‘,‘book‘) null default "it,book";
修改字段名;
alter table T4 change email mail varchar(30) default "stu@tedu.cn";
删除字段:
alter table T4 drop mail;
修改表名:
alter table T4 rename stutab;
创建index索引:
create index name on T3(name);
desc T3;
show index from T3;
drop index name on T3;
create table T4 (
name char(10) not null,
age tinyint not null default 25,
likes set("eat","sleep","game","zhang")
default "eat,sleep",
index(name),index(age)
);
desc T4;
show index from T4;
创建主键(primary key)
方法一:
create table T5(
name char(5),
class char(7),
stu_num char(9),
primary key(stu_num)
);
方法二:
create table T6(
name char(5),
class char(7),
stu_num char(9) primary key
);
创建复合主键:
create table T6(
clientip char(15),
serport smallint unsigned,
status enum("allow","deny"),
primary key(clientip,serport)
);
删除主键:
alter table T6 drop primary key;
在已有表里创建主键:
alter table T6 add primary key(serport,clientip);
与 AUTO_INCREMENT 连用 让字段的值自动增长:
数值类型
必须是primary key
create table T7(
id int primary key auto_increment,
name char(10),
age tinyint
);
删除id字段自增长功能:
alter table T7 modify id int not null;
创建外键:
create table yg(
yg_id int primary key auto_increment,
name char(10)
)engine=innodb;
create table gz(
gz_id int,
pay float(7,2)
foreign key(gz_id) references yg(yg_id)
on uptade cascade on delete cascade
)engine=innodb;
查看建表的过程:
show create table gz;
同步更新:
update yg set yg_id=8 where yg_id=2;
同步删除:
delete from yg where yg_id=1;
设置默认存储引擎:
修改/etc/my.cnf配置文件
default-storage-engine=存储引擎名称
vim /etc/my.cnf
[mysqld]
default-storage-engine=myisam
查询user表里name字段的个数:
select count(name) from user;
查询user表里name字段当其shell不等于/bin/bash的个数:
select count(name) from user where shell!="/bin/bash";
查询user表里,最小uid号码:
select min(uid) from user;
查询user表里,最小uid号码,当uid号<1000
select min(uid) from user where uid<=1000;
查询结果排序:
select name,uid from user order by uid; (升序)
select name,uid from user order by uid desc; (降序)
查询结果分组:
select gid from user group by gid;
select shell from user group by shell;
select shell from user where uid>10 and uid<=2000 group by shell;
数据完全备份:mysqldump
mkdir /mydata
mysqldump -uroot -p123456 -A > /mydata/alldb.sql
备份db4库数据:
mysqldump -uroot -p123456 db4 > /mydata/db4.sql
备份db4库里的t2表:
mysqldump -uroot -p123456 db4 t2 > /mydata/db4_t2.sql
同时备份多个库:
mysqldump -uroot -p123456 -B db3 db2 > /mydata/twodb.sql
完全恢复数据()删除对应的备份数据,然后使用备份文件恢复数据:
mysql -uroot -p123456 db4 < /mydata/db4_t2.sql
mysql -uroot -p123456 db4 < /mydata/db4.sql
启用binlog日志,进行实时增量备份:
binlog日志介绍?
启用日志文件:
vim /etc/my.cnf
[mysqld]
#log_bin
log_bin=/logdir/plj
server_id=50
binlog_format="mixed"
:wq
mkdir /logdir
chown mysql /logdir
systemctl restart mysqld
ls /logdir/plj.*
手动生成新的日志文件几种方法:
1.重启mysql服务
2.执行SQL操作,登陆mysql后执行命令: flush logs;
3.mysqldump --flush-logs
4.mysql -uroot -p123456 -e ‘flush logs‘
查看日志文件内容,查看日志文件记录新数据的范围,并记录偏移量的范围:
mysqlbinlog /logdir/plj.000001
执行binlog日志记录的sql命令恢复数据:
方法一:
mysqlbinlog --start-position=958 --stop-position=1153 /logdir/plj.000001
方法二:
mysqlbinlog --start-datatime="2018/11/21 15:26:31" --stop-datatime="2018/11/21 15:31:16" /logdir/plj.000001
使用完全备份文件恢复数据:
mysql -uroot -p123456 db4 < /root/t2.sql
标签:ldb 表结构 路径 foreign lint 最小 添加 数值 index
原文地址:https://www.cnblogs.com/jiang-bokeyuan/p/11703455.html