MySQL資料庫補充

2021-10-24 07:16:21 字數 2166 閱讀 2622

檢視表結構

desc 表名;
表處理

# 修改欄位名以及資料型別和約束

alter table 表名 change 原欄位名 新欄位名 資料型別 約束;

# 修改欄位的資料型別和約束

alter table 表名 modify 欄位名 資料型別 約束;

# 增加字段

alter table 表名 add 欄位名 資料型別 約束;

# 刪除字段

alter table 表名 drop 欄位名;

alter table 表名 drop column 欄位名;

# 修改表名

alter table 原表名 rename to 新錶名;

# 複製表結構

desc 表名; #檢視表結構

create table 新錶名 like 原表名;

# 複製表結構和內容

create table 新錶名 select * from 原表名;

# 找出單價小於10塊錢的書籍,並且插入信標

create table 新錶名 select * from 原表名 where 條件語句(單價<10);

# 複製表指定字段,但是不帶資料

create table 新錶名 select 欄位名1,欄位名2,欄位名3 from 原表名 where 不成立的條件語句 (如 0=1)

字段約束與表記約束

表結構

資料庫由多張表組成

create table 表名(

欄位名1 資料型別 字段約束,

欄位名2 資料型別 字段約束

);分類

————字段約束

————表級約束

# 建立表————字段約束

create table userinfo(

id int primary key, # 設定主鍵

username varchar(20) not null,

gender varchar(3) check(gender='男'organder='女'), # mysql中check不生效

phone char(11) unique, # 設定唯一

age int default 18,

address int references address(id) # 外來鍵 );

create table address(

id int primary key,

pname varchar(50) not null

);# 建立表————表級約束

create table newuserinfo(

id int, # 設定主鍵

username varchar(20),

gender varchar(3),

phone char(11), # 設定唯一

age int,

address int # 外來鍵

constraint pkey primary key(id),

constraint uqkey unique(phton),

constraint ck check(gender='男' or gender='女'),# mysql中check不生效

constraint fk foreign key(address) references address(id) );

# 通用格式

create table userinfo(

id int primary key, # 設定主鍵

username varchar(20) not null,

gender varchar(3),

phone char(11) unique, # 設定唯一

age int default 18,

address int,

# 外來鍵寫在表級約束裡面

constraint fk foreign key(address) references address(id)# 外來鍵

);

檢視建表語句

show create table 表名;
檢視約束

show index from 表名;

資料庫 Mysql內容補充二

多表查詢 mysql支援的是sql99標準的連線查詢,並不支援oracle公司的外連線查詢,但是支援oracle等值查詢,不等值查詢,自連線查詢,子查詢 只要不是外連線 都支援 oracle也支援sql99標準的連線查詢 內連線 等值查詢 select e.empno,e.ename,e.sal,d...

MySQL資料庫基礎補充 編碼問題

mysql 編碼問題 mysql級別編碼 修改位置 etc mysql mysql.conf.d mysqld.cnf client default character set utf8 mysqld character set server utf8 collation server utf8 g...

zabbix的Mysql資料庫清理 補充

1 背景 zabbix部署一年了,在web介面上定了資料只存乙個月,但是一年後,還是資料太多,掛了,按照網上的教程,清除了歷史庫的資訊,編寫定時指令碼,每週清理一次,乙個月後,又down了 down還不是一次死透,一會好了一會兒壞,直到兩天左右徹底死掉。什麼鬼?斷斷續續的死,不穩定 最大的表在8百萬...