mysql資料表的基本操作:表結構操作,欄位操作

來源:互聯網
上載者:User

標籤:資料庫   table   mbr   wp8   play   got   ott   char   jna   

本節介紹:

 

表結構操作
  • 建立資料表、
  • 查看資料表和查看欄位、
  • 修改資料表結構
  • 刪除資料表
欄位操作
  • 新增欄位、
  • 修改欄位資料類型、位置或屬性、
  • 重新命名欄位
  • 刪除欄位

 

 

首發時間:2018-02-18  21:31

表結構操作

  建立資料表:
  • 文法  :
create table [if not exists] 表名(欄位名字 資料類型,欄位名字 資料類型)[表選項];
  • 表選項  :

字元集:charset表中儲存資料的字元集

校對集:colloate表中用來校對資料的校對集

儲存引擎  :engine儲存資料的儲存引擎

  • 使用樣本  :
-- 建表之前必須指定資料庫,使用use ,或者顯式指定create table if not exists mydatabase.student(name varchar(20),sex varchar(20),number varchar(20),age int)charset utf8;-- use mydatabase;create table if not exists class(name varchar(20),room varchar(20))charset utf8;------------use mydatabase;create table if not exists class(name varchar(20),room varchar(20));
  • 補充說明  :
    • if not exists 是先檢查是否存在同名的表,如果存在,則不執行後面的建立語句。
    • 如果沒有指定表選項,將使用預設的,比如mysql預設的儲存引擎是innodb

 

 

 

查看資料表  :

查看資料表可以查看已有資料表、資料表的欄位資訊

  • 文法  :
-- 查看所有表show tables;-- 查看部分表show tables like ‘模糊比對‘;-- 查看錶的建立語句show create table 資料表名;-- 旋轉查看結構show create table 資料表名\G;-- 查看錶結構:查看錶中的欄位資訊:Desc/desc 表名;describe 表名;show columns from 表名;
  • 萬用字元:

_匹配單個字元

%匹配多個字元

  • 使用樣本  :
show tables;show tables like ‘my%‘;show create table student;show create table student\G;desc student;describe student;show columns from student;

圖例:

  1. show create table student;跟show create table sudent\G;

Desc/describe /show columns from 表名;

 

 

 

 

 

修改資料表結構  :

修改表本身只能修改表名和表選項。

  • 文法  :
-- 修改表名:rename table 老表名 to 新表名;--修改表選項:Alter table 表名 表選項 [=] 值;
  • 使用樣本  :
rename table student to my_student;rename table class to my_class;-- Alter table my_student charset gbk;Alter table my_collation_bin collate =utf8_bin;

 

 

 

 

 

刪除資料表  :
  • 文法  :
Drop table 表名1,表名2...;
  • 使用樣本  :
drop table demo;drop table demodata;
  • 補充說明  :
    • 刪除不可恢複

欄位操作  :  新增欄位  :

新增欄位是在表存在的基礎上新增欄位

  • 文法  :
Alter table 表名 add [column] 欄位名 資料類型 [列屬性] [位置];
  • 使用樣本  :
Alter table 表名 add [column] 欄位名 資料類型 [列屬性] [位置];Alter table demo add column id int first;Alter table demo add id int;Alter table demo add  class int after age;Alter table demo add  number int not null after age;
  • 補充說明  :
    • 位置常用文法:first 欄位名,after 欄位名 ;
    • 列屬性:主鍵,空 等;

 

 

修改欄位  :

修改欄位一般都是修改欄位資料類型或者欄位屬性

  • 文法  :
Alter table 表名 modify 欄位名 資料類型 [屬性] [位置];
  • 使用樣本  :
Alter table my_student modify number char(10) after id;Alter table demo modify  number int null ;--alter table student modify name varchar(20) not null;--alter table student modify name varchar(20) not null primary key;
  • 補充說明  :
    • 欄位名和資料類型是必填的,屬性和位置是選填的。
    • 如果欄位本身帶著屬性的,那麼必須帶上原來的,否則會被去除掉;如果需要在原有屬性的基礎上添加新的屬性,則在填入時,那麼在帶上原有屬性的基礎上加上新的屬性即可

 

 

 

 

重新命名欄位  :
  • 文法  :
Alter table 表名 change 舊欄位 新欄位 資料類型 [屬性] [位置];
  • 使用樣本  :
alter table demo change class room varchar(10);Alter table my_student change sex gender varchar(10);
  • 補充說明  :
    • 資料類型是必填的,但可以是新的【重名欄位可以順便改資料類型】
    • 改名的同時也能修改欄位的資料類型,屬性和位置。【如果欄位帶有屬性,重新命名欄位時可以不帶上】

 

 

刪除欄位  :
  • 文法  :
Alter table 表名 drop 欄位名;
  • 使用樣本  :
Alter table my_student drop age;alter table demo drop room;
  • 補充說明  :
    • 刪除需謹慎,刪除欄位代表著將該欄位下的所有資料都將刪除。

 

 

 

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.