mysql常用操作添加新用戶/分配權(quán)限/修改表/建索引等
bin>mysql -u root
mysql> grant 權(quán)限1,權(quán)限2,…權(quán)限n on 數(shù)據(jù)庫(kù)名稱.表名稱 to 用戶名@用戶地址 identified by ‘連接口令’;
權(quán)限1,權(quán)限2,…權(quán)限n代表select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個(gè)權(quán)限。
當(dāng)權(quán)限1,權(quán)限2,…權(quán)限n被all privileges或者all代替,表示賦予用戶全部權(quán)限。
當(dāng)數(shù)據(jù)庫(kù)名稱.表名稱被*.*代替,表示賦予用戶操作服務(wù)器上所有數(shù)據(jù)庫(kù)所有表的權(quán)限。
用戶地址可以是localhost,也可以是ip地址、機(jī)器名字、域名。也可以用’%'表示從任何地址連接。
‘連接口令’不能為空,否則創(chuàng)建失敗。
例如:
mysql>grant select,insert,update,delete,create,drop on vtdc.employee to joe@10.163.225.87 identified by ‘123′;
給來(lái)自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫(kù)vtdc的employee表進(jìn)行select,insert,update,delete,create,drop等操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on vtdc.* to joe@10.163.225.87 identified by ‘123′;
給來(lái)自10.163.225.87的用戶joe分配可對(duì)數(shù)據(jù)庫(kù)vtdc所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on *.* to joe@10.163.225.87 identified by ‘123′;
給來(lái)自10.163.225.87的用戶joe分配可對(duì)所有數(shù)據(jù)庫(kù)的所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
mysql>grant all privileges on *.* to joe@localhost identified by ‘123′;
給本機(jī)用戶joe分配可對(duì)所有數(shù)據(jù)庫(kù)的所有表進(jìn)行所有操作的權(quán)限,并設(shè)定口令為123。
創(chuàng)建含有外鍵的表:
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);
備份數(shù)據(jù)表
shell> mysqldump [OPTIONS] database [tables]
例子:
/usr/local/mysql/bin/mysqldump -u 用戶名 -p 數(shù)據(jù)庫(kù)名稱 > ./fedtrainning_db.sql
如果你不給定任何表,整個(gè)數(shù)據(jù)庫(kù)將被導(dǎo)出。
修改表的屬性 =======增、刪、改======================
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);
//增加一個(gè)新列
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);
加主關(guān)鍵字的索引
mysql> alter table tablename add primary key(id);
加唯一條件的索引
mysql> alter table tablename add unique emp_name2(cardnumber);
刪除某個(gè)索引
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;
======================= 編碼相關(guān) ==========================
1修改整個(gè)數(shù)據(jù)庫(kù)服務(wù)器
在my.cf文件的[mysqld]段設(shè)置:
default-character-set=utf8
2單獨(dú)設(shè)置某個(gè)數(shù)據(jù)庫(kù):
alter database testdb character set utf8;
3 查看mysql支持的編碼:
show character set;
4查看數(shù)據(jù)庫(kù)的編碼格式:
show create database testdb;
5 查看數(shù)據(jù)庫(kù)的各項(xiàng)編碼設(shè)置:
mysql> SHOW VARIABLES LIKE 'character_set_%';
SET NAMES 'utf8';
它相當(dāng)于下面的三句指令:
SET character_set_client = utf8;
SET character_set_results = utf8;
SET character_set_connection = utf8;
bitsCN.comCopyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬(wàn)商天勤律師事務(wù)所王興未律師提供法律服務(wù)