- MySQL數(shù)據(jù)庫(kù)管理實(shí)戰(zhàn)
- 甘長(zhǎng)春 孟飛
- 478字
- 2020-04-14 15:02:50
2.2.6 創(chuàng)建數(shù)據(jù)表
MySQL中使用CREATE TABLE命令創(chuàng)建表,基本語(yǔ)法如下:
CREATE TABLE table_name (column_name datatype [NULL|not null] [DEFAULT default_value][AUTO_INCREMENT][PRIMARY KEY] [COMMENT])
下面介紹各個(gè)屬性。
· table_name:要?jiǎng)?chuàng)建的表的名稱。
· column_talbe:表內(nèi)字段的名稱。
· datatype:字段的數(shù)據(jù)類型。
· NULL|NOT NULL:字段是否可以為空。
· DEFAULT:給字段提供一個(gè)默認(rèn)值。
· AUTO_INCREMENT:自動(dòng)增長(zhǎng)。
· PRIMARY KEY:主鍵。
· COMMENT:對(duì)字段的注釋。
當(dāng)創(chuàng)建表時(shí),必須指定表名、列名及數(shù)據(jù)類型。對(duì)同一個(gè)表而言,列名必須唯一,每一列都必須指定數(shù)據(jù)類型。
下面在數(shù)據(jù)庫(kù)中創(chuàng)建一個(gè)商品表,命令如下:
mysql> USE finecms; DatabASe changed mysql> CREATE TABLE shopnc_product ( id INt(11) NOT NULL auto_INcrement, product_name varchar(30) default null, product_num INt(11) default null, product_price DECIMAL(8,2) default null, product_pic varchar(50) default null, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FOR MAT=DYNAMIC COMMENT='product'; Query OK, 0 rows affected (0.08 sec)
查看剛剛創(chuàng)建的商品表,命令如下:
mysql> SHOW TABLES; +----------------+ | Tables_in_shop | +----------------+ | shopnc_product | +----------------+ 1 row in set (0.00 sec)
如果想進(jìn)一步查看表結(jié)構(gòu),可使用DESCRIBE命令。
mysql> DESCRIBE shopnc_product; +---------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+----------------+ | id | INt(11) | NO | PRI | NULL | auto_INcrement | | product_name | varchar(30) | YES | | NULL | | | product_num | INt(11) | YES | | NULL | | | product_price | DECIMAL(8,2) | YES | | NULL | | | product_pic | varchar(50) | YES | | NULL | | +---------------+--------------+------+-----+---------+----------------+ 5 rows in set (0.02 sec)
同時(shí)可以使用SHOW CREATE TABLE命令來(lái)自動(dòng)生成創(chuàng)建表的語(yǔ)句。
mysql> SHOW CREATE TABLE shopnc_product\G; *************************** 1. row *************************** Table: shopnc_product CREATE table: CREATE TABLE shopnc_product ( id INt(11) NOT NULL auto_INcrement, product_name varchar(30) default null, product_num INt(11) default null, product_price DECIMAL(8,2) default null, product_pic varchar(50) default null, PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC COMMENT='product'
推薦閱讀
- Learning Single:page Web Application Development
- Java系統(tǒng)分析與架構(gòu)設(shè)計(jì)
- arc42 by Example
- Hands-On Image Processing with Python
- 少年輕松趣編程:用Scratch創(chuàng)作自己的小游戲
- Vue.js 3.x從入門到精通(視頻教學(xué)版)
- Troubleshooting PostgreSQL
- C語(yǔ)言課程設(shè)計(jì)
- PhoneGap:Beginner's Guide(Third Edition)
- Raspberry Pi Home Automation with Arduino(Second Edition)
- 人工智能算法(卷1):基礎(chǔ)算法
- C++程序設(shè)計(jì)教程(第2版)
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- IPython Interactive Computing and Visualization Cookbook
- UML基礎(chǔ)與Rose建模實(shí)用教程(第三版)