数据库设置字段自增长(范例)

#VSTS虚拟股票交易系统表

#表1 交易表(TRADE)

create table Trade(
tradeid number primary key,
tradeprice number(10,5),
tradenumber number,
tradedate date,
userid number references User1(userid),
stockid number references Stock(stockid),
tadetypeid number references Tradetype(tadetypeid)
);

#表2 用户表(USER1)

create table User1(
userid number primary key,
name varchar2(32) unique,
passwd varchar2(32),
email varchar2(32),
phone varchar2(32),
reg_date date
);


#表3 股票表(STOCK)

create table Stock(
stockid number primary key,
name varchar2(32),
code varchar2(32),
price number(15,3)
);


#表5 股票类型(TRADETYPE)


create table Tradetype(
tadetypeid number primary key,
info varchar2(32)
);


#表5 所持股(holding)
create table Holding(
holdid number primary key,
number1 number,
avgprice number(15,5),
stockid number references Stock(stockid),
userid number references User1(userid)
);


#自增长

创建股票表
#先创建序列----stock_sequ
create sequence stock_sequ
increment by 1
start with 1;

#创建触发器----stock_trigger
create trigger stock_trigger
before insert on stock
for each row
begin
select stock_sequ.nextval into:new.stockid from dual;
end;

创建用户表
#先创建序列----user1_sequ

create sequence user1_sequ
increment by 1
start with 1;

#创建触发器----user1_trigger
create trigger user1_trigger
before insert on user1
for each row
begin
select user1_sequ.nextval into:new.userid from dual;
end;

#删除表
drop table trade;
drop table holding;
drop table tradetype;
drop table user1;
drop table stock;

你可能感兴趣的:(数据库设置字段自增长(范例))