MySQL運算元據庫和表的基本語句(DDL)

2022-08-12 14:24:17 字數 1984 閱讀 6816

1、建立資料庫:

create database 資料庫名;

eg.create database test_ddl;12

2、建立表

create table 表名(

列名 資料型別 約束,

...);

eg. create table table_ddl(

id int(10) primary key auto_increment,

test_content varchar(20) not null);1

2345

6789

3、複製表

(1)複製表結構(不含資料)

create table 新錶名 (select * from 舊表名 where 1=2);

eg.create table copy_table_ddl(

select *

from table_ddl

where 1=2);12

3456

(2)複製表資料以及結構

create table 新錶名 (select * from 舊表名);

eg.create table copy_table_ddl2(

select *

from table_ddl );1

2345

64.修改表(alter)

(1)修改表名:(rename)

alter table 舊表名 rename 新錶名;

eg.alter table table_ddl rename table_ddl_rename; 12

3(2)修改列名(change)

alter table 表名 change 舊列名 新列名 原資料型別 原約束;

eg.alter table table_ddl_rename

change test_content content varchar(20) not null; 12

34(3)修改資料型別和約束(modify)

alter table 表名 modify 列名 新資料型別 新約束;

eg.alter table table_ddl_rename modify content varchar(100) null;12

3(4)刪除列(drop)

alter table 表名 drop 列名;

eg.alter table table_ddl_rename drop column new_columm;12

3(5)新增列(add)

alter table 表名 add column 列名 資料型別 約束;

eg.alter table table_ddl_rename add column new_columm int(20) unique; 12

3(6)刪除主鍵、唯一鍵約束(先刪除索引,再刪除約束)

#因為主鍵只有乙個,所以可以直接刪除,不用索引:

刪除主鍵:

alter table 表名 drop primary key;

eg.alter table copy_table_ddl drop primary key;

刪除唯一鍵約束索引:

alter table 表名 drop index 索引名;

eg.alter table table_ddl_rename drop index new_columm; 12

3456

7895.其他語句:

desc 表名; #查詢表結構

show index from 表名; #查詢表索引12

6.約束條件:

(1)主鍵:primary key

(2)唯一鍵:unique

(3)非空:not null

(4)自增:auto_increment

(5)預設值:default

(6)外來鍵:foregin key 12

3456

。。。---------------------

MySQL運算元據庫表

1,建立表的時候寫注釋 create table if not exists demo dictionary id varchar 32 not null comment 主鍵id primary key,dictionary name varchar 100 not null comment 名稱...

MySQL運算元據庫和表的基本語句(DDL)

1 建立資料庫 create database 資料庫名 eg.create database test ddl 2 建立表 create table 表名 列名 資料型別 約束,eg.create table table ddl id int 10 primary key auto increme...

MySQL運算元據庫和表的基本語句(DDL)

范蠡 資深開發工程師 我看鴻蒙作業系統 大型 後台穩定性技術策略 技術境界的二三四 mysql單錶資料不要超過500萬行 是經驗數值,還是 鐵律?基於支付場景下的微服務改造與效能優化 1 建立資料庫 create database 資料庫名 eg.create database test ddl 2...