SQL 断言:管理器复合体
我有以下架构:
Emp(eid int, ename varchar(50), salary float, email varchar(80))
Works(eid int, did< /strong> int, pct_time float)
Dept(did int, Budget float, managerid int)
eid 是员工 ID。 did 是部门 ID。 我已将主键加粗。 managerid是外键,Works中的eid和did也是如此。
现在,我想添加一个“经理复合体”断言,以确保经理的薪水始终高于他/她管理的任何员工。
这就是我一直在想的:
CREATE ASSERTION managerComplex
CHECK
( NOT EXISTS (SELECT M.salary
FROM Dept D, Emp M
WHERE D.managerid = M.eid) <=
(SELECT E.salary
FROM Works W, Emp E
WHERE W.eid = E.eid) AND
D.did = W.did);
这是否接近正确? 另外,也许我应该只进行几次检查而不是插入? 我觉得多次检查比较草率,但可能更容易正确。
编辑:我问的主要原因是因为我不知道我是否正确理解不存在
I have the following schema:
Emp(eid int, ename varchar(50), salary float, email varchar(80))
Works(eid int, did int, pct_time float)
Dept(did int, budget float, managerid int)
eid is for employee id.
did is for department id.
I've bolded primary keys.
managerid is a foreign key, as well as eid and did in Works.
Now, I'd like to add a "manager complex" assertion that ensure a manager will always have a higher salary than any employee that he/she manages.
Here's what I've been thinking:
CREATE ASSERTION managerComplex
CHECK
( NOT EXISTS (SELECT M.salary
FROM Dept D, Emp M
WHERE D.managerid = M.eid) <=
(SELECT E.salary
FROM Works W, Emp E
WHERE W.eid = E.eid) AND
D.did = W.did);
Is this even close to right?
Also, maybe I should have just made a couple CHECKs instead of an insertion?
I feel like multiple CHECKs is sloppier but would probably be easier to get right.
EDIT: The main reason I am asking is because I don't know if I'm correctly understanding NOT EXISTS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你很接近。我对您的试用版的编辑:
I think you are close. My edit to your trial:
要使
NOT EXISTS ( SELECT ...)
为 true,SELECT ...
必须不返回任何结果。您想要创建一个
SELECT
语句,该语句将返回薪水高于其所在部门列出的经理的所有员工。看看您是否可以编写该查询,然后将其放入NOT EXISTS
中。我不完全确定您编写的内容是否有效 SQL,但它肯定不是您想要的。我有一个可能的答案给你,但由于这是标记为家庭作业的,我想在提供完整的答案之前尝试向你指出正确的方向。
正如OP所说,作业已上交...
我的
NOT EXISTS
内部有一个查询,如果有任何员工的工资高于其各自经理的工资,它将返回结果。我查看为某个部门工作的所有员工,只有当他们的工资高于该部门经理的工资时,才将他们拉入结果集。我什至不确定你的 SQL 是否有效,因为你有一个
(SELECT ...) <= (SELECT ...) AND ...
。将结果集与<=
进行比较对我来说没有任何意义。这可能只是我从未使用过的 SQL,但是...我从未使用过它。For
NOT EXISTS ( SELECT ...)
to be true, theSELECT ...
must return no results.You want to create a
SELECT
statement that will return any employee with a salary higher than the manager listed for the department(s) they work for. See if you can write that query, and then place it inside theNOT EXISTS
I'm not entirely sure what you wrote is even valid SQL, but it's certainly not what you want. I have a potential answer for you, but as this was marked homework, I'd like to attempt to prod you in the right direction before providing a complete answer.
As OP said the homework has been turned in...
My
NOT EXISTS
has a query inside of it, that will return results if there are any employee salaries higher than their respective manager's salary. I look at all employees who work for a department, and only pull them into the result set if their salary is higher than the manager for that department's salary.I'm not even sure your SQL is valid, as you have a
(SELECT ...) <= (SELECT ...) AND ...
. Comparing result sets with a<=
doesn't make any sense to me. It's possible that's just SQL I've never used, but... I've never used it.