SQL建立table的實例

SQL語言包含4個部分:mysql

★ 數據定義語言(DDL),例如:CREATE、DROP、ALTER等語句。sql

★ 數據操做語言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(刪除)語句。數據庫

★ 數據查詢語言(DQL),例如:SELECT語句。編碼

★ 數據控制語言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等語句。spa

建立數據庫和選擇code

drop database if exists auction;
create database auction default character set utf8;#設置mysql編碼爲auction,並建立數據庫 auction
use auction;

建立數據表 rem

##建立表auction_user 
##自增auto_increment 主鍵primary key 惟一unique 
drop table if exists auction_user;
create table auction_user(
  user_id int(11) not null primary key auto_increment,
  username varchar(50) not null unique,
  userpass varchar(50) not null,
  email varchar(100) not null
);

##建立表kind
drop table if exists  kind;
create table kind(
kind_id int(11) not null primary key auto_increment,
kind_name varchar(50) not null,
kind_desc varchar(255) not null
);


##建立表state
drop table if exists state;
create table state(
state_id int(11) not null primary key auto_increment,
state_name varchar(10)
);

##建立表item
##外鍵foreign key(外鍵) refereneces 引用表(主鍵...)
##default 設置默認值
drop table if exists item;
create table item(
item_id int(11) not null primary key auto_increment,
item_name varchar(255) not null,
item_pic varchar(255) not null,
item_desc varchar(255) ,
kind_id int(11) not null default 0,
foreign key(kind_id) references kind(kind_id),
addtime datetime not null default '0000-00-00',
endtime datetime not null default '0000-00-00',
init_price double not null default 0,
max_price double not null default 0,
owner_id int(11) not null default 0,
winer_id int(11) default null,
state_id int(11) not null default 0,
foreign key(owner_id) references auction_user(user_id),
foreign key(winer_id) references auction_user(user_id),
foreign key(state_id) references state(state_id)
);

##建立表bid
drop table if exists bid;
create table bid(
bid_id int(11) not null primary key auto_increment,
user_id int(11) not null default 0,
foreign key(user_id) references auction_user(user_id),
item_id int(11) not null default 0,
foreign key(item_id) references item(item_id),
bid_price double not null default 0,
bid_date date not null default '0000-00-00'
);

建立上面表的順序不可改變,由於須要用外鍵的表須要排列在後面建立it

mysql> desc item;io

+------------+--------------+------+-----+---------------------+---------------table

+

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

|

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

+

| item_id    | int(11)      | NO   | PRI | NULL                | auto_increment

|

| item_name  | varchar(255) | NO   |     | NULL                |

|

| item_pic   | varchar(255) | NO   |     | NULL                |

|

| item_desc  | varchar(255) | YES  |     | NULL                |

|

| kind_id    | int(11)      | NO   | MUL | 0                   |

|

| addtime    | datetime     | NO   |     | 0000-00-00 00:00:00 |

|

| endtime    | datetime     | NO   |     | 0000-00-00 00:00:00 |

|

| init_price | double       | NO   |     | 0                   |

|

| max_price  | double       | NO   |     | 0                   |

|

| owner_id   | int(11)      | NO   | MUL | 0                   |

|

| winer_id   | int(11)      | YES  | MUL | NULL                |

|

| state_id   | int(11)      | NO   | MUL | 0                   |

|

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

+

12 rows in set (0.01 sec)

相關文章
相關標籤/搜索