标签:primary 备份文件 person desc 好处 表结构 arc char 自己的
sc delete MySQL
mysql -uroot -ppasswd -h host
mysqladmin -uroot -ppasswd shutdown
mysqladmin -uroot -poldpasswd password newpasswd
update mysql.user set password=password(newpasswd) where user=‘root‘ and host=‘localhost‘;
create database dbname
create database dbname character set utf8 collate utf8_general_ci
show variables like ‘%set%‘;
show databases;
use databasename;
drop database dbname;
select database();
show create database dbname;
create user username@host identified by passwd;
drop user username@host;
select user();
授权
grant priviledges on dbname.* to username@host;
note: 权限包括select,insert,update,create,drop,peferences,idex, alter,idex,create temporary tables,lock tables,
execute,create view, show view,show view,create routine,alter routine, event, trigger
show grants for username@host;
revoke privileges on dbname.* from username@host;
刷新权限
flush privileges
grant all on privileges *.* to dbname@10.0.0.% identified by passwd;
grant all on *.* to dbname@255.255.255.0 identified by passwd;
查询数据库,按照主键索引查询最快,每个表只能有一个主键列,但是可以有多个普通索引列。
主键列要求列的所有内容必须唯一,而索引列不要求内容必须唯一
create table tablename(
id int(4) not null auto_increment,
name char(20) not null,
primary key(id), --主键索引
key index_name(name) --普通索引`
);
alter table tablename add index index_name(name);
alter table tablename change id id int primary key auto_increment;
create index index_name on tablename(name);
create index index_name on tablename(name(3));
创建联合索引
create index index_name on tablename(name, age);
note:联合索引上的字段可以有自己的索引且条件查询数据时,联合索引采用前缀生效特征,
即:联合索引有a,b,c,仅a,ab,abc三个查询条件列可以走索引,ac就不可以
create unique index uni_index_name on tablename(name);
查看索引
show index from table\G
note:\G让展示内容垂直分布
alter table tablename drop index index_name;
索引创建及生效条件
索引相当于文章的目录,好处是加快查询速度,但是占用系统空间,更新数据库是还要维护索引数据。
因此数十行到几百行的小表无需建立索引;写频繁,读少的业务要少建索引
建索引的位置
索引一定要创建在条件列,而不是select后的选择数据的列。另外要尽量选择在唯一值多的大表上建立索引
show tables;
show create table tablename;
desc tablename;
ALTER TABLE tablename RENAME [TO] new_tablename
ALTER TABLE tablename MODIFY [COLUMN] column_definition [FIRST | AFTER col_name]
ALTER TABLE tablename ADD [COLUMN] column_definition [FIRST | AFTER col_name]
ALTER TABLE tablename DROP [COLUMN] col_name
ALTER TABLE tablename CHANGE [COLUMN] old_col_name column_definition [FIRST|AFTER col_name]
note: change可以修改列名称,modify 则不能
INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn);
INSERT INTO tablename (field1,field2,……fieldn) VALUES(value1,value2,……valuesn), (value1,value2,……valuesn)...;
删
delete from tablename where 条件;
drop table tablename;
truncate tablename;
二者区别:truncate删除表的速度更快,因为truncate时通过物理磁盘删除的;而drop是通过逻辑一行一行删除的。
改
update tablename set field1=value1, field2=value2,... [where condition];
查
slect field1, field2,... from tablename [where condition];
检查查有没有走索引的方法
explain sql语句
备份
mysqldump -uroot -ppasswd -B dbname > filename.sql
note: -B备份指定库,使用-B备份文件中多了两行代码(create database dbname; use dbname;)
-A是备份所有的库
恢复
mysqldump -uroot -ppasswd dbname < filename.sql
标签:primary 备份文件 person desc 好处 表结构 arc char 自己的
原文地址:https://www.cnblogs.com/nwu-edu/p/9384309.html