文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

基本的SQL语句

2018-11-15 17:04

关注

基本的SQL语句

表结构




实验内容

1

CREATE DATABASE CAP
ON
(
	NAME = CAP,
	FILENAME = "C:UserseroDesktopCAPData.mdf", -- 文件名
	SIZE = 50, -- 初始存储空间大小
	MAXSIZE = 500,  -- 最大存储空间大小
	FILEGROWTH = 10  -- 自动增长量
)

2

use CAP
CREATE TABLE Customers
(
	"cid" char(4) CONSTRAINT Custom_Prim PRIMARY KEY,
	"cname" varchar(10),
	"city" varchar(10),
	"discnt" numeric(4,2) CONSTRAINT DISCNT_CHK CHECK(discnt BETWEEN 0 AND 30)
)
CREATE TABLE Products
(
	"Pid" char(3) CONSTRAINT Product_Prim PRIMARY KEY,
	"pname" varchar(10),
	"city" varchar(10),
	"quantity" int,
	"price" numeric(10,2) CONSTRAINT Price_NotNull NOT NULL
)
CREATE TABLE Agents
(
	"Aid" char(3) CONSTRAINT Agent_Prim PRIMARY KEY,
	"aname" varchar(10),
	"city" varchar(10),
	"percent" TINYINT
)
CREATE TABLE Orders
(
	"OrDno" char(4) CONSTRAINT Order_Prim PRIMARY KEY,
	"month" char(3) CONSTRAINT Month_CHK CHECK(month in ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") ),
	"cid" char(4) CONSTRAINT Cid_Fore FOREIGN KEY REFERENCES Customers(cid),
	"aid" char(3) CONSTRAINT Aid_Fore FOREIGN KEY REFERENCES Agents(Aid),
	"pid" char(3) CONSTRAINT Pid_Fore FOREIGN KEY REFERENCES Products(Pid),
	"qty" int,
	"dollars" numeric(10,2)
)

3

EXEC sp_helpdb

4

EXEC sp_helpconstraint Orders

5

CREATE TABLE Orders_Jan
(
	"OrDno" char(4) CONSTRAINT Order_Jan_Prim PRIMARY KEY,
	"month" char(3) ,
	"cid" char(4) CONSTRAINT Jan_Cid_Fore FOREIGN KEY REFERENCES Customers(cid),
	"aid" char(3) CONSTRAINT Jan_Aid_Fore FOREIGN KEY REFERENCES Agents(Aid),
	"pid" char(3) CONSTRAINT Jan_Pid_Fore FOREIGN KEY REFERENCES Products(Pid),
	"qty" int,
	"dollars" numeric(10,2)
);

INSERT INTO Orders_Jan
SELECT * 
FROM Orders
WHERE month="Jan"

6

DELETE 
FROM Orders
WHERE month="Jan"

7

UPDATE Customers
SET  discnt = discnt + 2  -- 更新discnt
WHERE cid IN(			-- 找出下过500订单的客户cid
			SELECT DISTINCT cid -- 使用DISTINCT防止对同一个客户重复更新discnt
            -- 其实可以直接SELECT cid , IN会自动去除重复值
			FROM Orders
			WHERE dollars > 500
			)

8

use CAP
DECLARE @i AS INT,@randNum AS FLOAT,@mon AS char(3),@OrdNo AS SMALLINT,@cid AS char(4),@aid AS char(3),@pid AS char(3),@price AS numeric(10,2);
SET @i=1;
SET @OrdNo=1030;
while @i<=5000  -- 流程控制,循环5000次
	BEGIN
		SET @randNum=RAND()*12;
		SET @mon=							-- 随机产生月份
		CASE
			WHEN @randNum<1 THEN "Jan"
			WHEN @randNum>=1 AND @randNum<2 THEN "Feb"
			WHEN @randNum>=2 AND @randNum<3 THEN "Mar"
			WHEN @randNum>=3 AND @randNum<4 THEN "Apr"
			WHEN @randNum>=4 AND @randNum<5 THEN "May"
			WHEN @randNum>=5 AND @randNum<6 THEN "Jun"
			WHEN @randNum>=6 AND @randNum<7 THEN "Jul"
			WHEN @randNum>=7 AND @randNum<8 THEN "Aug"
			WHEN @randNum>=8 AND @randNum<9 THEN "Sep"
			WHEN @randNum>=9 AND @randNum<10 THEN "Oct"
			WHEN @randNum>=10 AND @randNum<11 THEN "Nov"
			ELSE "Dec"
		END

		SELECT  @cid=cid 	-- 随机获取一个用户id
		FROM Customers
		ORDER BY NEWID()

		SELECT @pid=Pid,@price=price	-- 随机获取一个产品id以及对应的产品价格
		FROM Products
		ORDER BY NEWID()
		
		SET @aid=(SELECT TOP 1 Aid 			-- 随机获取一个代理商id
				  FROM Agents
		          ORDER BY NEWID()
				  )
		
		SET @randNum=RAND()*2000+400;  -- 生成400至2400的随机订单数

		INSERT INTO Orders				-- 插入一条订单记录
		VALUES(CONVERT(char(4),@OrdNo),@mon,@cid,@aid,@pid,CONVERT(INT,@randNum),CONVERT(INT,@randNum) * @price);
        -- 使用转换函数CONVERT()

		SET @OrdNo=@OrdNo+1;
		SET @i=@i+1;
	END

9

CREATE INDEX Orders_Index
ON Orders(month)

10

CREATE VIEW order_month_summary(month,total_qty,total_dollars)
AS SELECT month,SUM(qty),SUM(dollars)      -- 获得每月的总订单,总金额
   FROM Orders
   GROUP BY month
SELECT month,total_qty,total_dollars
FROM order_month_summary
WHERE month IN ("Jan","Feb","Mar")     -- 查询第一季度
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