Database
Database
 Plagiarism
 Plagiarism is a particular form of cheating. Plagiarism must be avoided at all costs and students who break the rules, however innocently, may be
 penalised. It is your responsibility to ensure that you understand correct referencing practices. As a university level student, you are expected to use
 appropriate references throughout and keep carefully detailed notes of all your sources of materials for material you have used in your work,
 including any material downloaded from the Internet. Please consult the relevant unit lecturer or your course tutor if you need any further advice.
 Student Declaration
 I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I declare that the work
 submitted for assessment has been carried out without assistance other than that which is acceptable according to the rules of the specification. I
 certify I have clearly referenced any sources and any artificial intelligence (AI) tools used in the work. I understand that making a false declaration
 is a form of malpractice.
Student’s signature
Grading grid
P1   P2   P3   P4   P5   M1   M2   M3   M4   M5   D1   D2   D3
 Summative Feedback:                             Resubmission Feedback:
                                               Customer
                       PK          ID_Customer            char(10)
                                   Name_Customer          varchar(50)
                                   Sex                    nvarchar(50)
                                   Address                nvarchar(50)
                                   PHONE                  varchar(50)
                                        Figure 1 : Customer table
        Staff Table
                                                STAFF
                       PK           ID_Staff                      char(10)
                                    Name_Staff                    varchar(50)
                                    Sex                           nvarchar(50)
                                    Phone                         varchar(15)
                                    Address                       nvarchar(50)
                                             Figure 2 : Staff
        Product Table
                                                 Product
                         PK         ID_Product                    char(10)
                                    Name_Product                  varchar(50)
                                    ID_Supplier                   char(10)
                                    Quantity                      int
                                    Price                         int
                                            Figure 3 : Product
        In addition, I also created 3 more tables, Import_Invoice, Supplier and Sales_Invoice, for easy
         use and application management.
        Import_Invoice Table
                                              Import_Invoice
                          PK          ID_Invoice                     char(10)
                                      ID_Staff                       char(10)
                                      ID_Product                     char(10)
                                      ID_Supplier                  char(10)
                                      Quantity                     Int
                                      Date_of_Sale                 date
                                      Address                      varchar(50)
                                      PHONE                        varchar(15)
                                      Unit_Price                   float
                                      Total_Money                  float
                                           Figure 5 : Import_Invoice
 Supplier Table
                                                   Supplier
                           PK           ID_Supplier                    char(10)
                                        Name_Supplier                  varchar(50)
                                        Address                        nvarchar(50)
                                        PHONE                          varchar(15)
                                                Figure 6 : Supplier
        Sales_Invoice Table
                                                   Sales_Invoice
                           PK           ID_Sales_Invoice               char(10)
                                        ID_Staff                       char(10)
                                        ID_Product                     char(10)
                                        ID_Customer                    char(10)
                                        Quantity                       Int
                                        Date_of_Sale                   date
                                        Address                        varchar(50)
                                        PHONE                          varchar(15)
                                        Unit_Price                     float
                                        Total_Money                    float
1.3 Draw an Entity-Relationship Diagram (ERD) to illustrate the entities and the
relationships between them:
        An Entity Relationship Diagram (ERD) is a fundamental tool in database design, serving as a
         structural representation of an organization's data. It visually depicts the relationships between
        "entities"—which can include people, objects, events, or concepts—and how they interact
        within the system.
       The key purposes of an ERD include:
        Database Design: Providing a clear, visual foundation for designing databases by identifying
         entities, their attributes, and the relationships between them.
        Requirement Definition: Helping to capture and define the data requirements of an
         organization's information system.
        Collaboration Tool: Facilitating communication among stakeholders, including developers,
         analysts, and business teams, to ensure a shared understanding of system requirements.
        Problem Identification: Identifying potential gaps or inconsistencies in the data model before
         implementation.
                                           Figure 7 : ERD
           Physical design: The design adapted to the RDBMS and system constraints and features. After
            we design the logical of user requirement, we implementing the physical design by SQL server.
