[Examination] Quản lý sản phẩm - Lập trình SQL Server
Part 1: Database Creation
Create a database named
ProductManagementSystem.Switch the current context to use the
ProductManagementSystemdatabase.
Part 2: Table Creation
Create the following four tables with the specified data types and constraints:
1. tblUser (Stores user information)
| Column | Datatype | Constraint |
| UserID | INT | NOT NULL |
| UserName | NVARCHAR(50) |
2. tblOrder (Stores order information)
| Column | Datatype | Constraint |
| OrderID | INT | NOT NULL |
| UserID | INT | NOT NULL |
| OrderDate | DATETIME |
3. tblProduct (Stores product information)
| Column | Datatype | Constraint |
| ProductID | INT | NOT NULL |
| ProductName | NVARCHAR(50) | |
| Quantity | INT | |
| Price | MONEY | |
| Description | NTEXT |
4. tblOrderDetail (Stores line items for orders)
| Column | Datatype | Constraint |
| OrderID | INT | NOT NULL |
| ProductID | INT | NOT NULL |
| Quantity | INT | |
| Price | MONEY |
Part 3: Indexing and Table Modification
Create Index: Create a Clustered Index named
CI_tblUser_UserIDon theUserIDcolumn of thetblUsertable.Drop Index: Remove the clustered index
CI_tblUser_UserIDthat you just created.Modify Table: Alter the
tblUsertable to add a new column namedBirthDatewith theDATETIMEdatatype.
Part 4: Constraints
Implement the following constraints to ensure data integrity:
Primary Keys:
PK_tblUser:UserIDintblUserPK_tblOrder:OrderIDintblOrderPK_tblProduct:ProductIDintblProductPK_tblOrderDetail: Composite key of (OrderID,ProductID) intblOrderDetail
Foreign Keys:
FK_tblOrder_tblUser:UserIDintblOrderreferencingUserIDintblUser.FK_tblOrderDetail_tblOrder:OrderIDintblOrderDetailreferencingOrderIDintblOrder.FK_tblOrderDetail_tblProduct:ProductIDintblOrderDetailreferencingProductIDintblProduct.
Default: Set a default value of
GETDATE()for theOrderDatecolumn intblOrder.Check: Ensure
OrderDateintblOrderis between'2000-01-01'and the current date.Unique: Ensure the
UserNameintblUseris unique.
Part 5: Data Insertion
Populate the tables with the records provided in the sample data tables (refer to original images for specific names and dates for Users, Orders, Products, and OrderDetails).
Part 6: Query Operations
Update: Apply a 10% discount to the
Priceof all records intblProductwhere theProductIDis 3.Select: Display records from all four tables joined together, including:
UserID,UserName,OrderID,OrderDate,Quantity,Price, andProductName.
Part 7: Views
Create a view named
view_Top2Productto display the top 2 best-selling products based on the total quantity sold. The view should show:ProductID,ProductName,Price, andTotalQuantity.
Part 8: Stored Procedures
Create a procedure named
sp_TimSanPhamwith the following parameters:@GiaMua(MONEY): The maximum price to filter products.@count(INT, OUTPUT): To return the total number of records found.
The procedure should return a list of products where
Price <= @GiaMuaand print the count of products found.Execute the procedure with
@GiaMua = 50.
Part 9: Triggers
Trigger 1: Create
TG_tblProduct_Updateon thetblProducttable forUPDATEevents. If the updatedPriceis less than 10, the update should fail, roll back, and print the message: "You don’t update price less than 10!"Drop Trigger: Drop the
TG_tblProduct_Updatetrigger.Trigger 2: Create
TG_tblUser_Updateon thetblUsertable forUPDATEevents. If a user attempts to modify theUserNamecolumn, the update should fail and print: "You don’t update this column UserName!"
