SQL创建table的实例

本文介绍了SQL语言的四个主要部分:数据定义语言(DDL)、数据操作语言(DML)、数据查询语言(DQL)和数据控制语言(DCL)。并详细展示了如何使用SQL语句创建数据库和多个数据表,包括auction_user、kind、state、item和bid表,以及这些表之间的关系。

SQL语言包含4个部分:

★ 数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。

★ 数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。

★ 数据查询语言(DQL),例如:SELECT语句。

★ 数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。

创建数据库和选择

drop database if exists auction;
create database auction default character set utf8;#设置mysql编码为auction,并创建数据库 auction
use auction;

创建数据表

##创建表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'
);

创建上面表的顺序不可改变,因为需要用外键的表需要排列在后面创建

mysql> desc item;

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

+

| 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)


转载于:https://my.oschina.net/u/566829/blog/81463

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值