DATE in sql and relation Algebra is NULL?

别等时光非礼了梦想. 提交于 2019-12-11 20:35:17

问题


I have a questions is it possible to set a date in tables NULL(in my case that a shipdate does not exist)? E.g I have this following tables:

Customer{cid,name}
Product{prodno,name}
Order{orderid, shipdate, cid}
Ordered{orderid, prodno, quantity}

And now I wanto to get all Products which were never ordered. So I create this in RA(Relational Algebra):

  πP.name,P.prodno(σO.cid = C.cid AND Order.shipdate is NULL(Order)))⋈Customer)

I am sure that it looks in SQL query:

  SELECT P.Name, P.Prodno 
  FROM CUSTOMERS C, Order O E, Ordered Ordd Product P
  WHERE O.CID = C.CID
  AND O.shipdate is Null

so I think if date is could possible in SQL than I can get all products that are not ordered. maybe it could be possible to do it with the table Ordered but how maybe if I check if the Order.orderid is not equal to Ordered.orderid and Porduct.prodno is not equal to Ordered.prodno not sure how so create it in the RA. But is to complex for only getting the Products, so I think my RA and SQ could be right or?

2.Update

 SELECT prodno, name
 FROM Products P, Order O Ordered Ordd
 WHERE prodno NOT IN (
     SELECT prodno FROM Ordered 

But how to create the RA?


回答1:


Yes, though the table schema has to declare the column as nullable. If you're creating a new table, you'll need to do something like

create table Order
(
  order_id  int      not null primary key ,
  ship_date datetime     null ,
  ...
)

If you are modifying an existing table, you'll need a magic incantation like

alter table Order alter column shop_date datetime null

The exact syntax may vary, depending on your particular flavour of SQL.




回答2:


There is no need of the table CUSTOMER, just get the prodno not in Ordered, unless you want to know which product each customers have never ordered.

In SQL

SELECT Prodno
FROM   Products 
WHERE  Prodno NOT IN (SELECT Prodno
                      FROM   Ordered)

or the equivalent

SELECT Prodno
FROM   Products 
EXCEPT 
SELECT Prodno
FROM   Ordered



回答3:


Apparently Product(prodno,name) means "product [prodno] is named [name]" and Ordered(orderid, prodno, quantity) means "order [orderid] was for quantity [quantity] of product [prodno]". But you need to make such meanings clear.

check if the Order.orderid is not equal to Ordered.orderid and Porduct.prodno is not equal to Ordered.prodno

That is just not clear enough to reason about the problem. Conditions don't always correspond to selections. Work to find a clear, full natural language description of what you want. Ordered products are those that appear in Ordered and unordered products are those that only appear in Product. Apparently you want prodno and name of unordered products.

We use algebraic substraction to remove a relation's tuples from another's. But subtraction arguments need the same attributes. The prodnos of products are π prodno Product and the prodnos of ordered products are π prodno Ordered. So unordered prodnos are π prodno Product - π prodno Ordered. But you want the prodno and name of the products with those prodnos:

(π prodno Product - π prodno Ordered) ⋈ Product

This doesn't involve customers.

This doesn't involve orders or their shipment dates. (Don't confuse shipped products with ordered products.)

However, your question topic involves dates and NULL. So I may not really have understood your question. If Order shipdate is nullable then that is a different issue. (You have to decide what relation corresponds to an SQL table with NULLs and you have to involve NULL in translating between algebra operators and SQL operators.)



来源:https://stackoverflow.com/questions/23791505/date-in-sql-and-relation-algebra-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!