2014-07-13 16:22:34
来 源
中存储网
MySQL
1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql2.导出一个表mysqldump -u 用户名 -p 数据库名 表名

1.导出整个数据库
  mysqldump -u 用户名 -p 数据库名 > 导出的文件名
  mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql
  2.导出一个表
  mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
  mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql
  3.导出一个数据库结构
  mysqldump -u wcnc -p -d --add-drop-table smgp_apps_wcnc >d:wcnc_db.sql
  -d 没有数据 --add-drop-table 在每个create语句之前增加一个drop table
  4.导入数据库
  常用source 命令
  进入mysql数据库控制台,
  如mysql -u root -p
  mysql>use 数据库
  然后使用source命令,后面参数为脚本文件(如这里用到的.sql)
  mysql>source d:wcnc_db.sql

===================================mysql命令集================================

导入用命令
mysql -u root -p book <book.sql //其中book为数据库。

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),uniq

ue(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','mailtoguoguo@163.com',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='mrguoguo@mail.csdn.net' 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:sitegrb.sql
注意:不用逗号结束。

创建/删除数据库
CREATE DATABASE mydb
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci

DROP DATABASE mydb

创建数据表
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tblname (
colname1 coltype coloptions reference,
colname2 coltype coloptions reference, ...
[, index1, index2, ...])
[ENGINE = MyISAM | InnoDB | HEAP]
[DEFAULT CHARSET = csname [COLLATE = colname]]

CREATE TABLE titles (
titleID INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
subtitle VARCHAR(100),
edition TINYINT,
pubID INT,
catID INT,
langID INT,
year INT,
isbn VARCHAR(20),
comment VARCHAR(255),
ts TIMESTAMP,
PRIMARY KEY (titleID),
KEY pubIdIndex (pubID),
KEY langID (langID),
KEY catID (catID),
KEY title (title),
CONSTRAINT titles_ibfk_1 FOREIGN KEY (pubID)
REFERENCES publishers (pubID),
CONSTRAINT titles_ibfk_2 FOREIGN KEY (langID)
REFERENCES publishers (langID),
CONSTRAINT titles_ibfk_3 FOREIGN KEY (catID)
REFERENCES categories (catID))
ENGINE = InnoDB
DEFAULT CHARSET = utf8 COLLATE = utf8_general_ci

CREATE TABLE table2 SELECT * FROM table1 where id <= 10;

DROP TABLE mytitle

创建/删除索引
CREATE INDEX idxTitle ON titles (title)
ALTER TABLE titles ADD INDEX idxTitle (title)
SHOW INDEX FROM tablename -- 列出数据表tablename的索引

DROP INDEX indexname ON tablename

ALTER TABLE titles ADD INDEX idxTitle (title(16))

变更表结构
ALTER TABLE tablename ADD newcolumn coltype coloptions [FIRST | AFTER]

ALTER TABLE tablename CHANGE oldcolname newcolumn coltype coloptions

ALTER TABLE tablename DROP colname

ALTER TABLE tblname ADD PRIMARY KEY (indexcols ...)
ALTER TABLE tblname ADD INDEX [indexname] (indexcols ...)
ALTER TABLE tblname ADD UNIQUE [indexname] (indexcols ...)
ALTER TABLE tblname ADD FULLTEXT [indexname] (indexcols ...)

ALTER TABLE tblname ADD FOREIGN KEY [idxname]
(column1) REFERENCES table2 (column2)

ALTER TABLE tblname DROP PRIMARY KEY
ALTER TABLE tblname DROP INDEX indexname
ALTER TABLE tblname DROP FOREIGN KEY indexname

ALTER TABLE tablename ENGINE typename

MySQL伴随Linux启动
vi /etc/rc.d/rc.local
加入语句: ./mysqld_safe --user=mysql &

改变root用户密码
mysql> update mysql.user set password=password('YourPwd') where user='root';
mysql> flush privileges;

增加新用户
[1]添加一用户,使它拥有所有权限
mysql> grant all on mydb.* to myuser@"myhost" Identified by "mypassword";

[2]简单添加一用户
mysql> i nsert into mysql.user (host, user, password) values('%', 'NewUser', password('NewUserPwd'));
mysql> flush privileges;
注:最好使用grant命令。

启动/关闭MySQL服务器
启动(Linux下): mysqld_safe -u mysql &
关闭: mysqladmin -uUser -pPassword -hHost shutdown

导出/导入数据
导出:
mysqldump -uUser -pPasswd -hHost -B DB_NAME > SQL_FILE_PATH -- 将数据库DB_NAME全部导出到SQL_FILE_PATH文件中
mysqldump -uUser -pPassword -hHost DATABASE_NAME --tables TABLE_NAME > SQL_FILE_PATH -- 将数据库DB_NAME中的表TABLE_NAME导出

导入:
mysql -uUser -pPassword -hHost < SQL_FILE_PATH -- 将数据文件导入MySQL服务器中。注:该文件中必须有建数据库的语句。
mysqldump -uUser -pPassword -hHost -B DB_NAME < SQL_FILE_PATH -- 将数据文件导入MySQL服务器的DB_NAME数据库中。注:该文件中没有建数据库的语句。
注意:在导入数据库之前,需要在MySQL中新建一个名为DATABASE_NAME的空数据库。

启动日志
修改mysql.ini/mysql.cnf文件,在[mysqld]后添加如下形式的语句。
log="Absolte_Path_To_Log/sys.log"
log-update=" Absolte_Path_To_Log/ sys_update.log"

使MySQL区分大小写
修改文件my.ini/my.cnf,在选项组[mysqld]之后添加
lower_case_table_names=2

修改某用户的登录密码
mysqladmin -uUser -pOldPasswd passwd NewPasswd
mysql>update user set password=PASSWORD("NewPasswd") where user="myuser" and host="myhost";

查看表结构
mysql> describe TableName

修改表的字符集
mysql> alter table tblName convert to character set charsetName

改表的表类型
mysql> alter table tblName engine engineType

SHOW
show character set -- 查看字符集
show character set
show collation
show columns
show create database
show create procedure and show create function
show create table
show create view
show databases
show engine
show engines
show errors
show grants
show index
show innodb status
show logs
show mutex status
show open tables
show privileges

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。