MySQL
SQL DDL ASSIGNMENT1 - Company Management System
Objective:
You are required to address a set of Data Definition Language (DDL) tasks for a Company Management System. Each query focuses on distinct DDL tasks, including the creation of tables, application of constraints, and establishment of relationships between tables.
Requirements:
You will need to complete at least 25 different DDL tasks, covering table creation, primary keys, foreign keys, constraints, and indexes. Each task represents a key part of designing the structure of the Company Management System.
Question 1: Create the DEPARTMENT Table
Write a DDL SQL statement to create a DEPARTMENT table with the following requirements:
department_idas a primary key with auto-increment functionality.department_nameas a string of maximum length 255 characters, not null.locationas a string.created_atas a datetime field, default it to system timestamp, not null.
Question 2: Create the EMPLOYEE Table with Constraints
Create an EMPLOYEE table with the following specifications:
employee_idas the primary key with auto-increment functionality.first_nameas a variable character string, not to exceed 100 characters, not null.last_nameas a string with a maximum of 100 characters.hire_dateas a date field, not null.salaryas a decimal with a maximum precision of 10, 2.- Add a
CHECKconstraint onsalaryto ensure it is greater than 0. yearly_leave_countas a number.created_atas a datetime field, not null.
Question 3: Create the PRODUCT Table with a CHECK Constraint
Write a DDL SQL statement to create a PRODUCT table:
product_idas the primary key with auto-increment functionality.product_nameas a string of maximum length 255 characters, not null.priceas a decimal, not null.- Add a
CHECKconstraint to ensure the price is greater than 0. product_categoryas string, not null.order_levelas numbercurrent_quantityas numbercreated_atas a datetime field, default it to system timestamp, not null.
Question 4: Create the CLIENT Table
Define a CLIENT table:
client_idas a primary key with auto-increment functionality.client_nameas a string with a maximum length of 255 characters, not null.emailas a string that must be unique and cannot be null.created_atas a datetime field, default it to system timestamp, not null.
Question 5: Create the SUPPLIER Table
Write a DDL SQL statement to create a SUPPLIER table with the following details:
supplier_idas the primary key with auto-increment functionality.supplier_nameas a string of maximum length 255 characters, not null.contact_numberas a string.- Ensure that the
contact_numberis unique across all suppliers. created_atas a datetime field, default it to system timestamp, not null.
Question 6: Create the PURCHASE_ORDER Table
Write a DDL SQL statement to create a PURCHASE_ORDER table with the following details:
order_idas the primary key with auto-increment functionality.order_dateas a date field, not null.total_amountas a decimal field, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 7: Create the PURCHASE_ORDER_DETAIL Table
Write a DDL SQL statement to create a PURCHASE_ORDER_DETAIL table with the following details:
order_detail_idas the primary key with auto-increment functionality.quantityas a number field, not null.unit_amountas a decimal field, not null.total_amountas a decimal field, not null.product_idas a foreign key, not null.order_idas a foreign key referencing the PURCHASE_ORDER table.
Question 8: Create the PROJECT Table
Define a PROJECT table with the following details:
project_idas the primary key with auto-increment functionality.project_nameas a string with a maximum length of 255 characters, not null.start_dateas a date field, not null.end_dateas a date field.- Add a
CHECKconstraint to ensureend_dateis greater thanstart_date. active_statusas boolean, default to true.created_atas a datetime field, default it to system timestamp, not null.
Question 9: Define the PROJECT_PRODUCT_USED Relationship Table
Write the SQL statement to create an PROJECT_PRODUCT_USED table that tracks which employees are working on which projects. Include:
project_idas a foreign key, not null.product_idas a foreign key, not null.quantityas a number, not null.created_atas a datetime field, default to system timestamp, not null.- Set up a composite primary key using both columns.
Question 10: Define the EMPLOYEE_PROJECT Relationship Table
Write the SQL statement to create an EMPLOYEE_PROJECT table that tracks which employees are working on which projects. Include:
project_idas a foreign key, not null.employee_idas a foreign key, not null.taskas a string.task_due_dateas a date.created_atas a datetime field, default it to system timestamp, not null.
Question 11: Define the CONTRACT Table with Foreign Keys
Write the SQL statement to create a CONTRACT table that includes:
contract_idas an auto-incrementing primary key.contract_dateas a date, not null.amountas a decimal (12, 2), not null.client_idas a foreign key, not null.project_idas a foreign key, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 12: Create the INVOICE Table with Constraints
Define an INVOICE table with the following specifications:
invoice_idas the primary key with auto-increment functionality.project_idas the foreign key, not null.invoice_dateas a date, with the default value being the current date.amount_dueas a decimal (12, 2), not null.statusas paid or unpaid- Add a
CHECKconstraint to ensure theamount_dueis greater than 0. created_atas a datetime field, default it to system timestamp, not null.
Question 13: Create the RECEIVABLE_TRANSACTION Table with a CHECK Constraint
Write a DDL SQL statement to create a PAYABLE_TRANSACTION table:
receivable_transaction_idas the primary key with auto-increment functionality.descriptionas a string of maximum length 255 characters.amountas a decimal, not null.payment_dateas a timestamp, not null.contract_invoice_idas the foreign key to INVOICE table, not null.- Add a
CHECKconstraint to ensure the amount is greater than 0.
Question 14: Create the PAYABLE_TRANSACTION Table with a CHECK Constraint
Write a DDL SQL statement to create a PAYABLE_TRANSACTION table:
payable_transaction_idas the primary key with auto-increment functionality.descriptionas a string of maximum length 255 characters.amountas a decimal, not null.payment_dateas a timestamp, not null.purchase_order_idas the foreign key to PURCHASE_ORDER table, not null.- Add a
CHECKconstraint to ensure the amount is greater than 0.
Question 15: Create the TIMESHEET Table with Defaults
Create a TIMESHEET table:
timesheet_idas the primary key with auto-increment functionality.employee_idas a foreign key, not null.dateas a date field, defaulted to the current date.hours_workedas a time value, not null.project_idas a foreign key, not null.created_atas a datetime field, default it to system timestamp, not null.
Question 16: Create the EMPLOYEE_LEAVES Table
Define an EMPLOYEE_LEAVES table to track system changes:
leave_idas the primary key with auto-increment functionality.employee_idas a foreign key, not null.start_dateas a date field, not null.end_dateas a date field, not null.reasonas a string.approved_employee_idas a foreign key referencing the EMPLOYEE table.created_atas a datetime field, default it to system timestamp, not null.
Question 17: Create the AUDIT_LOG Table
Define an AUDIT_LOG table to track system changes:
log_idas the primary key with auto-increment functionality.log_dateas a date field, not null.actionas a string to describe the action performed.employee_idas a foreign key referencing the EMPLOYEE table.
Question 18: Create the EMPLOYEE_LOGIN Table
Write a DDL SQL statement to create an EMPLOYEE_LOGIN table (one-to-one relationship with employee table):
employee_idas a foreign key referencing the EMPLOYEE table.login_idcan use alpha numeric login id or email as login id, not null.passwordmust encrypt the password before storing it in the database table.active_flagas boolean, 0 or 1, you can lock the customer login when required, not null.last_login_datetimeas a timestamp, last successful login date for a given customer.
Question 19: Add a Column to the EMPLOYEE Table
Write a SQL statement to add a phone_number column of type VARCHAR(15) to the EMPLOYEE table.
Question 20: Alter a Column in the CLIENT Table
Write a SQL statement to alter the email column in the CLIENT table, increasing its length to 255 characters.
Question 21: Drop a Column from the PAYABLE_TRANSACTION Table
Write a SQL statement to drop the description column from the PAYABLE_TRANSACTION table.
Question 22: Rename the AUDIT_LOG Table
Write a SQL statement to rename the AUDIT_LOG table to AUDIT_LOGS.
Question 23: Drop the EMPLOYEE_LOGIN Table
Write a SQL statement to drop the ** EMPLOYEE_LOGIN** table.
Question 24: Add a Primary Key on the EMPLOYEE_PROJECT Table
- Set up a composite primary key using
project_idandemployee_id.
Question 25: Add a Foreign Key column to the EMPLOYEE Table
Add a foreign key in the EMPLOYEE table to reference the DEPARTMENT table.
Question 26: Alter the PURCHASE_ORDER Table to Add a Foreign Key
Write a SQL statement to add a supplier_id foreign key to the PURCHASE_ORDER table, referencing the SUPPLIER table.
Question 27: Add a CHECK Constraint on the PURCHASE_ORDER Table
Write a SQL statement to add a CHECK constraint to the PURCHASE_ORDER table, ensuring that the total_amount is greater than 0.
Question 28: Add a UNIQUE Constraint on the PROJECT Table
Write a SQL statement to add a UNIQUE constraint to the PROJECT table ensuring that the project_name is not repeated.
Question 29: Add a default Constraint on the EMPLOYEE Table
Write a SQL statement to add a current system timestamp DEFAULT constraint to the EMPLOYEE table on created_at column.
Question 30: Add Indexes to the EMPLOYEE and PROJECT Tables
Write SQL statements to:
- Add an index on the
last_namecolumn in the EMPLOYEE table. - Add an index on the
project_namecolumn in the PROJECT table.
Question 31: Drop an Index from the EMPLOYEE Table
Write a SQL statement to drop the index on the last_name column in the EMPLOYEE table.
Question 32: Enforce UNIQUE constraints on all applicable tables.
Apply UNIQUE constraints to columns across the entire database wherever duplicate data is not permitted.
GOOD LUCK WITH YOUR ASSIGNMENT!!!
Don't forget to contact us if you need any further assistance with your assignments, and most importantly, for a manual review and approval of your work.
Sample ERD Data Model for Company Management System

To gain complete access, login with gmail or outlook, no need of signup, click here


Comments Not Found