MySQL整理4 資料表的基本操作2

2022-08-22 01:09:14 字數 1733 閱讀 9569

二、檢視資料表結構

檢視資料表結構可用:describe和show create table tablename語句

三、修改資料表

#修改表名字alter

alter table tb_e*** rename to tb_3;

#修改欄位的資料型別

alter table tb_e*** modify name varchar(30);

#修改列名和資料型別 alter table 表名 change 舊列名 新列名 資料型別

#change也可以只修改資料型別,舊新列名相同就可以

alter table tb_e*** change salary sal int ;

#新增字段

alter table tb_3 add managerid int(11);

#刪除一列

alter table tb_3 drop salary;

#修改列的順序

show tables;

desc tb_3;

alter table tb_3 modify name varchar(30) first;

desc tb_3;

alter table tb_3 modify name varchar(25) after id;

#更改表的儲存引擎

alter table tb_3 engine=innodb;

#刪除表的外來鍵約束

alter table tb_3 drop foreign key 《外鍵名》

四、刪除資料表

sql中的drop、delete、truncate都表示刪除,但是三者有一些差別

drop、delete與truncate分別在什麼場景之下使用?

delete和truncate刪除資料的區別?

1.刪除沒用被關聯的表

drop table if exists tb_emp1;
2.刪除被其他表關聯的主表(有外來鍵約束)

create table tb_dept1(

id int(11) primary key,

name varchar(22) not null,

location varchar(50)

);create table tb_emp5(

id int(11) primary key,

name varchar(25),

deptid int(11),

salary float,

constraint fk_empdept1 foreign key(deptid) references tb_dept1(id)

/*新增外來鍵約束 非空約束*/

);

此時直接刪除表tb_dept1並不能完成,sql語句會報錯。需要先刪除外來鍵約束

alter table tb_emp5 drop foreign key fk_empdept1;

drop table tb_dept1;

mysql資料表命令是 MySQL資料表操作命令

mysql語句 1 修改表名 rename table 舊表名 to 新錶名 2 修改字段型別 alter table 表名 modify column 欄位名 字段型別 長度 3 修改欄位名稱和型別 alter table 表名 change 現有欄位名稱 修改後欄位名稱 資料型別 4 增加字段 ...

4 資料表基本操作

普通建立表 create table table name field name field type field option 需先進入某資料庫,表預設建立在當前所在的資料庫 表的option有engine,charset,collate三種,預設engine為innodb 從已存在的表複製 只複...

mysql學習4 資料表

資料表是資料庫最重要的組成部分之一,是其他物件的基礎。1.使用資料庫 use db name 2.建立資料表 create table if not exists table name column name data type,3.檢視資料表 show tables from db name sh...