在日常工作中,會(huì)簡(jiǎn)單的使用一下mysql,故對(duì)常見(jiàn)命令操作總結(jié)一下,常用方式如下
mysqldump 命令的使用:
備份和導(dǎo)出數(shù)據(jù)庫(kù)
mysqldump -h database_ip -u Username -p --opt databasename > backup-file.sql
只導(dǎo)出數(shù)據(jù)庫(kù)表結(jié)構(gòu)
mysqldump -h database_ip -d -u Username -p databasename >database_structure.sql
只導(dǎo)出數(shù)據(jù)庫(kù)中的某個(gè)表
mysqldump --opt --add-drop-table -u Username -p databasename tablename > dump.sql
如果不想手工輸入密碼 請(qǐng)使用--password 參數(shù)
mysqldump -h database_ip -u Username --password=123456 --opt databasename > backup-file.sql
mysqldump -h database_ip -d -u Username --password=123456 databasename >database_structure.sql
mysql 命令使用:
將查詢結(jié)果保存到文件
select title from book into outfile '/tmp/outfile.txt';
查找表中多余的重復(fù)記錄,重復(fù)記錄是根據(jù)某個(gè)字段(peopleId)來(lái)判斷
select * from people where peopleId in (select peopleId from people group by
peopleId having count(peopleId) > 1);
查詢表中不重復(fù)記錄(排除重復(fù)記錄)
select * from phome_ecms_wma where title in (select distinct title from phome_ecms_wma);
刪除表中重復(fù)記錄,重復(fù)記錄是根據(jù)某個(gè)字段(title)來(lái)判斷
select *,count(distinct title) INTO OUTFILE '/tmp/table.bak' from phome_ecms_wma group by title;
delete from phome_ecms_wma;
LOAD DATA INFILE '/tmp/table.bak' REPLACE INTO TABLE phome_ecms_wma character set utf8;
查詢數(shù)據(jù)庫(kù)當(dāng)前編碼
mysql> show variables like "character_set%";
修改表字段類型
mysql> alter table table_name change last_action last_action datetime NOT NULL default '0000-00-00 00:00:00';
給表添加一個(gè)新字段
mysql> ALTER TABLE host ADD ks_mac VARCHAR(100);
從表中刪除一個(gè)字段
mysql> ALTER TABLE table_name DROP field_name;
重命名表
mysql>alter table t1 rename t2;
給字段加索引
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;
遠(yuǎn)程訪問(wèn)mysql 設(shè)置
mysql> GRANT ALL PRIVILEGES ON database_test.* to IDENTIFIED BY '123456';
mysql> FLUSH PRIVILEGES;
更多信息請(qǐng)查看IT技術(shù)專欄