<>1、MySQL命令行创建一个订单表
mysql> create table order_table( -> order_id int not null, -> price float(10,2)
, -> user_id int);
创建成功,效果如下:
mysql> desc order_table; +----------+-------------+------+-----+---------+----
---+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+-
-----+-----+---------+-------+ | order_id | int(11) | NO | | NULL | | | price |
float(10,2) | YES | | NULL | | | user_id | int(11) | YES | | NULL | | +---------
-+-------------+------+-----+---------+-------+ 3 rows in set
<>2、为已建好的表中 order_id 添加主键
mysql> alter table order_table add primary key(order_id);
添加成功,效果如下:
mysql> desc order_table; +----------+-------------+------+-----+---------+-----
--+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+--
----+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | price
| float(10,2) | YES | | NULL | | | user_id | int(11) | YES | | NULL | | +-------
---+-------------+------+-----+---------+-------+ 3 rows in set
<>3、为已建好的表中 order_id 添加自增
mysql> alter table order_table modify order_id int auto_increment;
添加成功,效果如下:
mysql> desc order_table; +----------+-------------+------+-----+---------+-----
-----------+ | Field | Type | Null | Key | Default | Extra | +----------+-------
------+------+-----+---------+----------------+ | order_id | int(11) | NO | PRI
| NULL | auto_increment | | price | float(10,2) | YES | | NULL | | | user_id |
int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+----
------------+ 3 rows in set
<>4、为已建好的表中 price 添加默认值:
mysql> alter table order_table modify price float(10,2) default '1234';
添加成功,效果如下:
mysql> desc order_table; +----------+-------------+------+-----+---------+-----
-----------+ | Field | Type | Null | Key | Default | Extra | +----------+-------
------+------+-----+---------+----------------+ | order_id | int(11) | NO | PRI
| NULL | auto_increment | | price | float(10,2) | YES | | 1234.00 | | | user_id
| int(11) | YES | | NULL | | +----------+-------------+------+-----+---------+--
--------------+ 3 rows in set