mysql學習筆記

來源:互聯網
上載者:User

標籤:

MySQL 資料庫常用命令

 

1、MySQL常用命令

create database name; 建立資料庫

use databasename; 選擇資料庫

drop database name 直接刪除資料庫,不提醒

show tables; 顯示表

describe tablename; 表的詳細描述

select 中加上distinct去除重複欄位

mysqladmin drop databasename 刪除資料庫前,有提示。

顯示當前mysql版本和當前日期

select version(),current_date;

 

2、修改mysql中root的密碼:

shell>mysql -u root -p

mysql> update user set password=password(”xueok654123″) where user=’root’;

mysql> flush privileges //重新整理資料庫

mysql>use dbname; 開啟資料庫:

mysql>show databases; 顯示所有資料庫

mysql>show tables; 顯示資料庫mysql中所有的表:先use mysql;然後

mysql>describe user; 顯示表mysql資料庫中user表的列資訊);

 

3、grant

建立一個可以從任何地方串連伺服器的一個完全的超級使用者,但是必須使用一個口令something做這個

mysql> grant all privileges on *.* to [email protected] identified by ’something’ with

增加新使用者

格式:grant select on 資料庫.* to 使用者名稱@登入主機 identified by “密碼”

GRANT ALL PRIVILEGES ON *.* TO [email protected] IDENTIFIED BY ’something’ WITH GRANT OPTION;

GRANT ALL PRIVILEGES ON *.* TO [email protected]”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;

刪除授權:

mysql> revoke all privileges on *.* from [email protected]”%”;

mysql> delete from user where user=”root” and host=”%”;

mysql> flush privileges;

建立一個使用者custom在特定用戶端it363.com登入,可訪問特定資料庫fangchandb

mysql >grant select, insert, update, delete, create,drop on fangchandb.* to [email protected] it363.com identified by ‘ passwd’

重新命名表:

mysql > alter table t1 rename t2;

 

4、mysqldump

備份資料庫

shell> mysqldump -h host -u root -p dbname >dbname_backup.sql

恢複資料庫

shell> mysqladmin -h myhost -u root -p create dbname

shell> mysqldump -h host -u root -p dbname < dbname_backup.sql

如果只想卸出建表指令,則命令如下:

shell> mysqladmin -u root -p -d databasename > a.sql

如果只想卸出插入資料的sql命令,而不需要建表命令,則命令如下:

shell> mysqladmin -u root -p -t databasename > a.sql

那麼如果我只想要資料,而不想要什麼sql命令時,應該如何操作呢?

   mysqldump -T./ phptest driver

其中,只有指定了-T參數才可以卸出純文字檔案,表示卸出資料的目錄,./表示目前的目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個資料庫的資料。每個表會產生兩個檔案,一個為.sql檔案,包含建表執行。另一個為.txt檔案,只包含資料,且沒有sql指令。

 

5、可將查詢儲存在一個檔案中並告訴mysql從檔案中讀取查詢而不是等待鍵盤輸入。可利用外殼程式鍵入重新導向公用程式來完成這項工作。

例如,如果在檔案my_file.sql 中存放有查

詢,可如下執行這些查詢:

例如,如果您想將建表語句提前寫在sql.txt中:

mysql > mysql -h myhost -u root -p database < sql.txt

 

 

