例
/* 产品销售分析 */
use master
go
if (exists (select * from master.dbo.sysdatabases where name='LeetCode'))
begin
drop database LeetCode
select 'dd'
end
create database LeetCode
go
use LeetCode
go
if (exists (select * from sys.objects where name='Product'))
drop table Product
go
create table Product
(
product_id int,
product_name varchar(15),
primary key (product_id)
)
if (exists (select * from sys.objects where name='Sales'))
drop table Sales
go
create table Sales
(
sale_id int identity,
product_id int,
year int,
quantity int,
price int
primary key (sale_id, year),
foreign key (product_id) references Product(product_id) -- 顺序,需先创建表Product
)
insert into Product values -- 顺序