0% found this document useful (0 votes)
76 views4 pages

SQL Database Creation and Data Insertion

Uploaded by

api-739359721
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views4 pages

SQL Database Creation and Data Insertion

Uploaded by

api-739359721
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BEGIN TRANSACTION

-- USE BMGT402_DB_Instructor_01
-- USE BMGT402_DB_Student_XXX

DROP TABLE OrderDetails;


DROP TABLE DeliveryPerson;
DROP TABLE UMDStudent;
DROP TABLE MenuItem;

CREATE TABLE UMDStudent (


StudentID INT NOT NULL,
StudentName VARCHAR(25) NOT NULL,
Dorm VARCHAR(25) NOT NULL,
CONSTRAINT pk_UMDstudent PRIMARY KEY (StudentID));

CREATE TABLE MenuItem (


itemNo INT NOT NULL,
category VARCHAR(25) NOT NULL,
itemName VARCHAR(25) NOT NULL,
price INT NOT NULL,
priceRange VARCHAR(25) NOT NULL,
itemDescription VARCHAR(30) NOT NULL,
CONSTRAINT pk_MenuItem PRIMARY KEY (itemNo));

CREATE TABLE DeliveryPerson (


deliveryNo INT NOT NULL,
PersonName VARCHAR(25) NOT NULL,
DeliveryPersonID INT NOT NULL,
CONSTRAINT pk_DeliveryPerson PRIMARY KEY (deliveryNo));