mysql命令

   一 . 常用mysql命令列命令 

  1 .mysql的啟動與停止 
  啟動MYSQL服務 net start mysql 
  停止MYSQL服務 net stop mysql


  2 . netstat –na | findstr 3306 查看被監聽的連接埠 , findstr用於尋找後面的連接埠是否存在


  3 . 在命令列中登陸MYSQL控制台 , 即使用 MYSQL COMMEND LINE TOOL 
   文法格式 mysql –user=root –password=123456 db_name 
   或 mysql –u root –p123456 db_name


  4 . 進入MYSQL命令列工具後 , 使用status; 或/s 查看運行環境資訊


  5 . 切換串連資料庫的文法 : use new_dbname; 
     
  6 . 顯示所有資料庫 : show databases; 
   
  7 . 顯示資料庫中的所有表 : show tables; 
   
  8 . 顯示某個表建立時的全部資訊 : show create table table_name; 
   
  9 . 查看錶的具體屬性資訊及表中各欄位的描述 
   Describe table_name; 縮寫形式 : desc table_name;


  三 。 MySql中的SQL語句 
  1 . 資料庫建立 : Create database db_name; 
  資料庫刪除 : Drop database db_name; 刪除時可先判斷是否存在,寫成 : drop database if exits db_name 
   
  2 . 建表 : 建立資料表的文法 : create table table_name (欄位1 資料類型 , 欄位2 資料類型); 
   例 : create table mytable (id int , username char(20)); 
   刪表 : drop table table_name; 例 : drop table mytable; 
   
  8 . 添加資料 : Insert into 表名 [(欄位1 , 欄位2 , ….)] values (值1 , 值2 , …..); 
  如果向表中的每個欄位都插入一個值,那麼前面 [ ] 括弧內欄位名可寫也可不寫 
   例 : insert into mytable (id,username) values (1,’zhangsan’); 
   
  9 . 查詢 : 查詢所有資料 : select * from table_name; 
  查詢指定欄位的資料 : select 欄位1 , 欄位2 from table_name; 
  例 : select id,username from mytable where id=1 order by desc;多表查詢語句------------參照第17條執行個體 
   
  10 . 更新指定資料 , 更新某一個欄位的資料(注意,不是更新欄位的名字) 
  Update table_name set 欄位名=’新值’ [, 欄位2 =’新值’ , …..][where id=id_num] [order by 欄位 順序] 
  例 : update mytable set username=’lisi’ where id=1; 
  Order語句是查詢的順序 , 如 : order by id desc(或asc) , 順序有兩種 : desc倒序(100—1,即從最新資料往後查詢),asc(從1-100),Where和order語句也可用於查詢select 與刪除delete 
   
  11 . 刪除表中的資訊 : 
   刪除整個表中的資訊 : delete from table_name; 
   刪除表中指定條件的語句 : delete from table_name where 條件陳述式 ; 條件陳述式如 : id=3; 
   
  12 . 建立資料庫使用者 
  一次可以建立多個資料庫使用者如: 
  CREATE USER username1 identified BY ‘password’ , username2 IDENTIFIED BY ‘password’…. 
   
  13 . 使用者的許可權控制:grant 
   庫,表級的許可權控制 : 將某個庫中的某個表的控制權賦予某個使用者 
   Grant all ON db_name.table_name TO user_name [ indentified by ‘password’ ]; 
   
  14 . 表結構的修改 
  (1)增加一個欄位格式: 
  alter table table_name add column (欄位名 欄位類型); ----此方法帶括弧 
  (2)指定欄位插入的位置: 
  alter table table_name add column 欄位名 欄位類型 after 某欄位; 
  刪除一個欄位: 
  alter table table_name drop欄位名; 
  (3)修改欄位名稱/類型 
  alter table table_name change 舊欄位名 新欄位名 新欄位的類型; 
  (4)改表的名字 
  alter table table_name rename to new_table_name; 
  (5)一次性清空表中的所有資料 
  truncate table table_name; 此方法也會使表中的取號器(ID)從1開始 
   
  15 . 增加主鍵,外鍵,約束,索引。。。。(使用方法見17執行個體) 
  ① 約束(主鍵Primary key、唯一性Unique、非空Not Null) 
  ② 自動增張 auto_increment 
  ③外鍵Foreign key-----與reference table_name(col_name列名)配合使用,建表時單獨使用 
  ④ 刪除多個表中有關聯的資料----設定foreign key 為set null ---具體設定參考說明文檔 
   
  16 . 查看資料庫當前引擎 
   SHOW CREATE TABLE table_name; 
   修改資料庫引擎 
   ALTER TABLE table_name ENGINE=MyISAM | InnoDB; 
   
  17 . SQL語句運用執行個體: 
  --1 建users表 
  create table users (id int primary key auto_increment,nikename varchar(20) not null unique,password varchar(100) not null,address varchar(200), reg_date timestamp not null default CURRENT_TIMESTAMP); 
   
  --2 建articles表,在建表時設定外鍵 
  create table articles (id int primary key auto_increment,content longtext not null,userid int,constraint foreign key (userid) references users(id) on delete set null); 
   
  ----------------------------------------------------------------------- 
  --2.1 建articles表,建表時不設定外鍵 
   create table articles (id int primary key auto_increment,content longtext not null,userid int); 
  --2.2 給articles表設定外鍵 
   alter table articles add constraint foreign key (userid) references users(id) on delete set null; 
  ------------------------------------------------------------------------ 
   
  --3. 向users表中插入資料,同時插入多條 
  insert into users (id,nikename,password,address) values (1,‘lyh1‘,‘1234‘,null),(10,‘lyh22‘,‘4321‘,‘湖北武漢‘),(null,‘lyh333‘,‘5678‘, ‘北京海澱‘); 
   
  --4. 向article中插入三條資料 
  insert into articles (id,content,userid) values (2,‘hahahahahaha‘,11),(null,‘xixixixixix‘,10),(13,‘aiaiaiaiaiaiaiaiaiaiaiaia‘,1),(14,‘hohoahaoaoooooooooo‘,10); 
   
  --5. 進行多表查詢,選擇users表中ID=10的使用者發布的所有留言及該使用者的所有資訊 
  select articles.id,articles.content,users.* from users,articles where users.id=10 and articles.userid=users.id order by articles.id desc; 
   
  --6. 查看資料庫引擎類型 
  show create table users; 
   
  --7. 修改資料庫引擎類型 
  alter table users engine=MyISAM; ---因為users表中ID被設定成外鍵,執行此句會出錯 
   
  --8. 同表查詢,已知一個條件的情況下.查詢ID號大於使用者lyh1的ID號的所有使用者 
  select a.id,a.nikename,a.address from users a,users b where b.nikename=‘lyh1‘ and a.id>b.id; 
  ------也可寫成 
  select id,nikename,address from users where id>(select id from users where nikename=‘lyh1‘); 
   
  9. 顯示年齡比領導還大的員工: 
  select a.name from users a,users b where a.managerid=b.id and a.age>b.age; 
   
  查詢編號為2的發帖人: 先查articles表,得到發帖人的編號,再根據編號查users得到的使用者名稱。 
  接著用關聯查詢. 
  select * from articles,users得到笛卡兒積,再加order by articles.id以便觀察 
   
  使用select * from articles,users where articles.id=2 篩選出2號文章與每個使用者的組合記錄 
   
  再使用select * from articles,users where articles.id=2 and articles.userid=users.id選出users.id等於2號帖的發帖人id的記錄. 
   
  只取使用者名稱:select user where user.id=(select userid from articles where article.id =2) 
   
  找出年齡比小王還大的人:假設小王是28歲,先想找出年齡大於28的人 
  select * from users where age>(select age from users where name=‘xiaowang‘); 
  *****要查詢的記錄需要參照表裡面的其他記錄: 
  select a.name from users a,users b where b.name=‘xiaowang‘ and a.age>b.age 
   
  表裡的每個使用者都想pk一下.select a.nickname,b.nickname from users a,users b where a.id>b.id ; 
   
  更保險的語句:select a.nickname,b.nickname from (select * from users order by id) a,(se 
  lect * from users order by id) b where a.id>b.id ; 
   
  再查詢某個人發的所有文章. 
  select b.* from articles a , articles b where a.id=2 and a.userid=b.userid 
   
  說明: 表之間存在著關係,ER概念的解釋,用access中的樣本資料庫示範表之間的關係.只有innodb引擎才支援foreign key,mysql的任何引擎目前都不支援check約束。 
  四、字元集出現錯誤解決辦法 
  出現的問題: 
  mysql> update users 
  -> set username=‘關羽‘ 
  -> where userid=2; 
  ERROR 1366 (HY000): Incorrect string value: ‘/xB9/xD8/xD3/xF0‘ for column ‘usern 
  ame‘ at row 1 
  向表中插入中文字元時,出現錯誤。 
   
  mysql> select * from users; 
  +--------+----------+ 
  | userid | username | 
  +--------+----------+ 
  | 2 | ???? | 
  | 3 | ???? | 
  | 4 | ?í?ù | 
  +--------+----------+ 
  3 rows in set (0.00 sec) 
  表中的中文字元位亂碼。 
  解決辦法: 
  使用命令: 
  

