命令列下操作MySQL資料庫,各種命令的使用入門樣本。(附圖)

來源:互聯網
上載者:User

標籤:led   資訊   語句   incr   標識   using   signed   root   sig   

登入到MySQL:
mysql -h 主機名稱 -u 使用者名稱 -p
-h : 該命令用於指定用戶端所要登入的MySQL主機名稱, 登入當前機器該參數可以省略;
-u : 所要登入的使用者名稱;
-p : 告訴伺服器將會使用一個密碼來登入, 如果所要登入的使用者名稱密碼為空白, 可以忽略此選項。


建立一個資料庫:
create database 資料庫名 [其他選項];
提示: 可以使用 show databases; 命令查看已經建立了哪些資料庫。



選擇所要操作的資料庫:
use lxk


建立資料庫表:
create table 表名稱(列聲明);
以建立 students 表為例, 表中將存放 學號(id)、姓名(name)、性別(sex)、年齡(age)、聯絡電話(tel) 這些內容:
create table students
(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);


對於一些較長的語句在命令提示字元下可能容易輸錯, 因此我們可以通過任何文字編輯器將語句輸入好後儲存為 xxx.sql 的檔案中, 通過命令提示字元下的檔案重新導向執行執行該指令碼.
開啟命令提示字元, 輸入: mysql -D lxk -u root -p < student.sql
直接進入命令列模式,也就是沒登入資料庫的時候。
注意,只能是大寫的D,小寫d失敗。後面的lxk是資料庫名。
提示: 
1.如果串連遠程主機請加上 -h 指令; 
2. createtable.sql 檔案若不在當前工作目錄下需指定檔案的完整路徑。



語句解說:
create table tablename(columns) 為建立資料庫表的命令, 列的名稱以及該列的資料類型將在括弧內完成;
括弧內聲明了5列內容, id、name、sex、age、tel為每列的名稱, 後面跟的是資料類型描述, 列與列的描述之間用逗號(,)隔開;
以 "id int unsigned not null auto_increment primary key" 行進行介紹:
"id" 為列的名稱;
"int" 指定該列的類型為 int(取值範圍為 -8388608到8388607), 在後面我們又用 "unsigned" 加以修飾, 表示該類型為無符號型, 此時該列的取值範圍為 0到16777215;
"not null" 說明該列的值不可為空, 必須要填, 如果不指定該屬性, 預設可為空白;
"auto_increment" 需在整數列中使用, 其作用是在插入資料時若該列為 NULL, MySQL將自動產生一個比現存值更大的唯一識別碼值。在每張表中僅能有一個這樣的值且所在列必須為索引列。
"primary key" 表示該列是表的主鍵, 本列的值必須唯一, MySQL將自動索引該列。
下面的 char(8) 表示儲存的字元長度為8, tinyint的取值範圍為 -127到128, default 屬性指定當該列值為空白時的預設值。


向表中插入資料
insert 語句可以用來將一行或多行資料插到資料庫表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
其中 [] 內的內容是可選的, 例如, 要給 samp_db 資料庫中的 students 表插入一條記錄, 執行語句:
insert into students values(NULL, "王剛", "男", 20, "13811371377");


查詢表中的資料
select 語句常用來根據一定的查詢規則到資料庫中擷取資料, 其基本的用法為:
select 列名稱 from 表名稱 [查詢條件];
例如:
要查詢 students 表中所有學生的名字和年齡, 
輸入語句 select name, age from students;
也可以使用萬用字元 * 查詢表中所有的內容, 
語句: select * from students;


按特定條件查詢:

where 關鍵詞用於指定查詢條件, 用法形式為: select 列名稱 from 表名稱 where 條件;
以查詢所有性別為女的資訊為例, 輸入查詢語句: select * from students where sex="女";
1.查詢年齡在21歲以上的所有人資訊: select * from students where age >= 21;
2.查詢名字中帶有 "王" 字的所有人資訊: select * from students where name like "%王%";
3.查詢id小於5且年齡大於20的所有人資訊: select * from students where id<5 and age>20;


