在創建表是預設為加上數據引擎和字元集,如創建一個student表,代碼如下: 插入數據(insert和replace) 1.insert命令直接在表的插入一條記錄。 2.replace命令在插入記錄時判斷主鍵是否相同,相同就修改,否則插入一條新記錄。 用select 結果集創建一個表 修改表名 有兩 ...
在創建表是預設為加上數據引擎和字元集,如創建一個student表,代碼如下:
create table students(id int unsigned zerofill auto_increment primary key, name varchar(20) not null, --不允許欄位為null sex char(1) );
-----------------------------自動加上數據引擎和字元集------------------------
create table students(id int unsigned zerofill auto_increment primary key,
name varchar(20) not null,
sex char(1)
)engines=innodb default charset=utf8;
- 插入數據(insert和replace)
1.insert命令直接在表的插入一條記錄。
insert into students(sname,sex) values('orna','男'); insert into students(sname,sex) values('lisi','男'); insert into students(sname,sex) values('wangwu','男'); mysql> select * from students; +------------+--------+------+------+ | id | sname | sex | tid | +------------+--------+------+------+ | 0000000001 | orna | 男 | NULL | | 0000000002 | lisi | 男 | NULL | | 0000000003 | wangwu | 男 | NULL | +------------+--------+------+------+ 3 rows in set (0.00 sec)
2.replace命令在插入記錄時判斷主鍵是否相同,相同就修改,否則插入一條新記錄。
replace into students(id,sname,sex) values(2,'luscy','女'); replace into students(id,sname,sex) values(20,'zhangsan','男'); mysql> select * from students; +------------+----------+------+------+ | id | sname | sex | tid | +------------+----------+------+------+ | 0000000001 | orna | 男 | NULL | | 0000000002 | luscy | 女 | NULL | | 0000000003 | wangwu | 男 | NULL | | 0000000020 | zhangsan | 男 | NULL | +------------+----------+------+------+ 4 rows in set (0.00 sec)
- 用select 結果集創建一個表
mysql> select * from students; +------------+-------+------+ | id | name | sex | +------------+-------+------+ | 0000000001 | luscy | 女 | | 0000000002 | lisi | 男 | | 0000000003 | orna | 男 | +------------+-------+------+ 3 rows in set (0.00 sec) mysql> create table stu select * from students; Query OK, 3 rows affected (0.33 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from stu; +------------+-------+------+ | id | name | sex | +------------+-------+------+ | 0000000001 | luscy | 女 | | 0000000002 | lisi | 男 | | 0000000003 | orna | 男 | +------------+-------+------+ 3 rows in set (0.00 sec)
- 修改表名
有兩種方法可以修改表名。
mysql> rename table stu to st; Query OK, 0 rows affected (0.17 sec) mysql> alter table st rename to s; Query OK, 0 rows affected (0.23 sec)
- 刪除表
drop table 表名;
drop table 表1,表2,表3.............. 表示可以同時刪除多少個表。
- 修改表的列名稱
在修改欄位需要註意幾點:
- 修改欄位寬度只能改大,不能改小;
- 修改欄位類型時,要相容;
mysql> alter table s change name sname varchar(30); Query OK, 3 rows affected (1.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from s; +------------+-------+------+ | id | sname | sex | +------------+-------+------+ | 0000000001 | luscy | 女 | | 0000000002 | lisi | 男 | | 0000000003 | orna | 男 | +------------+-------+------+ 3 rows in set (0.01 sec)
- 限定欄位的取值範圍
create table students(id int unsigned zerofill auto_increment primary key, name varchar(20) not null, sex enum('男','女') --指定取值範圍。enum和set關鍵字同意 ); insert into students(name,sex) values('orna','男'); --插入數據成功 ------------------------------------插入數據時報錯--------------------------------------- mysql> insert into students(name,sex) values('orna','未知'); ERROR 1265 (01000): Data truncated for column 'sex' at row 1 mysql> select * from students; +------------+------+------+ | id | name | sex | +------------+------+------+ | 0000000001 | orna | 男 | +------------+------+------+ 1 row in set (0.00 sec)
- 主建、外建和唯一
一個表只能有一個主鍵,但可以用多個欄位建立一個組合主鍵。可以有多個外鍵和唯一約束。
- 增加主鍵
創建主鍵的兩種方法:
--指定主鍵名 alter table s add constraint pk primary key(id); --不指定主鍵名 alter table s add constraint primary key(id);
2.刪除主鍵
mysql> desc s; +-------+---------------------------+------+-----+------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+------------+-------+ | id | int(10) unsigned zerofill | NO | PRI | 0000000000 | | | sname | varchar(30) | YES | | NULL | | | sex | set('男','女') | YES | | NULL | | +-------+---------------------------+------+-----+------------+-------+ 3 rows in set (0.00 sec) mysql> alter table s drop primary key; Query OK, 3 rows affected (0.82 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> desc s; +-------+---------------------------+------+-----+------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+------------+-------+ | id | int(10) unsigned zerofill | NO | | 0000000000 | | | sname | varchar(30) | YES | | NULL | | | sex | set('男','女') | YES | | NULL | | +-------+---------------------------+------+-----+------------+-------+ 3 rows in set (0.00 sec)
3.增加欄位
在增加欄位時,可以用first和after關鍵字,但沒有before關鍵字。
mysql> alter table students add address varchar(50) not null after sex; Query OK, 0 rows affected (0.49 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc students; +---------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------------------+------+-----+---------+----------------+ | id | int(10) unsigned zerofill | NO | PRI | NULL | auto_increment | | name | varchar(20) | NO | | NULL | | | sex | set('男','女') | YES | | NULL | | | address | varchar(50) | NO | | NULL | | +---------+---------------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
4.刪除欄位
mysql> select * from s; +------------+-------+------+ | id | sname | sex | +------------+-------+------+ | 0000000001 | luscy | 女 | | 0000000002 | lisi | 男 | | 0000000003 | orna | 男 | +------------+-------+------+ 3 rows in set (0.00 sec) mysql> alter table s drop column sex; Query OK, 0 rows affected (0.63 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc s; +-------+---------------------------+------+-----+------------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+------------+-------+ | id | int(10) unsigned zerofill | NO | | 0000000000 | | | sname | varchar(30) | YES | | NULL | | +-------+---------------------------+------+-----+------------+-------+ 2 rows in set (0.00 sec)
2.增加唯一約束
--創建表時增加唯一約束。 create table students(id int unsigned zerofill auto_increment primary key, sname varchar(20) not null unique, sex set('男','女'), tid int unsigned )engine=innodb default charset=utf8; --創建表後,再增加唯一約束。 alter table students add constraint uk unique(sname); mysql> desc students; +-------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+---------+----------------+ | id | int(10) unsigned zerofill | NO | PRI | NULL | auto_increment | | sname | varchar(20) | NO | UNI | NULL | | | sex | set('男','女') | YES | | NULL | | | tid | int(10) unsigned | YES | | NULL | | +-------+---------------------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
--刪除唯一約束。
mysql> alter table students drop index uk;
Query OK, 0 rows affected (0.26 sec)
Records: 0 Duplicates: 0 Warnings: 0
3.增加外鍵約束
alter table students add constraint fk foreign key(tid) references teachers(id);
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete set null;表示刪除外鍵引用的記錄時把當前記錄的外鍵值修改成空。
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete cascade;表示刪除外鍵引用的記錄時把當前記錄同時刪除。
--alter table students add constraint fk foreign key(tid) references teachers(id) on delete set null on update cascade;表示刪除外鍵引用的記錄時把當前記錄的外鍵值修
--改成空,或者修改引用記錄同時修改外鍵引用欄位。 mysql> desc students; +-------+---------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------------+------+-----+---------+----------------+ | id | int(10) unsigned zerofill | NO | PRI | NULL | auto_increment | | sname | varchar(20) | NO | | NULL | | | sex | set('男','女') | YES | | NULL | | | tid | int(10) unsigned | YES | MUL | NULL | | +-------+---------------------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
4.刪除外鍵約束
alter table students drop foreign key fk; alter table students drop index fk;
5.創建索引
mysql> create index sname on students(sname desc); Query OK, 0 rows affected (0.27 sec) Records: 0 Duplicates: 0 Warnings: 0
--alter table students add index(sname desc);
6.刪除索引
alter table students drop index sname;
--drop index sname on students;