mysql> status;
--------------
mysql Ver 14.14 Distrib 5.5.19, for Win64 (x86)

Connection id: 3
Current database: test
Current user: [email protected]
SSL: Not in use
Using delimiter: ;
Server version: 5.6.17 MySQL Community Server (GPL)
Protocol version: 10
Connection: localhost via TCP/IP
Server characterset: latin1
Db characterset: utf8
Client characterset: latin1
Conn. characterset: latin1
TCP port: 3306
Uptime: 1 day 4 hours 18 min 56 sec

Threads: 5 Questions: 189 Slow queries: 0 Opens: 91 Flush tables: 1 Open tables: 66 Queries per second avg: 0.001


  -------------- 
  查看mysql發現Server characterset,Db characterset的字元集設成了latin1,所以出現中文亂碼。 
   
  mysql> show tables; 
  +----------------+ 
  | Tables_in_test | 
  +----------------+ 
  | users | 
  +----------------+ 
  1 row in set (0.00 sec) 
   
  更改表的字元集。 
  mysql> alter table users character set GBK; 
  Query OK, 3 rows affected (0.08 sec) 
  Records: 3 Duplicates: 0 Warnings: 0 
   
  查看錶的結構: 
  mysql> show create users; 
  ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
  corresponds to your MySQL server version for the right syntax to use near ‘users 
  ‘ at line 1 
  mysql> show create table users; 
  +-------+----------------------------------------------------------------------- 
  ------------------------------------------------------------------------------+ 
  | Table | Create Table 
  | 
  +-------+----------------------------------------------------------------------- 
  ------------------------------------------------------------------------------+ 
  | users | CREATE TABLE `users` ( 
  `userid` int(11) default NULL, 
  `username` char(20) character set latin1 default NULL 
  ) ENGINE=InnoDB DEFAULT CHARSET=gbk | 
  +-------+----------------------------------------------------------------------- 
  ------------------------------------------------------------------------------+ 
  1 row in set (0.00 sec) 
   
  mysql> desc users; 
  +----------+----------+------+-----+---------+-------+ 
  | Field | Type | Null | Key | Default | Extra | 
  +----------+----------+------+-----+---------+-------+ 
  | userid | int(11) | YES | | NULL | | 
  | username | char(20) | YES | | NULL | | 
  +----------+----------+------+-----+---------+-------+ 
  2 rows in set (0.02 sec) 
   
  這時向表中插入中文然後有錯誤。 
  mysql> insert into users values(88,‘中文‘); 
  ERROR 1366 (HY000): Incorrect string value: ‘/xD6/xD0/xCE/xC4‘ for column ‘usern 
  ame‘ at row 1 
  mysql> insert into users values(88,‘中文‘); 
  ERROR 1366 (HY000): Incorrect string value: ‘/xD6/xD0/xCE/xC4‘ for column ‘usern 
  ame‘ at row 1 
   
  還要更改users表的username的字元集。 
  mysql> alter table users modify username char(20) character set gbk; 
  ERROR 1366 (HY000): Incorrect string value: ‘/xC0/xEE/xCB/xC4‘ for column ‘usern 
  ame‘ at row 1 
  mysql> alter table users modify username char(20) character set gbk; 
  ERROR 1366 (HY000): Incorrect string value: ‘/xC0/xEE/xCB/xC4‘ for column ‘usern 
  ame‘ at row 1 
   
  因為表中已經有資料,所以更改username字元集的操作沒有成*** 
  清空users表中的資料 
  mysql> truncate table users; 
  Query OK, 3 rows affected (0.01 sec) 
   
  從新更改user表中username的字元集 
  mysql> alter table users modify username char(20) character set gbk; 
  Query OK, 0 rows affected (0.06 sec) 
  Records: 0 Duplicates: 0 Warnings: 0 
   

mysql學習筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.