MySQL常用操作


创建数据库

http://www.cnblogs.com/ggjucheng/archive/2012/11/02/2751840.html


设置root密码

/usr/bin/mysqladmin-uroot password {password}

/usr/bin/mysqladmin-uroot –p{password} password {new_password}

忘记密码

# /etc/init.d/mysql stop 
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 
# mysql -u root mysql 
mysql> UPDATE user SET Password=PASSWORD('newpassword') where USER='root';
mysql> FLUSH PRIVILEGES; 
mysql> quit


为指定ip的用户授权

授予指定IP以user为用户名secret为密码(同时创建用户),登录本机数据库权限:

mysql –uroot -proot

> grant all privileges on openfire.* to user@'10.101.1.%' identified by 'secret';

> flush privileges;


修改数据库的字符集
    mysql>use mydb
    mysql> alter database mydb default character set utf8;
创建数据库指定数据库的字符集
    mysql>create database mydb defaultcharacter set utf8;

mysql> CREATE DATABASE `zuqiua_dev` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;


插入记录

insert into {table-name} ({col-name1},{col-name2}) values ({col-value1},{col-value2});

insert into {table-name} ... values() ON DUPLICATE KEY UPDATE col1=value1,....

更新字段值

update {table-name} set {col-name}={col-value} where {condition clause}

使用子查询update所有相关的字段值

update home_cn_province_city a, (select a.id as id, a.province as province from home_cn_province a, home_cn_province_city b where a.province=b.city_en) b set a.province_id=b.id where a.city_en=b.province;

通过select插入记录

insert into table (name1,name2,name3) select name1,name2,name3 from table2;

insert into table (table.name1,table.name2) select name1,name2 from ( select a.name1, b.name2 a join b where a.id=b.id) c;

重命名数据表:

mysql> rename table {table-name} to {new-table-name}

增加字段

alter table register_info add messageUserID varchar(40) not NULL;

在指定位置增加字段

alter table register_info add messageUserID varchar(40) not null default '0'AFTER password;

alter table register_info add type2 tinyint not null default 0 AFTER type1;  (CHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci)

删除字段:、

alter table `register_info` drop column messageUserID;

修改字段名称/类型:(old_name, new_name, new_type)

ALTER TABLE register_info CHANGE password password varchar(40);

增加unique key:

ALTER TABLE register_info add constraint t_unique_key unique(phoneNumber);


查看索引:

show index from tblname;
show keys from tblname;

创建/添加索引:

alter table table_name add index [index_name] (column_list) ;
alter table table_name add unique (column_list) ;
alter table table_name add primary key (column_list) ;

删除索引:

drop indexindex_name on table_name ;
alter table table_name drop index index_name ;
alter table table_name drop primary key ;


备份数据库(内的所有表):

--no-data 只备份表结构不备份数据

mysqldump -uroot -p{password} [--no-data] {db-name} >  /path/to/{db-name}_back.sql

导入数据库(内的所有表):

mysql> use {db-name}

mysql> source  /path/to/{db-name}_back.sql


备份数据表

mysqldump -uroot -p{password} {db-name} {table-name} > /path/to/{table-name}.sql

导入数据表

(进入mysql命令行,use {db-name2},数据表需要先存在。)

mysql> use {db-name}

mysql> source /path/to/{table-name}.sql



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值