1.4 Design the tables needed in the database, the columns corresponding to
the attributes, and the data type for each column.
 CUSTOMER :
                                              Figure 8 : CUSTOMER
            PRODUCT :
                                               Figure 9 : PRODUCT
            STAFF :
                                                Figure 10 : STAFF
   Import_Invoice:
Figure 11 : Import_Invoice
 Sales_Invoice
                      Figure 12 :   Sales_Invoice
   Supplier
1.5 Set up primary keys and foreign keys to ensure data integrity.
          Entities and Attributes:
          Supplier
          o Entity Description: Represents suppliers who provide products to the store.
          o Attributes:
             ID_Supplier (CHAR(10)): Primary key, unique identifier for each supplier.
             Name_Supplier (VARCHAR(50)): The name of the supplier.
             Address (NVARCHAR(50)): The address of the supplier.
             PHONE (VARCHAR(15)): The contact number of the supplier
          Product
          o Entity Description: Represents products available for sale or purchase in the store.
          o Attributes:
             ID_Product (CHAR(10)): Primary key, unique identifier for each product.
             Name_Product (VARCHAR(50)): The name of the product.
             ID_Supplier (CHAR(10)): Foreign key referencing Supplier(ID_Supplier), indicating
                 which supplier provides the product.
             Quantity (INT): The available stock quantity of the product.
             Price (INT): The price of the product.
          Staff
          o Entity Description: Represents the employees of the store.
          o Attributes:
             ID_Staff (CHAR(10)): Primary key, unique identifier for each staff member.
             Name_Staff (VARCHAR(50)): The name of the staff member.
             Sex (NVARCHAR(50)): The gender of the staff member.
             Address (NVARCHAR(50)): The address of the staff member.
             PHONE (VARCHAR(15)): The contact number of the staff member.
          Customer
          o Entity Description: Represents customers who make purchases.
          o Attributes:
             ID_Customer (CHAR(10)): Primary key, unique identifier for each customer.
             Name_Customer (VARCHAR(50)): The name of the customer.
     Sex (NVARCHAR(50)): The gender of the customer.
     Address (NVARCHAR(50)): The address of the customer.
    o PHONE (VARCHAR(15)): The contact number of the customer
  Import_Invoice
  o Entity Description: Represents invoices for importing products into inventory.
     Attributes:
     ID_Invoice (CHAR(10)): Primary key, unique identifier for each import invoice.
     ID_Staff (CHAR(10)): Foreign key referencing Staff(ID_Staff), indicating the staff
        member handling the import.
     ID_Product (CHAR(10)): Foreign key referencing Product(ID_Product), indicating the
        product being imported.
     ID_Supplier (CHAR(10)): Foreign key referencing Supplier(ID_Supplier), indicating the
        supplier of the imported product.
     Quantity (INT): Quantity of the product being imported.
     Date_of_Sale (DATE): Date of the import transaction.
     Address (VARCHAR(50)): Address for the delivery or transaction.
     PHONE (VARCHAR(15)): Contact number related to the transaction.
     Unit_Price (FLOAT): The unit price of the imported product.
     Total_Money (FLOAT): The total value of the import transaction.
  Sales_Invoice
  o Entity Description: Represents invoices for products sold to customers.
  o Attributes:
     ID_Sales_Invoice (CHAR(10)): Primary key, unique identifier for each sales invoice.
     ID_Staff (CHAR(10)): Foreign key referencing Staff(ID_Staff), indicating the staff
        member handling the sale.
     ID_Customer (CHAR(10)): Foreign key referencing Customer(ID_Customer), indicating
        the customer making the purchase.
     ID_Product (CHAR(10)): Foreign key referencing Product(ID_Product), indicating the
        product being sold.
     Quantity (INT): Quantity of the product being sold.
     Date_of_Sale (DATE): Date of the sale transaction.
     Address (VARCHAR(50)): Address for the delivery or transaction.
     PHONE (VARCHAR(15)): Contact number related to the transaction.
     Unit_Price (FLOAT): The unit price of the product sold.
     Total_Money (FLOAT): The total value of the sales transaction.
 Relationships
  Supplier ↔ Product: A supplier provides products (Product.ID_Supplier references
   Supplier.ID_Supplier).
  Staff ↔ Import_Invoice: Staff handle imports (Import_Invoice.ID_Staff references
   Staff.ID_Staff).
             Product ↔ Import_Invoice: Products are imported via invoices (Import_Invoice.ID_Product
              references Product.ID_Product).
             Customer ↔ Sales_Invoice: Customers make purchases (Sales_Invoice.ID_Customer
              references Customer.ID_Customer).
             Staff ↔ Sales_Invoice: Staff process sales (Sales_Invoice.ID_Staff references Staff.ID_Staff).
             Product ↔ Sales_Invoice: Products are sold via invoices (Sales_Invoice.ID_Product references
              Product.ID_Product).
