mysql常用操作添加新用戶/分配權限/修改表/建索引等 bin>mysql -u root mysql> grant 權限1,權限2,…權限n on 數據庫名稱.表名稱 to 用戶名@用戶地址 identified by ‘連接口令’; 權限1,權限2,…權限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個權限。 當權限1,權限2,…權限n被all privileges或者all代替,表示賦予用戶全部權限。 當數據庫名稱.表名稱被*.*代替,表示賦予用戶操作服務器上所有數據庫所有表的權限。 用戶地址可以是localhost,也可以是ip地址、機器名字、域名。也可以用’%'表示從任何地址連接。 ‘連接口令’不能為空,否則創建失敗。 例如: mysql>grant select,insert,update,delete,create,drop on vtdc.employee to [email protected] identified by ‘123′; 給來自10.163.225.87的用戶joe分配可對數據庫vtdc的employee表進行select,insert,update,delete,create,drop等操作的權限,並設定口令為123。 mysql>grant all privileges on vtdc.* to [email protected] identified by ‘123′; 給來自10.163.225.87的用戶joe分配可對數據庫vtdc所有表進行所有操作的權限,並設定口令為123。 mysql>grant all privileges on *.* to [email protected] identified by ‘123′; 給來自10.163.225.87的用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。 mysql>grant all privileges on *.* to joe@localhost identified by ‘123′; 給本機用戶joe分配可對所有數據庫的所有表進行所有操作的權限,並設定口令為123。 創建含有外鍵的表: create table question(id int auto_increment primary key not null,content varchar(300) not null, intime datetime not null,u_id int not null,foreign key(u_id) references user(id) on delete cascade on update cascade); 備份數據表 shell> mysqldump [OPTIONS] database [tables] 例子: /usr/local/mysql/bin/mysqldump -u 用戶名 -p 數據庫名稱 > ./fedtrainning_db.sql 如果你不給定任何表,整個數據庫將被導出。 修改表的屬性 =======增、刪、改====================== ALTER TABLE notify CHANGE content content varchar(500) not null //主鍵 alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id); //增加一個新列 alter table t2 add d timestamp; alter table infos add ex tinyint not null default '0'; //刪除列 alter table t2 drop column c; //重命名列 alter table t1 change a b integer; //改變列的類型 alter table t1 change b b bigint not null; alter table infos change list list tinyint not null default '0'; //重命名表 alter table t1 rename t2; 加索引 mysql> alter table tablename change depno depno int(5) not null; mysql> alter table tablename add index 索引名 (字段名1[,字段名2 …]); mysql> alter table tablename add index emp_name (name); 加主關鍵字的索引 mysql> alter table tablename add primary key(id); 加唯一限制條件的索引 mysql> alter table tablename add unique emp_name2(cardnumber); 刪除某個索引 mysql>alter table tablename drop index emp_name; 修改表: 增加字段: mysql> ALTER TABLE table_name ADD field_name field_type; 修改原字段名稱及類型: mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type; 刪除字段: mysql> ALTER TABLE table_name DROP field_name; ======================= 編碼相關 ========================== 1修改整個數據庫服務器 在my.cf文件的[mysqld]段設置: default-character-set=utf8 2單獨設置某個數據庫: alter database testdb character set utf8; 3 查看mysql支持的編碼: show character set; 4查看數據庫的編碼格式: show create database testdb; 5 查看數據庫的各項編碼設置: mysql> SHOW VARIABLES LIKE 'character_set_%'; SET NAMES 'utf8'; 它相當於下面的三句指令: SET character_set_client = utf8; SET character_set_results = utf8; SET character_set_connection = utf8;