1. 程式人生 > >MySQL之表相關操作

MySQL之表相關操作

warning inf data employees duplicate 不同 建表 www img

一 存儲引擎介紹

存儲引擎即表類型,mysql根據不同的表類型會有不同的處理機制

詳見:http://www.cnblogs.com/linhaifeng/articles/7213670.html

二 表介紹

表相當於文件,表中的一條記錄就相當於文件的一行內容,不同的是,表中的一條記錄有對應的標題,稱為表的字段

技術分享圖片

id,name,qq,age稱為字段,其余的,一行內容稱為一條記錄

三 創建表

技術分享圖片
#語法:
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
);

#註意: 1. 在同一張表中,字段名是不能相同 2. 寬度和約束條件可選 3. 字段名和類型是必須的
技術分享圖片 技術分享圖片
MariaDB [(none)]> create database db1 charset utf8;

MariaDB [(none)]> use db1;

MariaDB [db1]> create table t1(
    -> id int,
    -> name varchar(50),
    -> sex enum(male,female),
    -> age int(3)
    
-> ); MariaDB [db1]> show tables; #查看db1庫下所有表名 MariaDB [db1]> desc t1; +-------+-----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-----------------------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | varchar(50) | YES | | NULL | | | sex | enum(
male,female) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+-----------------------+------+-----+---------+-------+ MariaDB [db1]> select id,name,sex,age from t1; Empty set (0.00 sec) MariaDB [db1]> select * from t1; Empty set (0.00 sec) MariaDB [db1]> select id,name from t1; Empty set (0.00 sec)
View Code 技術分享圖片
MariaDB [db1]> insert into t1 values
    -> (1,egon,18,male),
    -> (2,alex,81,female)
    -> ;
MariaDB [db1]> select * from t1;
+------+------+------+--------+
| id   | name | age  | sex    |
+------+------+------+--------+
|    1 | egon |   18 | male   |
|    2 | alex |   81 | female |
+------+------+------+--------+



MariaDB [db1]> insert into t1(id) values
    -> (3),
    -> (4);
MariaDB [db1]> select * from t1;
+------+------+------+--------+
| id   | name | age  | sex    |
+------+------+------+--------+
|    1 | egon |   18 | male   |
|    2 | alex |   81 | female |
|    3 | NULL | NULL | NULL   |
|    4 | NULL | NULL | NULL   |
+------+------+------+--------+
往表中插入數據

註意註意註意:表中的最後一個字段不要加逗號

四 查看表結構

技術分享圖片
MariaDB [db1]> describe t1; #查看表結構,可簡寫為desc 表名
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum(male,female) | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+


MariaDB [db1]> show create table t1\G; #查看表詳細結構,可加\G
技術分享圖片

五 數據類型

http://www.cnblogs.com/linhaifeng/articles/7233411.html

六 表完整性約束

http://www.cnblogs.com/linhaifeng/articles/7238814.html

七 修改表ALTER TABLE

技術分享圖片
語法:
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. 修改字段類型modify
mysql> 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 defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

6. 對已經存在的表增加復合主鍵
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: 0

mysql> create table t4 like employees;
技術分享圖片

九 刪除表

DROP TABLE 表名;

MySQL之表相關操作