1.5.1 Supplier - Product
        Relationship: 1-N (One supplier supplies many products).
        Foreign Key: The ID_Supplier column in the Product table is a foreign key that references the
         ID_Supplier column in the Supplier table.
       Action:
        ON DELETE CASCADE: Deleting a supplier will automatically delete related products.
        ON UPDATE CASCADE: If the ID_Supplier is updated, the related products will be
          automatically updated.
1.5.2 Staff - Import_Invoice:
        Relationship: 1-N (One staff can create multiple import invoices).
        Foreign Key: The ID_Staff column in the Import_Invoice table is a foreign key that references
         the ID_Staff column in the Staff table.
       Action:
        ON DELETE CASCADE: Deleting a staff will delete the import invoices that the staff
          has created.
        ON UPDATE CASCADE: If the ID_Staff is updated, the related invoices will be updated
          accordingly.
1.5.3 Supplier - Import_Invoice
        Relationship: 1-N (A supplier can be associated with multiple import invoices).
        Foreign Key: ID_Supplier column in Import_Invoice table references ID_Supplier in Supplier
         table.
       Action:
        ON DELETE NO ACTION: Do not allow deleting supplier if there is a related import invoice.
        ON UPDATE NO ACTION: Do not allow updating ID_Supplier if there is a related import
          invoice.
1.5.4 Product - Import_Invoice
           Relationship: 1-N (A product can be imported through multiple import invoices).
           Foreign Key: ID_Product column in Import_Invoice table references ID_Product in Product table.
           Action:
           ON DELETE CASCADE: Deleting product will delete related import invoices.
             ON UPDATE CASCADE: If ID_Product is updated, related import invoices will be updated
              automatically..
1.5.5 Staff - Sales_Invoice
           Relationship: 1-N (One employee can create multiple sales invoices).
           Foreign Key: ID_Staff column in Sales_Invoice table references ID_Staff in Staff table.
           Action:
           ON DELETE CASCADE: Deleting employee will delete related sales invoices.
           ON UPDATE CASCADE: If ID_Staff is updated, related sales invoices will be updated
             automatically..
1.5.6 Customer - Sales_Invoice
        Relationship: 1-N (A customer can purchase multiple sales invoices).
        Foreign Key: The ID_Customer column in the Sales_Invoice table references the ID_Customer
         in the Customer table.
       Action:
        ON DELETE CASCADE: Deleting a customer will delete the related sales invoices.
        ON UPDATE CASCADE: If ID_Customer is updated, the related sales invoices will be
          automatically updated..
1.5.7 Product - Sales_Invoice
        Relationship: 1-N (A product can appear in multiple sales invoices).
        Foreign Key: The ID_Product column in the Sales_Invoice table references the ID_Product in
         the Product table.
       Action:
        ON DELETE CASCADE: Deleting a product will delete the related sales invoices.
        ON UPDATE CASCADE: If ID_Product is updated, the related sales invoices will be
          automatically updated.
1.6 Normalize the database to eliminate duplicate data and ensure data consistency.
1.6.1 Steps for Normalization:
             First Normal Form (1NF):
             Ensure that each column contains only atomic (indivisible) values.
             Ensure that there are no repeating groups of data.
             Second Normal Form (2NF):
             Eliminate partial dependencies by ensuring that all non-key attributes are fully functionally
              dependent on the primary key.
             This applies to tables with composite primary keys.
             Third Normal Form (3NF):
             Remove transitive dependencies, ensuring that non-key attributes are only dependent on the
              primary key.
