標籤:資料庫 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;
圖例:
- 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資料表的基本操作:表結構操作,欄位操作