JUW Summer 2021 Database Lab Task
JUW Summer 2021 Database Lab Task
Primary keys maintain table record uniqueness by ensuring that each entry in a table is distinguished by a specific column or set of columns. In the context of 'shop' and 'customer' tables, the 'shop_id' for shops and 'cust_id' for customers serve as primary keys, preventing duplicate entries and enabling precise referencing from other related tables. This structure supports data integrity and efficient relational queries.
Joins in SQL are used to combine rows from two or more tables based on a related column between them. A common key, such as a primary key in one table and a foreign key in another, is essential for conducting join operations. This allows for related data to be fetched simultaneously.
You can determine the maximum and minimum expiry dates for a range of products using SQL aggregate functions like MAX() and MIN(). To rename these result columns, you can use the AS keyword to alias the column names. The SQL query can be formulated as: SELECT MAX(Expiry) AS Max, MIN(Expiry) AS Min FROM Products;
The ALTER TABLE statement is used to add, delete, or modify columns in an existing database table. For instance, to add an email column to a Customers table, you can use: ALTER TABLE Customers ADD Email varchar(255)
Table aliases are significant for simplifying complex SQL queries by giving tables or columns shorter, more readable temporary names. This makes the queries easier to write and understand. For example, using 'c' as an alias for the Customer table: SELECT c.cust_name FROM Customer AS c;
For querying product records that meet specific naming patterns, use the SQL LIKE clause, which allows pattern matching with wildcards. For example, to find products starting with 'K' and having 't' as the third letter, execute: SELECT * FROM Products WHERE ProductName LIKE 'K_t%';
To update the price of products from suppliers with a specific SupplierID, use the UPDATE statement with a WHERE clause to target specific rows. For example, to update products with SupplierID 1 to have a new price: UPDATE Products SET Price = new_value WHERE SupplierID = 1; Considerations include ensuring the correct rows are affected by verifying the SupplierID condition.
Data type constraints ensure that the data entered into a table conforms to expected formats and ranges, preventing anomalies and preserving data integrity. In the 'shopping' table, using an incorrect data type, such as DATE(20), could lead to storage inconsistency and retrieval errors. Proper data types ensure consistency and accuracy of stored information, enabling reliable database operations and analysis.
While designing a database table for a shopping application, it is crucial to select appropriate data types and sizes based on data characteristics and storage efficiency. For names, VARCHAR can be used with a reasonable size to accommodate varying lengths. For locations and similar text fields, VARCHAR with an adequate character limit is appropriate. For dates, DATE or DATETIME should be used depending on the precision needed. Proper consideration avoids data truncation and ensures efficient querying.
Foreign keys in relational database tables establish and enforce links between tables, maintaining data integrity. An example involving table creation with foreign keys is incorporating the primary keys of related tables as foreign keys. When creating a Shopping table, include shop_id and cust_id as foreign keys referencing Shop and Customer tables respectively: CREATE TABLE Shopping (shopping_id INT, shopping_date DATE, shop_id INT, cust_id INT, FOREIGN KEY(shop_id) REFERENCES Shop(shop_id), FOREIGN KEY(cust_id) REFERENCES Customer(cust_id))