CREATE TABLE OrderDetails (


OrderNo INT IDENTITY (50,1) NOT NULL,
StudentID INT NOT NULL,
itemNo INT NOT NULL,
quantity INT NOT NULL,
tipAmount NUMERIC(3,2) NOT NULL,
deliveryNo INT NOT NULL,
orderDate DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT pk_OrderDetails PRIMARY KEY (OrderNo),
CONSTRAINT fk1_OrderDetails FOREIGN KEY (studentID) REFERENCES
UMDStudent(StudentId),
CONSTRAINT fk2_OrderDetails FOREIGN KEY (itemNo) REFERENCES MenuItem(itemNo),
CONSTRAINT fk3_OrderDetails FOREIGN KEY (deliveryNo) REFERENCES
DeliveryPerson(deliveryNo));
--------------------------------------------------------------------
-- Inserting data
INSERT INTO UMDStudent VALUES
(123456,'Bob','Hagerstown'),
(234561,'Jerry','Oakland'),
(345612,'Barbie','Ellicott'),
(456123,'Kendall','Prince Frederick'),
(561234,'Taylor','Johnson-Whittle'),
-- additional values
(678901,'Alex','Prince Frederick'),
(789012,'Emily','Hagerstown'),
(890123,'Max','Ellicott'),
(901234,'Sophia','Oakland'),
(123890,'Luke','Johnson-Whittle'),
(234901,'Emma','Hagerstown'),
-- additional values
(345678,'Avery','Prince Frederick'),
(456789,'Cameron','Hagerstown'),
(567890,'Dylan','Ellicott'),
(078901,'Emily','Oakland'),
(089012,'Jordan','Johnson-Whittle')
--------------------------------------------------------------------
INSERT INTO MenuItem VALUES
(1,'Appetizer','Hummus Platter',23,'$$$','Hummus with oil and herbs'),
(2,'Entree','Greek Salad',8,'$','Romaine with greek dressing'),
(3,'Entree','Falafel Sandwich',13,'$$','Fried falafel in pita bread'),
(4,'Entree','Lamb gyros',11,'$$','Roasted lamb in pita bread'),
(5,'Entree','Greek beef stew',9,'$','Red wine beef stew in broth'),
(6,'Dessert','Lemon & Honey Baklava',15,'$$','Layers of phyllo dough')
-- do not need to add additional values to this
--------------------------------------------------------------------
INSERT INTO DeliveryPerson VALUES
(101,'Josh', 987654),
(102, 'Savannah', 876543),
(103,'Ava', 765432),
(104,'Carlos', 654321),
(105,'Theo', 543210),
(106,'Jerry', 543210),
(107, 'Long', 654321),
(108,'Ron', 765432),
(109,'Wesley', 876543),
(110,'Simon', 987654),
-- additional values
(111,'Adam', 112233),
(112,'Bella', 223344),
(113,'Charlie', 334455),
(114,'Diana', 445566),
(115,'Ethan', 556677),
(116,'Fiona', 667788),
-- additional values
(117, 'Olivia', 778899),
(118, 'Parker', 889900),
(119, 'Quinn', 990011),
(120, 'Riley', 001122),
(121, 'Sawyer', 112233);
--------------------------------------------------------------------
INSERT INTO OrderDetails VALUES
(123456, 1, 3, 7.10, 101, '2023-05-01'),
(234561, 2, 2, 3.45, 102, '2023-05-01'),
(345612, 6, 1, 4.50, 103, '2023-05-02'),
(456123, 5, 3, 6.75, 104, '2023-05-02'),
(561234, 4, 5, 5.65, 105, '2023-05-02'),
(123456, 4, 3, 1.10, 106, '2023-05-03'),
(234561, 5, 2, 1.12, 107, '2023-05-04'),
(345612, 3, 1, 1.14, 108, '2023-05-04'),
(456123, 2, 3, 1.16, 109, '2023-05-04'),
(561234, 1, 5, 1.18, 110, '2023-05-04'),
-- additional values
(678901, 6, 2, 4.10, 111, '2023-05-06'),
(789012, 5, 5, 2.45, 112, '2023-05-07'),
(890123, 4, 1, 3.50, 113, '2023-05-07'),
(901234, 4, 3, 5.75, 114, '2023-05-08'),
(123890, 4, 4, 4.65, 115, '2023-05-08'),
(234901, 3, 6, 1.10, 112, '2023-05-10'),
(078901, 2, 5, 2.12, 111, '2023-05-11'),
(789012, 1, 3, 3.14, 112, '2023-05-11'),
(890123, 1, 1, 1.16, 112, '2023-05-11'),
(901234, 2, 6, 5.18, 114, '2023-05-12'),
(123890, 3, 2, 4.20, 115, '2023-05-12'),
(234901, 4, 4, 1.22, 116, '2023-05-12'),
-- additional values
(901234, 6, 4, 3.75, 113, '2023-05-13'),
(123890, 2, 5, 2.20, 112, '2023-05-14'),
(234901, 1, 1, 1.50, 115, '2023-05-15'),
(345678, 3, 3, 2.25, 114, '2023-05-15'),
(456789, 4, 2, 1.80, 115, '2023-05-16')
commit;
--------------------------------------------------------------------
-- Displaying all results from the tables
SELECT * FROM UMDStudent;
SELECT * FROM MenuItem;
SELECT * FROM DeliveryPerson;
SELECT * FROM OrderDetails;
--------------------------------------------------------------------

Common questions

Powered by AI

The database schema includes four primary tables: UMDStudent, MenuItem, DeliveryPerson, and OrderDetails. The UMDStudent table stores student information with 'StudentID' as the primary key. The MenuItem table contains menu items with 'itemNo' as the primary key. The DeliveryPerson table tracks delivery personnel using 'deliveryNo' as the primary key. The OrderDetails table links orders with students, menu items, and delivery persons via foreign keys: 'StudentID' (referencing UMDStudent), 'itemNo' (referencing MenuItem), and 'deliveryNo' (referencing DeliveryPerson). These foreign key constraints ensure that records in OrderDetails correspond to valid entries in UMDStudent, MenuItem, and DeliveryPerson, enforcing referential integrity .

The MenuItem table stores item prices as integers, limiting granularity for precise financial tracking. Each item has a 'price' and a 'priceRange', which lacks real-time adjustment capability or robust categorization. To improve financial reporting, transitioning prices to a decimal data type would offer finer precision and compatibility with monetary values. Additionally, introducing a separate classification table for 'priceRange' would facilitate dynamic updates and potentially enrich sales reporting. Regular recalibration based on sales data can also enhance price listings, ensuring better financial alignment with market demands .

