程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Mysql中對表字段的增刪改查

Mysql中對表字段的增刪改查

編輯:MySQL綜合教程

Mysql中對表字段的增刪改查   mysql中對表字段的增加,修改(修改類型),刪除,排序  

1.建立一個new 表,表中字段:學號,姓名,年齡。

mysql> create table new (ne_id int(10),ne_name varchar(20),ne_age int(12));

Query OK, 0 rows affected (0.97 sec)

2.表的結構圖

mysql> desc new;

+---------+-------------+------+-----+---------+-------+

| Field   | Type        | Null | Key | Default | Extra |

+---------+-------------+------+-----+---------+-------+

| ne_id   | int(10)     | YES  |     | NULL    |       |

| ne_name | varchar(20) | YES  |     | NULL    |       |

| ne_age  | int(12)     | YES  |     | NULL    |       |

+---------+-------------+------+-----+---------+-------+

 rows in set (0.01 sec)

3.向new 表中添加一個地址

mysql> alter table new add ne_address varchar(20);

Query OK, 0 rows affected (1.28 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new;

+------------+-------------+------+-----+---------+-------+

| Field      | Type        | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| ne_id      | int(10)     | YES  |     | NULL    |       |

| ne_name    | varchar(20) | YES  |     | NULL    |       |

| ne_age     | int(12)     | YES  |     | NULL    |       |

| ne_address | varchar(20) | YES  |     | NULL    |       |

+------------+-------------+------+-----+---------+-------+

rows in set (0.00 sec)

4.將字段名ne_id 改名為 ne_nor

mysql> alter table new change ne_id ne_nor int(10);

Query OK, 0 rows affected (0.22 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new;

+------------+-------------+------+-----+---------+-------+

| Field      | Type        | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| ne_nor     | int(10)     | YES  |     | NULL    |       |

| ne_name    | varchar(20) | YES  |     | NULL    |       |

| ne_age     | int(12)     | YES  |     | NULL    |       |

| ne_address | varchar(20) | YES  |     | NULL    |       |

+------------+-------------+------+-----+---------+-------+

4 rows in set (0.00 sec)

5.刪除字段ne_nor 

mysql> alter table new drop ne_nor;

Query OK, 0 rows affected (1.04 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new;

+------------+-------------+------+-----+---------+-------+

| Field      | Type        | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| ne_name    | varchar(20) | YES  |     | NULL    |       |

| ne_age     | int(12)     | YES  |     | NULL    |       |

| ne_address | varchar(20) | YES  |     | NULL    |       |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

6.修改字段類型:將ne_age 的 int 改為 varchar

mysql> alter table new modify ne_age varchar(20);

Query OK, 0 rows affected (1.34 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new;

+------------+-------------+------+-----+---------+-------+

| Field      | Type        | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| ne_name    | varchar(20) | YES  |     | NULL    |       |

| ne_age     | varchar(20) | YES  |     | NULL    |       |

| ne_address | varchar(20) | YES  |     | NULL    |       |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.01 sec)

7.排序

mysql> alter table new modify ne_age varchar(20) after ne_address;

Query OK, 0 rows affected (0.98 sec)

Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new;

+------------+-------------+------+-----+---------+-------+

| Field      | Type        | Null | Key | Default | Extra |

+------------+-------------+------+-----+---------+-------+

| ne_name    | varchar(20) | YES  |     | NULL    |       |

| ne_address | varchar(20) | YES  |     | NULL    |       |

| ne_age     | varchar(20) | YES  |     | NULL    |       |

+------------+-------------+------+-----+---------+-------+

3 rows in set (0.00 sec)

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved