mysql必會命令整理

來源:互聯網
上載者:User

mysql必會命令整理

共39條命令,條條精闢

1、顯示當前資料庫伺服器中的資料庫列表:
mysql> SHOW DATABASES;
注意:mysql庫裡面有MYSQL的系統資訊,我們改密碼和新增使用者,實際上就是用這個庫進行操作。
2、顯示資料庫中的資料表:
mysql> USE 庫名;
mysql> SHOW TABLES;
3、顯示資料表的結構:
mysql> DESCRIBE 表名;
4、建立資料庫:
mysql> CREATE DATABASE 庫名;
5、建立資料表:
mysql> USE 庫名;
mysql> CREATE TABLE 表名 (欄位名 VARCHAR(20), 欄位名 CHAR(1));
6、刪除資料庫:
mysql> DROP DATABASE 庫名;
7、刪除資料表:
mysql> DROP TABLE 表名;
8、將表中記錄清空:
mysql> DELETE FROM 表名;
9、顯示表中的記錄:
mysql> SELECT * FROM 表名;
10、往表中插入記錄:
mysql> INSERT INTO 表名 VALUES (”hyq”,”M”);
11、更新表中資料:
mysql-> UPDATE 表名 SET 欄位名1=’a',欄位名2=’b’ WHERE 欄位名3=’c';
12、用文本方式將資料裝入資料表中:
mysql> LOAD DATA LOCAL INFILE “D:/mysql.txt” INTO TABLE 表名;
13、匯入.sql檔案命令:
mysql> USE 資料庫名;
mysql> SOURCE d:/mysql.sql;
mysql> FLUSH PRIVILEGES;
15、顯示use的資料庫名:
mysql> SELECT DATABASE();
16、顯示當前的user:
mysql> SELECT USER();
17、使用G按行垂直顯示結果
select * from USER\G;
18、建立使用者:
CREATE USER ‘haha’@'%' IDENTIFIED BY '123456';
19、建立資料庫:
CREATE DATABASE IF NOT EXISTS `haha`;
20、賦予許可權:
GRANT ALL PRIVILEGES ON `haha`.* TO  'haha'@'%';
21、重新整理許可權:
FLUSH PRIVILEGES;
22、撤銷許可權:
revoke all on *.* from 'e421083458'@'%';
23、命令列修改root密碼:
mysql> UPDATE mysql.user SET password=PASSWORD(’新密碼’) WHERE User=’root’;
24、插入表格樣本:
create table teacher(
id int(5) auto_increment NOT NULL primary key,
`name` varchar(20) default 'hello',
year date
);
25、插入資料
insert into teacher values(”,’allen’,'大連一中’,'1976-10-10′);
insert into teacher values(”,’jack’,'大連二中’,'1975-12-23′);

26、匯出表
mysqldump -u 使用者名稱 -p 資料庫名 > 匯出的檔案名稱
mysqldump -u user_name -p123456 database_name > outfile_name.sql
27、匯出一個表
mysqldump -u 使用者名稱 -p 資料庫名 表名> 匯出的檔案名稱
mysqldump -u user_name -p database_name table_name > outfile_name.sql
28、匯出一個資料庫結構
mysqldump -u user_name -p -d –add-drop-table database_name > outfile_name.sql
-d 沒有資料 –add-drop-table 在每個create語句之前增加一個drop table
29、帶語言參數匯出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql
30、更改表名:
    alter table table_name rename new_t;
31、添加列 :
    alter table table_name add column c_n column attributes;
32、刪除列:
    alter table table_name drop column c_n;
33、建立索引:
    alter table c_table add index (c_n1,c_n2);
    alter table c_table add unique index_name(c_n);
    alter table c_table add primary key(sid);
34、刪除索引:
    alter table c_table drop index c_n1;
35、更改列資訊:
    alter  table t_table change c_1 c_1 varchar(200);
    alter table t_table modify 1 c_1 varchar(200);
36、insert插入語句:
    insert into table_name (c_1,c_2)
        values ('x1',1);
37、update語句:
    update  table_name set c_1 =1 where c_2=3;
38、刪除資料庫或者表:
    drop table table_name;
    drop database database_name;//使用mysql_drop_db()可以刪除的.

39、破解本地的mysql使用者名稱密碼

    Windows:
    1.用系統管理員登陸系統。
    2.停止MySQL的服務。
    3.進入命令視窗,然後進入MySQL的安裝目錄,比如我的安裝目錄是c:\mysql,進入C:\mysql\bin
    4.跳過許可權檢查啟動MySQL,
    c:\mysql\bin>mysqld-nt --skip-grant-tables
    5.重新開啟一個視窗,進入c:\mysql\bin目錄,設定root的新密碼
    c:\mysql\bin>mysqladmin -u root flush-privileges password "newpassword"
    c:\mysql\bin>mysqladmin -u root -p shutdown
    將newpassword替換為你要用的root的密碼,第二個命令會提示你輸入新密碼,重複第一個命令輸入的密碼。
    6.停止MySQL Server,用正常模式啟動Mysql
    7.你可以用新的密碼連結到Mysql了。

    Unix&Linux:
    1.用root或者運行mysqld的使用者登入系統;
    2.利用kill命令結束掉mysqld的進程;
    3.使用--skip-grant-tables參數啟動MySQL Server
    shell>mysqld_safe --skip-grant-tables &
    4.為root@localhost設定新密碼
    shell>mysqladmin -u root flush-privileges password "newpassword"5.重啟MySQL Server

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.