MySQL動作陳述式

來源:互聯網
上載者:User

標籤:null   pass   where   table   val   資料類型   database   address   個數   

登入到MySQL

mysql -h主機名稱 -u使用者名稱 -p

 

退出MySQL

exit; 

quit;

 

建立資料庫

create database 資料庫名 [其他選項];

eg: create database article character set utf8; //建立一個名為article的資料庫,並為他設定utf-8編碼

 

選擇所要操作的資料庫 1.登入資料庫時指定
mysql -D資料庫 -h主機名稱 -u使用者名稱 -p

2.登入後指定

use 資料庫名;

 

建立資料表

create table 表名稱(列聲明);

eg:建立 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 "-"
  );

 

  對於一些較長的語句在命令提示字元下可能容易輸錯, 因此我們可以通過任何文字編輯器將語句輸入好後儲存為 createtable.sql 的檔案中, 通過命令提示字元下的檔案重新導向執行執行該指令碼。


  開啟命令提示字元, 輸入: mysql -D samp_db -u root -p < createtable.sql


  (提示: 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將自動索引該列。
 

向表中插入資料

insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);

eg:insert into students values(NULL, "王剛", "男", 20, "13811371377"); //省略列名,直接按順序插入所有值
  insert into students (name, sex, age) values("孫麗華", "女", 21); //插入部分值,不能省略列明

查詢表中資料

select 列名稱 from 表名稱 [查詢條件];
eg:select name, age from students;//查詢表中所有學生的姓名和年齡
  select*from students;//查詢表中所有欄位

  where 子句不僅僅支援 "where 列名 = 值" 這種名等於值的查詢形式, 對一般的比較運算的運算子都是支援的, 例如 =、>、<、>=、<、!= 以及一些擴充運算子 is [not] null、in、like 等等。 還可以對查詢條件使用 or 和 and 進行組合查詢。

  樣本:

  查詢年齡在21歲以上的所有人資訊: select * from students where age > 21;

  查詢名字中帶有 "王" 字的所有人資訊: select * from students where name like "%王%";

  查詢id小於5且年齡大於20的所有人資訊: select * from students where id<5 and age>20;

更新表中資料

update 表名稱 set 列名稱=新值 where 更新條件;

eg: 將id為5的手機號改為預設的"-": update students set tel=default where id=5;

  將所有人的年齡增加1: update students set age=age+1;

  將手機號為 13288097888 的姓名改為 "張偉鵬", 年齡改為 19: update students set name="張偉鵬", age=19 where tel="13288097888";

刪除表中的資料

delete from 表名稱 where 刪除條件;


eg:刪除id為2的行: delete from students where id=2;

   刪除所有年齡小於21歲的資料: delete from students where age<20;

   刪除表中的所有資料: delete from students;

 

建立後表的修改
添加列    alter table 表名 add 列名 列資料類型 [after 插入位置];
eg:在表的最後追加列 address: alter table students add address char(60);    在名為 age 的列後插入列 birthday: alter table students add birthday date after age;

修改列
  
alter table 表名 change 列名稱 列新名稱 新資料類型;

  eg:將表 tel 列改名為 telphone: alter table students change tel telphone char(13) default "-";
    將 name 列的資料類型改為 char(16): alter table students change name name char(16) not null;

刪除列
  
alter table 表名 drop 列名稱;

  eg:刪除 birthday 列: alter table students drop birthday;

  重新命名表

  alter table 表名 rename 新表名;

  eg:重新命名 students 表為 workmates: alter table students rename workmates;

 刪除整張表

  drop table 表名;

  eg: 刪除 workmates 表: drop table workmates;

 

 

 刪除整個資料庫

  drop database 資料庫名;

  eg:刪除 samp_db 資料庫: drop database samp_db;

 

修改root使用者密碼

 

mysqladmin -uroot -p password 

 

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.