萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> MySQL數據庫常用命令用法總結

MySQL數據庫常用命令用法總結

   在日常工作中,會簡單的使用一下mysql,故對常見命令操作總結一下,常用方式如下

  mysqldump 命令的使用:

  備份和導出數據庫

  mysqldump -h database_ip -u Username -p --opt databasename > backup-file.sql

  只導出數據庫表結構

  mysqldump -h database_ip -d -u Username -p databasename >database_structure.sql

  只導出數據庫中的某個表

  mysqldump --opt --add-drop-table -u Username -p databasename tablename > dump.sql

  如果不想手工輸入密碼 請使用--password 參數

  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 命令使用:

  將查詢結果保存到文件

  select title from book into outfile '/tmp/outfile.txt';

  查找表中多余的重復記錄,重復記錄是根據某個字段(peopleId)來判斷

  select * from people where peopleId in (select peopleId from people group by

  peopleId having count(peopleId) > 1);

  查詢表中不重復記錄(排除重復記錄)

  select * from phome_ecms_wma where title in (select distinct title from phome_ecms_wma);

  刪除表中重復記錄,重復記錄是根據某個字段(title)來判斷

  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;

  查詢數據庫當前編碼

  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';

  給表添加一個新字段

  mysql> ALTER TABLE host ADD ks_mac VARCHAR(100);

  從表中刪除一個字段

  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);

  加主關鍵字的索引

  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 設置

  mysql> GRANT ALL PRIVILEGES ON database_test.* to [email protected] IDENTIFIED BY '123456';

  mysql> FLUSH PRIVILEGES;

copyright © 萬盛學電腦網 all rights reserved