1.6.2 Revised Schema (Normalized)
        Table: Supplier
        Attributes:
        o ID_Supplier (Primary Key)
        o Name_Supplier
        o Address
        o Phone
        o No changes needed; this table is already normalized.
       Table: Product
        Attributes:
        o ID_Product (Primary Key)
        o Name_Product
        o ID_Supplier (Foreign Key)
        o Price
        o Remove Quantity from this table, as it is a transactional attribute (specific to invoices).
       Table: Staff
        Attributes:
        o ID_Staff (Primary Key)
        o Name_Staff
        o Sex
        o Address
        o Phone
        o No changes needed; this table is already normalized.
       Table: Customers
        Attributes:
        o ID_Customer (Primary Key)
        o Name_Customer
        o Sex
        o Address
        o Phone
        o No changes needed; this table is already normalized.
       Table: Import_Invoice
        Attributes:
        o ID_Invoice (Primary Key)
        o Date_of_Import
        o ID_Staff (Foreign Key)
        o ID_Supplier (Foreign Key)
        o Remove ID_Product, Quantity, Unit_Price, and Total_Money as they belong to a line-item
           table.
       Table: Import_Invoice_Line
        Attributes:
        o ID_Invoice (Composite Primary Key, Foreign Key to Import_Invoice)
        o ID_Product (Composite Primary Key, Foreign Key to Product)
        o Quantity
         o Unit_Price
         o Total_Money (can be derived as Quantity * Unit_Price, optionally stored for performance)
        Table: Sales_Invoice
         Attributes:
         o ID_Sales_Invoice (Primary Key)
         o Date_of_Sale
         o ID_Staff (Foreign Key)
         o ID_Customer (Foreign Key)
         o Remove ID_Product, Quantity, Unit_Price, and Total_Money as they belong to a line-item
           table.
        Table: Sales_Invoice_Line
         Attributes:
         o ID_Sales_Invoice (Composite Primary Key, Foreign Key to Sales_Invoice)
         o ID_Product (Composite Primary Key, Foreign Key to Product)
         o Quantity
         o Unit_Price
         o Total_Money (can be derived as Quantity * Unit_Price, optionally stored for performance)
1.7 Design the table about user requirements and system requirements for the database
     Requirement                                        Description
        Type
   General         Build software to support Store X in effectively managing operations, including sales,
   Requirements       inventory, and employee management.
