MySQL(5):表的crud

來源:互聯網
上載者:User

標籤:

表名的首碼:

兩個student表,為了區分相同邏輯表名的不同應用,給邏輯表名,增加首碼

//學生管理

create table info_student (

name varchar(20),

stu_no varchar(20)

);

//線上考試

create table exam_student (

name varchar(20),

stu_no varchar(20),

score int

);

1.有哪些表?

show tables;

 

2.

show tables [like ‘pattern‘];

其中like pattern 部分,表示只獲得那種規則的表名

show tables like ‘exam_%‘;//-----%表示任一字元的組合----稱之為萬用字元

 

3.某一個表的建立資訊

show create table exam_student;

 

也可以使用:

show create table exam_student\G (資料很多時候,這樣顯示容易看懂)

 

4.查看錶的結構(描述表的結構)

describe exam_student;

 

也可以簡寫成如下:

desc info_student;

資料庫對應著目錄,顯然資料庫內容對應的就應該是目錄的內容,檔案。

我們追蹤到磁碟上面的資料庫檔案如下:

 

這裡的frm檔案:儲存的是列表結構

 

5.刪除表格

drop table tbl_name;

 

如果表不存在,執行刪除操作就會報錯,有時候也可以這樣操作可以避免這種報錯:

drop table if exists democlass;(同時也適用drop database if exists db_name)

 

 

6.修改表

(1)修改表名

重新命名:rename table old_tbl_name to new_tbl_name;

同時給多個表rename:

rename table info_student to infos, exam_class to classes;

上面我們可以先show tables 擷取所有的tables,然後批量修改rename table info_student to infos, exam_class to classes,……;

 

支援跨資料庫重新命名:

從原來資料庫php_one中的infos列表 存放到 demo資料庫中的infos列表

我們進去看看demo資料中的內容:

 

上面支援跨資料庫重新命名,可以實現資料庫重新命名。

重新建立一個新的資料庫,舊的資料庫的表,都rename到新到資料庫內,刪除舊的資料庫;

 

 

(2) 修改列定義

修改表結構,上面的子命令,上級是,alter table tbl_name  [add|drop|change|modify]

add (增加一個新的列):

modify(修改一個列的定義):修改列的類型 和屬性

drop(刪除一個列):

change(重新命名一個列):提供重新命名的服務

 

 

 

(3)修改表選項

alter table  tbl_name   新的表選項;

 

MySQL(5):表的crud

聯繫我們

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