The primary key selection appears effective in uniquely identifying records across tables: 'StudentID' for UMDStudent, 'itemNo' for MenuItem, 'deliveryNo' for DeliveryPerson, and 'OrderNo' for OrderDetails. However, potential drawbacks include the lack of composite keys where a combination of attributes may better ensure uniqueness, particularly for MenuItem or DeliveryPerson where duplicate names can occur. Additionally, using a natural key like StudentName or ItemName might enhance data integrity but at the cost of added complexity. An alternative could involve a unique composite key in OrderDetails combining 'StudentID', 'itemNo', and 'orderDate' to preemptively address issues with concurrent orders on similar dates by the same student .

The identity property for the 'OrderNo' column in the OrderDetails table ensures that each new order entry is automatically assigned a unique sequential number starting from 50, incrementing by 1 for each new record. This feature simplifies data entry by automatically generating a primary key for each order, reducing the likelihood of key collisions. Additionally, it streamlines data retrieval and sorting by providing a consistent and ordered sequence, thus facilitating tracking and managing orders over time .

The current database design may limit scalability and analytical capabilities due to its limited fields per table and lack of normalization. For example, price fields are stored as integers rather than decimals, which can lead to precision issues for financial analysis. Moreover, the 'priceRange' column in MenuItem lacks normalization with distinct categorical tables, hindering flexible querying and ordering by price. Furthermore, the static VARCHAR size constraints without indexing can slow retrieval times with increased data volume. Additionally, the identity property of 'OrderNo' in the OrderDetails table assumes a simple linear growth which might not suit varied or complex ordering patterns, potentially affecting performance as data grows .

Using VARCHAR for student and menu item names allows more natural, readable data, facilitating clear record identification and user-friendly data retrieval. However, VARCHAR consumes variable storage space, potentially leading to inefficient storage and slower retrieval times due to non-fixed lengths. In contrast, numeric identifiers would enhance storage efficiency and query speed due to fixed integer sizes, facilitating faster indexing operations. Nonetheless, numeric identifiers require lookup tables for mapping numbers to real-world names, which can complicate data interpretation and report generation. Therefore, the choice depends on balancing user accessibility with performance efficiency .

The schema allows analysis of delivery efficiency by connecting orders to delivery personnel and tracking the 'orderDate' in the OrderDetails table. This setup enables performance comparisons across different dates and between delivery personnel. However, the current design lacks direct attributes for tracking delivery time spans or outcomes per delivery, which could restrict detailed efficiency analysis. Implementing columns for delivery start/end times or success indicators would enable a more comprehensive efficiency evaluation, offering insights into performance variances beyond mere delivery counts or personnel assignments .

The current design of the UMDStudent table, which includes a fixed 'Dorm' field as a string of preset VARCHAR length, risks limiting flexibility amid changes in university housing structures. If dorm names, assignments, or structures significantly change, extensive data modifications through batch updates or schema redesign might be needed. Such changes can lead to increased administrative overhead and potential downtime or errors if not managed efficiently. Introducing a separate Dorm table with unique dorm IDs could increase flexibility, allowing dorm names and structures to change without affecting the UMDStudent table directly .

Foreign key constraints in the OrderDetails table serve to link each order to existing records in related tables, thereby maintaining database integrity. The 'StudentID' foreign key ensures each order can be traced back to a legitimate student entry in the UMDStudent table. The 'itemNo' foreign key mandates that each order pertains to an existing menu item in the MenuItem table. The 'deliveryNo' foreign key confirms that the assigned delivery person is recognized in the DeliveryPerson table. These constraints prevent orphan records and ensure all relational links are valid and refer to existent records in other tables, thus preserving the logical consistency of the database .

The default value for 'orderDate' in OrderDetails set to 'CURRENT_TIMESTAMP' ensures automatic capturing of the order entry date, facilitating data accuracy in timestamps. This reduces manual errors during data entry, maintaining precise records of when orders are logged. However, this design choice has limitations for entering historical data or correcting entries afterwards without additional administrative rights or adjustments. It may inadvertently skew historical analyses unless explicitly updated post-entry, which requires additional database permissions and potential risks of data integrity issues if not managed correctly .

You might also like