Software           Develop using .Net Framework 4.5 (C#) or later versions, with SQL Server 2012 or
   Platform           higher for data storage.
   Product         Enable comprehensive product catalog management, including product code, name,
   Management         price, inventory, and images. Support functionality for adding, editing, deleting,
                      searching, and filtering products. Update inventory during import/export
                      processes.
 Employee              Manage employee details, including employee code, name, position, and roles.
 Management            Provide functionalities for adding, editing, deleting, and searching employees.
                       Require password updates upon first login and enforce role-based access rights.
 Customer              Maintain customer data, including code, name, phone number, and address.
 Management            Enable functionalities for adding, editing, deleting, and searching customers.
                       Store purchase history for each customer.
 Access Rights         Define access levels: Admins have full access to all functions; sales staff are
                       restricted to sales-related functions; warehouse staff are limited to inventory-
                       related tasks. Employees cannot add/delete staff or access statistics unless
                       authorized.
 Security              Use strong encryption for data, securely manage sensitive information, and
 Enhancements          conduct regular updates to ensure compliance with the latest security standards.
                                        Figure 14 : Product
    CUSTOMER :
Figure 15 : Customer
 STAFF
                                          Figure 16 :Staff
    Supplier
                                          Figure 17 : Supplier
 Import_Invoice
                                     Figure 18 : Import_Invoice
       Sales_Invoice
Figure 19 : Sales_Invoice
       After coding and executing the commands, this is the result we obtain.
   Upon creation, the Microsoft SQL Server Management system has successfully generated
    tables, views, security configurations, and full storage, thereby completing the system.
   Here are the results after we have created the tables.
   Querying across multiple tables: We have the affiliations and relationships of the copies with
    each other after constructing the appropriate components in the database
1.2 Creating Relationships
          Supplier → Product: 1-N Relationship
          Supplier can supply many Products.
          A Product is linked to a Supplier via ID_Supplier.
1.4 The SQL script you provided is used to add foreign key constraints to the database.
These constraints ensure that data between related tables is consistent and does not lose
integrity.
           Data Integrity:
            Foreign key constraints enforce relationships between tables and ensure that data between
             them remains consistent. For example:
            In the Sales_Invoice table, the ID_Staff column must refer to a valid ID_Staff from the Staff
             table. If the referenced employee is deleted, the ON DELETE CASCADE action ensures that
             all related sales invoices are also deleted, preventing orphan records.
            If the ID_Product in the Sales_Invoice_Line table does not exist in the Product table, an error
             occurs, preventing invalid data from being entered into the database.
           Consistency:
            Referential integrity: Foreign key constraints ensure that each record in the child table (e.g.
             Sales_Invoice_Line, Import_Invoice_Line) always refers to a valid record in the parent table
             (e.g. Product, Import_Invoice).
            Cascading Update/Delete: The ON DELETE CASCADE and ON UPDATE CASCADE
             options ensure that when a record in the parent table is modified (e.g. a Product record) or
             deleted, related records in the child tables are automatically updated or deleted, preventing
             inconsistencies.
           Preventing Anomalies:
            Foreign Key Constraints prevent:
            Orphan Records: A record in a child table (e.g. a sales transaction in Sales_Invoice) will not be
             suspended if the corresponding record in the parent table (e.g. the related Employee or
             Customer) is deleted.
            Invalid Data: Ensures that you cannot enter a Sales_Invoice for a non-existent Product or
             Employee member, applying valid references.
            Example of Cascading Actions:
1.5 Develop a user interface (UI) for users to interact with the database (e.g.,
forms, menus).
          Product Management Form:
          Functions: Add new, Fix, delete, Back
 When you log in to your account, you will go to the App's home page:
        Home:
    o Functions: Information Statistical, Search, Come back and Exit
 In Information Statistical
    o Customer
    o Staff
 Product:
    o Sales_Invoice
2. Implement a query language into the relational database system (P3)
2.1 Assess the suitability of the T-SQL query language for sales software applications.
         T-SQL (Transact-SQL) is an extension of the Structured Query Language (SQL) developed by
         Sybase and Microsoft, offering additional features such as transaction control, error handling,
         row processing, and the use of declared variables.
      All applications interacting with SQL Server communicate by sending T-SQL statements to the
         server. T-SQL queries typically include commands such as the SELECT statement, selecting
         specific columns, labeling output columns, filtering rows, and modifying search conditions.
         T-SQL identifiers are used for all databases, servers, and database objects within SQL Server.
         These identifiers encompass tables, constraints, stored procedures, views, columns, and data
         types. Each identifier must have a unique name, is assigned upon the creation of an object, and
         serves to identify that object.
      Why T-SQL is suitable for the above sales application (scenario)?
        Relational Database Management System (RDBMS) Integration
        o Sales applications typically require a relational database to handle structured data
            efficiently. T-SQL is specifically built for relational databases like Microsoft SQL Server,
            which is designed to manage structured data across multiple related tables.
        o The sales application involves multiple related entities (e.g., customers, products, sales
            invoices, staff, suppliers), and T-SQL can easily manage the relationships between these
            tables using foreign keys and join operations.
        Data Integrity and Consistency
        o T-SQL supports constraints like primary keys, foreign keys, and unique constraints, which
            are essential for ensuring referential integrity in a sales application.
        o The schema for the sales application uses foreign key constraints (as demonstrated earlier) to
            ensure that related records (e.g., sales invoices, customer data) are consistent. This helps
            prevent orphaned data or invalid references (such as a sale being associated with a non-
            existent product or customer).
          Security Features
          o Sales applications often require strict control over who can access and modify data. T-SQL
            offers role-based security, which allows you to control user access and permissions based on
            roles (e.g., sales staff, inventory manager).
          o Using T-SQL security features, the application can ensure that only authorized personnel can
            view sensitive customer data or modify inventory records.
2.2 Implement data connection and execute queries to fulfill software functionalities.