更新表中的資料
update 語句可用來修改表中的資料, 基本的使用形式為:
update 表名稱 set 列名稱=新值 where 更新條件;
1.將id為2的手機號改為預設的"-": update students set tel=default where id=2;
2.將所有人的年齡增加1: update students set age=age+1;
3.將手機號為 13288097888 的姓名改為 "張偉鵬", 年齡改為 19: update students set name="張偉鵬", age=19 where tel="13288097888";


刪除表中的資料
delete 語句用於刪除表中的資料, 基本用法為:
delete from 表名稱 where 刪除條件;
1.刪除id為2的行: delete from students where id=2;
2.刪除所有年齡小於21歲的資料: delete from students where age=22;
3.刪除表中的所有資料: delete from students;



建立後表的修改
alter table 語句用於建立後對錶的修改, 基礎用法如下:

添加列
基本形式: alter table 表名 add 列名 列資料類型 [after 插入位置];
1.在表的最後追加列 address: alter table students add address char(60);
2.在名為 age 的列後插入列 birthday: alter table students add birthday date after age;



修改列
基本形式: alter table 表名 change 列名稱 列新名稱 新資料類型;
1.將表 tel 列改名為 telphone: alter table students change tel telphone char(13) default "-";
2.將 name 列的資料類型改為 char(16): alter table students change name name char(16) not null;


刪除列
基本形式: alter table 表名 drop 列名稱;
樣本:刪除 birthday 列: alter table students drop birthday;


重新命名表
基本形式: alter table 表名 rename 新表名;
樣本:重新命名 students 表為 workmates: alter table students rename workmates;


刪除整張表
基本形式: drop table 表名;
樣本: 刪除 workmates 表: drop table workmates;


刪除整個資料庫

基本形式: drop database 資料庫名;
樣本: 刪除 samp_db 資料庫: drop database samp_db;


最後:建立一個資料庫使用者和密碼,並且設定所有許可權。
create user ‘lxk‘@‘localhost‘ identified by ‘lxk‘;
grant all privileges on lxk.* to ‘lxk‘@‘localhost‘;
flush privileges;
具體怎麼解釋,我也不是很清楚,但就是這麼用的。跟上我的個人理解吧。
上面就是建立了個使用者,@標識本地的資料庫,identified by 後面的跟的就是密碼了。
grant單詞就是授權的意思,all,全部,privileges,特權的意思。lxk.*估計就是lxk使用者下的所有的資料庫吧,


CREATE USER ‘[email protected]‘ [IDENTIFIED BY ‘PASSWORD‘] 其中密碼是可選項;
說明:該方法建立出來的使用者只有串連資料庫的許可權,需要後續繼續授權;
注意:使用者與@後主機地址是一體的,用一個分號串連,否則會報錯.
ERROR 1396 (HY000): Operation CREATE USER failed for ‘remote‘@‘%‘
使用例子:CREATE USER ‘[email protected]‘ IDENTIFIED BY "123";

grant all privileges on lxk.* to ‘lxk‘@‘localhost‘;
給主機為localhost的使用者lxk分配可對資料庫lxk所有表進行所有操作的許可權,並設定口令為123。

完成使用者的建立後,請記得重新整理系統許可權表;
flush privileges;

以下是我參考的連結:
MySQL建立使用者的三種方法
http://blog.csdn.net/huaishu/article/details/50540814
MYSQL問題解決方案:Access denied for user ‘root‘@‘localhost‘ (using password:YES)
http://blog.csdn.net/skywalker_leo/article/details/47274441




改密碼的方法,在我的MySQL分類的文章的MySQL安裝篇有介紹。

補充,MySQL資料庫的資料類型。
如下,具體詳細的,看我的jdbc篇的,jdbc全部概念篇,裡面講了MySQL資料庫的各種資料類型的詳解。
MySQL有三大類資料類型, 分別為數字、日期\時間、字串, 這三大類中又更細緻的劃分了許多子類型:

數字類型
整數:
tinyint、smallint、mediumint、int、bigint
浮點數: float、double、real、decimal
日期和時間: date、time、datetime、timestamp、year
字串類型
字串:
char、varchar
文本: tinytext、text、mediumtext、longtext
二進位(可用來儲存圖片、音樂等): tinyblob、blob、mediumblob、longblob



命令列下操作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.