mysql數據表的基本操做:表結構操做,字段操做

本節介紹:

 

表結構操做

  • 建立數據表、
  • 查看數據表和查看字段、
  • 修改數據表結構
  • 刪除數據表

字段操做

  • 新增字段、
  • 修改字段數據類型、位置或屬性、
  • 重命名字段
  • 刪除字段

 

 

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


表結構操做

 

建立數據表:

  • 語法  :

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來指定後續操做基於哪一個數據庫 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。

 

 

 

查看數據表  :

查看數據表能夠查看已有數據表、數據表的字段信息sql

  • 語法  :
-- 查看全部表
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;

image

image

Desc/describe /show columns from 表名;url

 

image

 

 

 

 

修改數據表結構  :

修改表只能修改表名和表選項。spa

  • 語法  :
-- 修改表名:
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;
  • 補充說明  :
    • 刪除不可恢復,刪除需謹慎。


字段操做  :

 

 

新增字段  :

新增字段是在表存在的基礎上新增字段.net

  • 語法  :
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 字段名 :表明在某個字段後面;
    • 列屬性:主鍵,空值 等;

 

 

修改字段  :

修改字段通常都是修改字段數據類型或者字段屬性code

  • 語法  :
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;
  • 補充說明  :
    • 字段名和數據類型是必填的,屬性和位置是選填的。
    • 若是字段自己帶着屬性的,那麼必須帶上原來的,不然會被去除掉;若是須要在原有屬性的基礎上添加新的屬性,則在填入時,那麼在帶上原有屬性的基礎上加上新的屬性便可
    • image

 

 

 

 

重命名字段  :

  • 語法  :
Alter table 表名 change 舊字段 新字段 數據類型 [屬性] [位置];
  • 使用示例  :
alter table demo change class room varchar(10); Alter table my_student change sex gender varchar(10);
  • 補充說明  :
    • 數據類型是必填的,但能夠是新的【重名字段能夠順便改數據類型】
    • 更名的同時也能修改字段的數據類型,屬性和位置。【若是字段帶有屬性,重命名字段時能夠不帶上】image

 

 

刪除字段  :

  • 語法  :
Alter table 表名 drop 字段名;
  • 使用示例  :
Alter table my_student drop age; alter table demo drop room;
  • 補充說明  :
    • 刪除需謹慎,刪除字段表明着將該字段下的全部數據都將刪除。
相關文章
相關標籤/搜索