Mysql基礎教程 mysql進階

Mysql添加和刪除默認值



添加默認值


ALTER TABLE tbl_name ALTER 字段名稱 SET DEFAULT 默認值


查看表


mysql> SHOW TABLES;
+-----------------+
| Tables_in_maizi |
+-----------------+
| cms_cate        |
| cms_news        |
| course          |
| test1           |
| test10          |
| test11          |
| test2           |
| test3           |
| test4           |
| test5           |
| test6           |
| test7           |
| test8           |
| test9           |
| user            |
| user1           |
| user10          |
| user2           |
| user3           |
| user4           |
| user5           |
| user6           |
| user7           |
| user8           |
| user9           |
+-----------------+
25 rows in set (0.00 sec)


創建表text12


mysql> CREATE TABLE IF NOT EXISTS user11(
    -> id TINYINT UNSIGNED KEY AUTO_INCREMENT ,
    -> username VARCHAR(20) NOT NULL UNIQUE,
    -> age TINYINT UNSIGNED
    -> );
Query OK, 0 rows affected (0.31 sec)


查看表結構


mysql> DESC user11;
+----------+---------------------+------+-----+---------+----------------+
| Field    | Type                | Null | Key | Default | Extra          |
+----------+---------------------+------+-----+---------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)       | NO  | UNI | NULL    |                |
| age      | tinyint(3) unsigned | YES  |     | NULL   |                |
+----------+---------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)


選中字段添加默認值


mysql> ALTER TABLE user11 ALTER age SET DEFAULT 18;
Query OK, 0 rows affected (0.07 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC user11;
+----------+---------------------+------+-----+---------+----------------+
| Field    | Type                | Null | Key | Default | Extra          |
+----------+---------------------+------+-----+---------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)         | NO   | UNI | NULL    |                |
| age      | tinyint(3) unsigned | YES  |     | 18      |                |
+----------+---------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)


添加一個字段


mysql> ALTER TABLE user11 ADD email VARCHAR(50) ;
Query OK, 0 rows affected (0.76 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC user11;
+----------+---------------------+------+-----+---------+----------------+
| Field    | Type             | Null | Key | Default | Extra          |
+----------+---------------------+------+-----+---------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)      | NO   | UNI | NULL    |                |
| age      | tinyint(3) unsigned | YES  |     | 18      |                |
| email    | varchar(50)       | YES  |     | NULL    |                |
+----------+---------------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)


給email添加默認值


mysql> ALTER TABLE user11 ALTER email SET DEFAULT '[email protected]';
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC user11;
+----------+---------------------+------+-----+------------------+----------------+
| Field    | Type                | Null | Key | Default          | Extra          |
+----------+---------------------+------+-----+------------------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL             | auto_increment |
| username | varchar(20)      | NO   | UNI | NULL             |                |
| age      | tinyint(3) unsigned | YES  |     | 18               |                |
| email    | varchar(50)       | YES  |     | [email protected] |                |
+----------+---------------------+------+-----+------------------+----------------+
4 rows in set (0.00 sec)



刪除默認值


ALTER TABLE tbl_name ALTER 字段名稱 DROP DEFAULT


刪除默認值,先選中這段,再刪除字段


mysql> ALTER TABLE user11 ALTER age DROP DEFAULT;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC user11;
+----------+---------------------+------+-----+------------------+----------------+
| Field    | Type                | Null | Key | Default          | Extra          |
+----------+---------------------+------+-----+------------------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL             | auto_increment |
| username | varchar(20)      | NO   | UNI | NULL             |                |
| age      | tinyint(3) unsigned | YES  |     | NULL             |                |
| email    | varchar(50)       | YES  |     | [email protected] |                |
+----------+---------------------+------+-----+------------------+----------------+
4 rows in set (0.00 sec)


刪除email默認值


mysql> ALTER TABLE user11 ALTER age DROP DEFAULT;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> DESC user11;
+----------+---------------------+------+-----+------------------+----------------+
| Field    | Type                | Null | Key | Default          | Extra          |
+----------+---------------------+------+-----+------------------+----------------+
| id       | tinyint(3) unsigned | NO   | PRI | NULL             | auto_increment |
| username | varchar(20)       | NO   | UNI | NULL             |                |
| age      | tinyint(3) unsigned | YES  |     | NULL             |                |
| email    | varchar(50)       | YES  |     | [email protected] |                |
+----------+---------------------+------+-----+------------------+----------------+
4 rows in set (0.00 sec)



【本文由麥子學院獨家原創,轉載請注明出處并保留原文鏈接】

logo
? 2012-2016 www.jvbprd.live
蜀ICP備13014270號-4 Version 5.0.0 release20160127

免費領取價值1888元求職寶典!

客服熱線 400-862-8862

回到頂部

彩票安徽25选5 蛇怎么赚钱 汇收款如何赚钱 怎么用支付宝给花呗赚钱 欢乐捕鱼3破解游戏 湖南麻将规则 开京东电器实体店能赚钱吗 魔兽世界8.0 爱游贵州麻将官网 学画画后做那种工作赚钱 财神捕鱼技巧 什么传奇可以直接回收装备赚钱 pos机刷流水赚钱 拉菲彩票安卓 东方头条视频怎么赚钱 明年免费的好的赚钱前门 ↙微乐龙江麻将