日期:2014-05-16  浏览次数:20761 次

MySQL学习总结

????前一段时间很忙,基本没有自己写Blog多少都是转载别人的,想想?很长时间没有使用MySQL数据库了,今天有空闲时间,再次开始学习MySQL数据的一些知识总结一下:

?

?

MySQL总结
基本信息查看
登录: mysql -uroot -pxxx? -h192.168.0.1
语法帮助:> help show variables;
查数据库:show databases;
?创建数据库
?create database
?删除数据库:
?drop database if exists testdb;
查告警:show warnings;
查看数据库及表,行数:mysqlshow -uroot --count;? mysqlshow --count
切换数据库:use? xxx;?
查数据库中的表:show tables;
查看数据库中的参数:
show variables ;
show variables like '%lo%';

?

?

?
?????? show的命令的学习
?????? show语法如下:
????????? SHOW DATABASES [LIKE wild]
??????or SHOW TABLES [FROM db_name] [LIKE wild]
??????or SHOW COLUMNS FROM tbl_name [FROM db_name] [LIKE wild]
??????or SHOW INDEX FROM tbl_name [FROM db_name]
??????or SHOW STATUS
??????or SHOW VARIABLES [LIKE wild]
??????or SHOW [FULL] PROCESSLIST
??????or SHOW TABLE STATUS [FROM db_name] [LIKE wild]
??????or SHOW GRANTS FOR user
??????
??????
???例如:
???查看数据库表中索引
???show? index from userinfo from testdb;
???show index from testdb.userinfo;
???查询表的状态
???show table status;
???或者
???show status;
???
???查看用户的权限
???show grants for xiaobai@'localhost';
???
???explain语法:
??? EXPLAIN tbl_name
?????or? EXPLAIN SELECT select_options
?????
?????EXPLAIN tbl_name是DESCRIBE tbl_name或SHOW COLUMNS FROM tbl_name的一个同义词。
?????

?

查看表结构的方法
方法一:
desc userinfo; 或者describe userinfo;


方法二:
show columns from userinfo;

?

查看表的schema
show create table 表名;

?

通过information_schema看表结构
use information_schema

select * from columns where table_name='表名';

?

针对表的操作
????
?创建Mysql表结构:

drop table if?exists? userinfo ;


?create table? userinfo
?(
?userName varchar(25) not null),
?email varchar(30)
?);
?
?添加一列
?alter table userinfo
?add column userId varchar(20) not null;
?
?设置为主键
?alter table userinfo
?add prmariy key (userId);
?
?删除一列:
?alter table userinfo
?drop column? userId ;
?
?
?添加列并添加主键:
?alter table userinfo
? add column userId int? primary key (userId);
?
?
???? 添加列并主键:添加主键同时为自增
?alter table userinfo
? add column userId int? primary key auto_increment;
?
? 设置自动增长的列从10开始增长
? alter table userinfo
? auto_increment=10;
?
? 设置列的默认值的方法
? alter table userinfo
? add column sex int not null default 1;
?
? 添加唯一约束
? alter table userinfo
?? add unique UQ_UserID(userId);
??
??
?? 添加单字段索引的
?? alter table userinfo
???? add index index_UserId(userId);
?
?
???? 添加多字段索引的
?? alter table userinfo
???? add index index_UN(userId,email);
???
???
???? 删除索引
??? alert table userinfo?
????? drop index index_UN;
?????
?????
????? 修改表名称
????? alter table userInfo rename user_Info;
?????
????? 或者
????? rename table userInfo to user_Info;
?????
?????
????? 表的复制
????? create table user_info
????? select * from userInfo;
?????

????? 删除表
????? drop table user_info;

?

?

????? 删除数据库

????? drop? database userdb;

?

?

?

?

?查看数据库的编码格式:show create database testdb;
?????
?????
????? 让其用UTF-8编码创建数据库:
?????? create database testdb character set utf8;
??????

?

?

?

?

?

?

?MySQL用户信息管理
?????? 查看用户信息
?????? select * from mysql.user where user='root';
?????? 或者
?????? show grants for root@localhost;
?????? 创建用户
?????? CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']
??????????? [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
????????
???????? 例如:??
???????? create User xiaobai identified by 'abc'