MySQL5.0常用命令
MySQL5.0中文问题:
登录时用命令:
mysql --default-character-set=gbk<gb2312,utf8> -u root -p
建表时用命令:
create table name(name varchar(20))[TYPE=MyISAM,]default character set gbk<gb2312,utf8>;
=====================
连接服务者:
mysql -h host -u user -p
=====================
新增超级用户:
GRANT ALL PRIVILEGES ON *.* TO grb@"%"
IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
=====================
返回当前所选数据库、当前用户、版本信息和当前时间:
select
database(),user(),version(),now();
=====================
查看库、表:
show databases;
use
databasename;
show tables;
show tables from databaseName;
=====================
建库、删库:
create database if not exsits
library;
drop database library;
=====================
建表:
一个表中只能有一个auto_increment;
在没指定default情况下:对于null就插入null,对于not
null,数值类型就插入0,字符串类型就插入
空字符串,时间戳就插入当前时期和时
间,enum类型就插入枚举组的第一条?
create table members(member_id int(11) not null auto_increment,fname
varc
har(50) default ‘guo‘ not null,lname varchar(50) not null,tel
varchar(15),email varchar
(50) no
t null,primary key(member_id));
not null auto_increment default primary key()
=====================
索引:
create table users(
user_id int(4) not
null auto_increment,
fname varchar(50) not null,
lname varchar(50) not
null,
index id(user_id);
create table users(
id int(4) not null auto_increment,
fname
varchar(50) not null,
lname varchar(50) not null,
index
(fname,lname),primary key(id));
=====================
为已存在的表建索引:
create index indexname on
tablename(column);
=====================
删除索引:
drop index indexname on tablename;
=====================
unique索引:
用unique修饰符指定输入字段值必须唯一.
create table
users(name varchar(8),pass varchar(20),unique(name));
=====================
* fulltext全文索引
=====================
主键:
index也可以用于多个字段,插入记录时要求两index不同时重复即可。
create
table firewall( host varchar(11) not null, port smallint(4) not null,access
enum
(‘deny‘,‘allow‘) not null,primary key
(host,port));
=====================
* 外键:
删除外键:alter table table_name drop foreign key key_id;
=====================
* 表类型:MyISAM、ISAM、HEAP、BerkeleyDB、InnoDB、MERGE
=====================
复制表:(create table…select不能复制键,要手工创建)
create table
newTable select field1,field2 from oldTable where
condition;
新建表并复制原表的若干字段:
create table newTable(newField1,newField2)
select oldField1,oldField2 from oldTable;
创建一个空副本:
create table newTable
select * from oldTable where 0=1;
另一种复制表方法:(MySQL 4.1)
create table
newTable like oldTable;
=====================
描述表(查看表结构):
describe
tableName;
=====================
查看索引:
show index from tableName;
=====================
查看数据库中表的详细信息:
show table status from
databaseName;
=====================
检索用sql命令创建的表:
show create table
databaseName.tableName;
=====================
修改表:
alter table members add othermessage
varchar(50) not null;//添加字段
alter table tableName add primary
key(email);//添加主键
alter table drop primary key;//删除主键
alter table
tableName change oldField newField auto_increment unique;//修改已存在字段
alter
table tableName drop field;//删除字段
alter table tableName add email varchar(30)
after id;//在指定位置添加新字段after或first
alter table tableName alter id set default
0;//设置或删除默认值
alter table tableName add id int(3) auto_increment primary key
first;//添加
auto_increment字段后,原有记录会被自动编号
alter ignore table tableName change name name
varchar(10) not null unique;//设置某一字段
为unique时用ignore字句删除重复记录
* 添加外键参照;
* 更改表类型;
* 添加删除索引;
alter table
addressbook_table default character set gb2312;
=====================
重命名表:
alter table oldTableName rename to
newTableName;
或:rename table oldTableName to newTableName;
=====================
删除表:
drop table tableName;
drop table if
exsits tableName;
=====================
查询记录:
select id,name,mail from
dbname.tablename;
=====================
插入记录:(into是可选的)
insert into members
values(null,‘guo‘,‘guo‘,‘7758521‘,‘[email protected]‘,null);
insert into
members(id,name,password) values(last_insert_id()+1,‘tom‘,‘secret‘);
insert
into members values(a,b,c),(d,e,f),(g,h,i);//一次插入多条记录
插入时间:insert into
time(now());
=====================
* replace
=====================
on duplicate key update
insert into
menu(id,label,url) values(a,b,c) on duplicate key update label=‘d‘,url=‘e‘;
=====================
删除所有记录:
delete from members;
=====================
修改记录:
update members set
tel=‘7654321‘,email=‘[email protected]‘ where member_id=1;
=====================
重复信息只显示一次:
select distinct name from members;
=====================
查询中运用算术运算:
select name,math+physics+chinese from
grades;
=====================
使用内建函数:
select count(*) from members;
select
avg(math),avg(phisics),avg(chinese) from grades;
select min(math) from
grades;
select max(math) from grades;
=====================
排序:
select * from members order by member_id,name
desc;//asc
=====================
限制条数:
select * from members limit
2,3;(从第2行开始显示,共显示3条记录)
select * from members limit 19,-1;//返回从第20行到表尾的记录
=====================
分组:
select name,count(*) from tableName group by
name;
=====================
having类似where
where针对所有记录,通常与select delete
update搭配
having只对经过操作的记录检索,以进一步筛选,通常与group by搭配
=====================
* 使用变量
=====================
通配符:
select * from members where name like
‘%guo%‘;
=====================
* 为表和列取别名
=====================
复制记录:
insert into users(name,pass) select
name,pass from otherTable;
=====================
导出记录:
select * from tableName into outfile
‘d:/abc.txt‘ fields terminated by ‘\t‘ enclosed by
‘@‘;
=====================
将.sql或.txt文件导入数据
mysql> \.
d:\site\grb.sql
注意:不用逗号结束。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。