標籤:cte 儲存引擎 表結構 character des rds after employee 亂碼
文法:create table 表名(欄位名1 類型[(寬度) 約束條件],欄位名2 類型[(寬度) 約束條件],欄位名3 類型[(寬度) 約束條件]);注意:1. 在同一張表中,欄位名是不能相同2. 寬度和約束條件可選3. 欄位名和類型是必須的
#為表添加欄位create database db1 charset utf8;use db1;create table t1( -> id int, -> name varchar(50), -> sex enum(‘male‘,‘female‘), -> age int(3) -> );show tables; #查看db1庫下所有表名
亂碼問題
mysql> create database db1 charset latin1;mysql> use db1;mysql> create table t1(name varchar(20));mysql> show create table t1; #查看錶,發現表預設與資料db1的字元編碼一致mysql> insert into t1 values(‘宋‘); #插入中文出錯,因為latin1不支援中文ERROR 1366 (HY000): mysql> #解決方案一:刪除庫db1,重建db1,字元編碼指定為utf8#解決方案二:修改mysql> alter table t1 charset utf8; #修改表t1的編碼mysql> insert into t1 values(‘宋‘); #雖然t1的編碼改了,但是t1的欄位name仍然是按照latin1編碼建立的ERROR 1366 (HY000): mysql> alter table t1 modify name varchar(20); #需要重新定義下欄位namemysql> insert into t1 values(‘宋‘);mysql> select * from t1;#不要忘記將資料庫編碼也改成utf8,這樣以後在該資料庫下建立表時,都預設utf8編碼了
永久解決編碼問題
#1. 修改設定檔[mysqld]default-character-set=utf8 [client]default-character-set=utf8 [mysql]default-character-set=utf8#mysql5.5以上:修改方式有所改動 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set=utf8 [mysql] default-character-set=utf8#2. 重啟服務#3. 查看修改結果:\sshow variables like ‘%char%‘
查看錶結構
describe 表名;show create table 表名\G;#查看詳細表結構
修改表
文法:1. 修改表名 ALTER TABLE 表名 RENAME 新表名;2. 增加欄位 ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…], ADD 欄位名 資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…] FIRST; ALTER TABLE 表名 ADD 欄位名 資料類型 [完整性條件約束條件…] AFTER 欄位名; 3. 刪除欄位 ALTER TABLE 表名 DROP 欄位名;4. 修改欄位 ALTER TABLE 表名 MODIFY 欄位名 資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 舊資料類型 [完整性條件約束條件…]; ALTER TABLE 表名 CHANGE 舊欄位名 新欄位名 新資料類型 [完整性條件約束條件…];樣本:1. 修改儲存引擎mysql> alter table service -> engine=innodb;2. 添加欄位mysql> alter table student10 -> add name varchar(20) not null, -> add age int(3) not null default 22; mysql> alter table student10 -> add stu_num varchar(10) not null after name; //添加name欄位之後mysql> alter table student10 -> add sex enum(‘male‘,‘female‘) default ‘male‘ first; //添加到最前面3. 刪除欄位mysql> alter table student10 -> drop sex;mysql> alter table service -> drop mac;4. 修改欄位類型modifymysql> alter table student10 -> modify age int(3);mysql> alter table student10 -> modify id int(11) not null primary key auto_increment; //修改為主鍵5. 增加約束(針對已有的主鍵增加auto_increment)mysql> alter table student10 modify id int(11) not null primary key auto_increment;ERROR 1068 (42000): Multiple primary key definedmysql> alter table student10 modify id int(11) not null auto_increment;Query OK, 0 rows affected (0.01 sec)Records: 0 Duplicates: 0 Warnings: 06. 對已經存在的表增加複合主鍵mysql> alter table service2 -> add primary key(host_ip,port); 7. 增加主鍵mysql> alter table student1 -> modify name varchar(10) not null primary key;8. 增加主鍵和自動成長mysql> alter table student1 -> modify id int not null primary key auto_increment;9. 刪除主鍵a. 刪除自增約束mysql> alter table student10 modify id int(11) not null; b. 刪除主鍵mysql> alter table student10 -> drop primary key;
複製表
複製表結構+記錄 (key不會複製: 主鍵、外鍵和索引)mysql> create table new_service select * from service;只複製表結構mysql> select * from service where 1=2; //條件為假,查不到任何記錄Empty set (0.00 sec)mysql> create table new1_service select * from service where 1=2; Query OK, 0 rows affected (0.00 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> create table t4 like employees;
刪除表
DROP TABLE 表名;
mysql資料庫之表的操作