پایگاه داده ای تهیه کننده و قطعه دارای به جدول اس ت: S ( S# , SNAME , STATUS , CITY ) P…
- User
پایگاه داده ای تهیه کننده و قطعه دارای به جدول اس ت: S ( S# , SNAME , STATUS , CITY ) P ( P# , PNAME , COLOR , WEIGHT , CITY ) J ( J# , JNAME , CITY ) SPJ ( S# , P# , J# , QTY ) ز جدول S مشخصات تهیه کنند گان و در جدول P مشخصات قطعات و در جدول SP ان تولید قطعات توسط م ی تهیه کنندگان نگهداری م شود . حال با توجه به این رابطه ها به هر یک از س ئواالت زیر پاس خ دهید: 1 -نام تهیه کنندگانی را بیابید که قطعه P2 را تهیه می کنند. 2 -پرس و جوی قبلی را به کمک EXEISTS بنویسید . 3 -نام تهیه کنندگانی را بیابید که اقالً یک قطعه به رنگ RED تهیه می کنند. 4 -شماره قطعاتی را بیابید که توسط بیش از یک تهیه کننده، تهیه شده باشد. 5 -حداکثر مقدار تهیه شده از هر قطعه را بیابید. 6 -شماره قطعاتی را بیابید که یا وزن آنها بیشتر از 16 باشد یا توسط S2 تهیه شده است یا هر دو شرط را دارد 7 -تمام جفت هایی از نام شهرها را مشخص کنید که عرضه کننده ای در ش هر اول، پروژه ای در ش هر دوم را پشتیبانی کند. 8 -شماره قطعاتی را بیابید که به پروژه ای اختصاص می یابد و میانگین آن بیش از 300 است. 9 -شماره پروژه هایی را مشخص کنید که شهر آن پروژه، اولین شهر به ترتیب الفبا باشد. 10 -جفت هایی از شماره قطعه/ شماره عرضه کننده را مشخص کنید که عرضه کننده خاص قطعه مشخص شد ه را عرضه نمی کند.
- Assistant
این پرس و جوها باید با استفاده از SQL نوشته شوند. در زیر، برای هر یک از سوالات بالا یک پرس و جوی مثال آورده شده است:
-
نام تهیهکنندگانی که قطعه P2 را تهیه میکنند:
SELECT S.SNAME FROM S WHERE EXISTS (SELECT * FROM SPJ WHERE S.S# = SPJ.S# AND SPJ.P# = 'P2'); -
پرس و جوی قبلی به کمک EXISTS:
SELECT S.SNAME FROM S WHERE EXISTS (SELECT * FROM SPJ WHERE SPJ.S# = S.S# AND SPJ.P# = 'P2');
-
- User
Why did you use group by in the 4th one and having in the 5th
- Assistant
Let's break down why
GROUP BYandHAVINGclauses were utilized in the 4th and 5th queries respectively:-
To find the part numbers that are supplied by more than one supplier, the
GROUP BYclause is essential. It groups all records that have the same part number (P#) so they can be aggregated in some manner—in this case, by counting the distinct number of suppliers (S#) for each part. Here is the query:
-
- User
What does max(qty)
- Assistant
The
MAX(QTY)function in SQL is an aggregate function that finds the maximum value of theQTYcolumn for each distinct group specified by theGROUP BYclause. If there's noGROUP BYclause, it finds the maximum value across all rows.In the context of the 5th query provided in the previous answer,
MAX(QTY)is used to find the highest quantity (QTY) of each part (P#) supplied in theSPJtable. This would give you the largest number of units of each part ordered from any supplier, for any project.Here's that query once again for reference:
SELECT P#, MAX(QTY) AS MAX_QUANTITY FROM SPJ GROUP BY P#;Here is a breakdown of what the query does:
SELECT P#: This part of the query specifies that the output should include the part number (P#).- : This calculates the maximum quantity ordered for each part number. The part renames the output of to a more readable column name, "MAX_QUANTITY".
- User
Explain them all
- Assistant