print '================================================================================
初始化数据库:
================================================================================'
USE master
GO
IF EXISTS(SELECT * FROM sysdatabases WHERE name='aDB')
DROP DATABASE aDB
GO
EXEC XP_cmdshell 'mkdir D:\project',no_output
CREATE DATABASE aDB
ON
(
)
LOG ON
(
)
GO
print '================================================================================
开始:
================================================================================'
USE aDB
GO
SET NOCOUNT ON
IF EXISTS(SELECT * FROM sysobjects WHERE name='Books')
DROP table Books
GO
create table Books
(
)
GO
IF EXISTS(SELECT * FROM sysobjects WHERE name='Authors')
DROP table Authors
GO
Create table Authors
(
)
declare @n int,@m int
select @n=1,@m=1
while (@n<=5)
begin
insert into Authors values(@n,substring(replace(newid(),'-',''),1,5))
set @n=@n+1
end
while (@m<=10)
begin
insert into Books values(@m,cast(rand()*5 as int)+1,substring(replace(newid(),'-',''),1,10)) --一个作者可以有多本书
set @m=@m+1
end
if exists (select * from sysobjects where name = 'newtable')
drop table newtable
GO
create table newtable
(
)
declare @name varchar(20)
declare @id int
print '================================================================================
全部作者信息:
================================================================================'
select 作者编号=ID,作者姓名=Name,写过的书籍=Title from newtable
GO
print '================================================================================
写过2本书以上的作者信息:
================================================================================'
select 作者编号=ID,作者姓名=Name,写过的书籍=Title from newtable N
where
(select count(*) from Books B where B.Author=N.ID)>=2
print '================================================================================
作者表:
================================================================================'
select * from Authors
print '================================================================================
书表:
================================================================================'
select * from Books
浙公网安备 33010602011771号