一 庫相關操做: 0建立數據庫: 語法(help create database) CREATE DATABASE 數據庫名 charset utf8; 1 查看數據庫 show databases; show create database db1; select database(); 2 選擇數據庫 USE 數據庫名 3 刪除數據庫 DROP DATABASE 數據庫名; 4 修改數據庫 alter database db1 charset utf8; 二 表相關操做 建立表: #語法: create table 表名( 字段名1 類型[(寬度) 約束條件], 字段名2 類型[(寬度) 約束條件], 字段名3 類型[(寬度) 約束條件] ); #注意: 1. 在同一張表中,字段名是不能相同 2. 寬度和約束條件可選 3. 字段名和類型是必須的 例子一: 1 建立表 create table t1(id int,name varchar(50),sex enum('male','female'),age int(3)); 表中插入數據 insert into t1 values(1,'egon','male',18),(2,'alex','female',81); 順序要和表結構一致 查看錶結構
1 mysql> describe t1; 2 +-------+-----------------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +-------+-----------------------+------+-----+---------+-------+ 5 | id | int(11) | YES | | NULL | | 6 | name | varchar(50) | YES | | NULL | | 7 | sex | enum('male','female') | YES | | NULL | | 8 | age | int(3) | YES | | NULL | | 9 +-------+-----------------------+------+-----+---------+-------+10 4 rows in set (0.00 sec)
show create table t1\G; #查看錶詳細結構,可加\G 2 修改表ALTER TABLE ALTER TABLE 表名 RENAME 新表名; 例子: alter table t1 rename t2; 3 增長表字段:(不能和原來的重複)
1 mysql> alter table student10 add name varchar(20) not null,add age int(3) not null default 22; 2 Query OK, 0 rows affected (1.06 sec) 3 Records: 0 Duplicates: 0 Warnings: 0 4 5 mysql> describe student10; 6 +-------+-------------+------+-----+---------+-------+ 7 | Field | Type | Null | Key | Default | Extra | 8 +-------+-------------+------+-----+---------+-------+ 9 | id | int(11) | YES | | NULL | |10 | name | varchar(20) | NO | | NULL | |11 | age | int(3) | NO | | 22 | |12 +-------+-------------+------+-----+---------+-------+13 3 rows in set (0.00 sec)
4 在name字段以後添加一個class字段
1 mysql> alter table student10 add class varchar(10) not null after name; 2 Query OK, 0 rows affected (1.00 sec) 3 Records: 0 Duplicates: 0 Warnings: 0 4 5 mysql> describe student10; 6 +-------+-------------+------+-----+---------+-------+ 7 | Field | Type | Null | Key | Default | Extra | 8 +-------+-------------+------+-----+---------+-------+ 9 | id | int(11) | YES | | NULL | |10 | name | varchar(20) | NO | | NULL | |11 | class | varchar(10) | NO | | NULL | |12 | age | int(3) | NO | | 22 | |13 +-------+-------------+------+-----+---------+-------+14 4 rows in set (0.00 sec)
5 添加到字段最前面
1 mysql> alter table student10 add sex enum('male','female') default 'male' first; 2 Query OK, 0 rows affected (1.05 sec) 3 Records: 0 Duplicates: 0 Warnings: 0 4 5 mysql> describe student10; 6 +-------+-----------------------+------+-----+---------+-------+ 7 | Field | Type | Null | Key | Default | Extra | 8 +-------+-----------------------+------+-----+---------+-------+ 9 | sex | enum('male','female') | YES | | male | |10 | id | int(11) | YES | | NULL | |11 | name | varchar(20) | NO | | NULL | |12 | class | varchar(10) | NO | | NULL | |13 | age | int(3) | NO | | 22 | |14 +-------+-----------------------+------+-----+---------+-------+15 5 rows in set (0.00 sec)
6 刪除字段 mysql> alter table student10 drop sex; Query OK, 0 rows affected (0.97 sec) Records: 0 Duplicates: 0 Warnings: 0 7 修改字段 modify 例子一:
1 mysql> alter table student10 modify age int(5); 2 Query OK, 0 rows affected (1.21 sec) 3 Records: 0 Duplicates: 0 Warnings: 0 4 5 mysql> describe student10; 6 +-------+-------------+------+-----+---------+-------+ 7 | Field | Type | Null | Key | Default | Extra | 8 +-------+-------------+------+-----+---------+-------+ 9 | id | int(11) | YES | | NULL | |10 | name | varchar(20) | NO | | NULL | |11 | class | varchar(10) | NO | | NULL | |12 | age | int(5) | YES | | NULL | |13 +-------+-------------+------+-----+---------+-------+14 4 rows in set (0.00 sec)
例子二:
mysql> alter table student10 modify id int(11) not null primary key auto_increment; Query OK, 0 rows affected (1.22 sec) Records: 0 Duplicates: 0 Warnings: 0mysql> describe student10;+-------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-------+-------------+------+-----+---------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | varchar(20) | NO | | NULL | || class | varchar(10) | NO | | NULL | || age | int(5) | YES | | NULL | |+-------+-------------+------+-----+---------+----------------+4 rows in set (0.01 sec)
8. 對已經存在的表增長複合主鍵 mysql> alter table service2 -> add primary key(host_ip,port); 9. 增長主鍵 mysql> alter table student1 -> modify name varchar(10) not null primary key; 10. 增長主鍵和自動增加 mysql> alter table student1 -> modify id int not null primary key auto_increment; 11. 刪除主鍵 a. 刪除自增約束 mysql> alter table student10 modify id int(11) not null; b. 刪除主鍵 mysql> alter table student10 -> drop primary key; 12 複製表 複製表結構+記錄 (key不會複製: 主鍵、外鍵和索引
mysql> create table new_hu select * from department; Query OK, 2 rows affected (0.77 sec) Records: 2 Duplicates: 0 Warnings: 0mysql> select * from new_hu;+-------+-----------------------------------+| id | name |+-------+-----------------------------------+| 1 | 歐德博愛技術有限事業部 || 22222 | 艾利克斯人力資源部 |+-------+-----------------------------------+2 rows in set (0.00 sec) 只複製表結構:用like mysql> create table t10 like department; Query OK, 0 rows affected (0.66 sec) mysql> select * from t10; Empty set (0.01 sec)
13 刪除表 DROP TABLE 表名;