Updated the SQL script to work better with Entity Framework and to eliminate issues with naming objects in the generated C# code such as "AdItem1". Updated the primary key fields to auto-increment their values and forced a NOT NULL constraint on the foreign keys where needed.

This commit is contained in:
2017-03-22 04:08:39 -05:00
parent c131dd2eb4
commit f554d37fb1
@@ -1,8 +1,14 @@
--File Version: 1.0.0.0 for SQL Server --File Version: 1.2.0.0 for SQL Server
--Purpose: Creates an empty database template in SQL for the Advertising Profit Control software; master template. --Purpose: Creates an empty database template in SQL for the Advertising Profit Control software; master template.
--Notes: This version does not implement any table relationships, its just flat data tables. --Notes: This version does not implement any table relationships, its just flat data tables.
-- Currency will be represented with DECIMAL(19,2) as the standard. From stackoverflow: -- Currency will be represented with DECIMAL(19,2) as the standard. From stackoverflow:
-- http://stackoverflow.com/questions/628637/best-data-type-for-storing-currency-values-in-a-mysql-database -- http://stackoverflow.com/questions/628637/best-data-type-for-storing-currency-values-in-a-mysql-database
-- Percentages will be stored as DECIMAL(6,6) to allow a max of six characters with 5 characters following the decimal point.
-- Reference for descision: http://stackoverflow.com/questions/7205697/sql-which-data-type-represents-percentages-well
-- Also a good one to view: http://stackoverflow.com/questions/2762302/appropriate-datatype-for-holding-percent-values
--Versions:
-- 1.0.0.0: Initial script version.
-- 1.2.0.0: Changed column names that produced duplicate names when C# code is generated by Entity Framework (i.e. Aditem becomes AdItem1).
--Create the database called "AdvertisingProfitControl". --Create the database called "AdvertisingProfitControl".
CREATE DATABASE AdvertisingProfitControl CREATE DATABASE AdvertisingProfitControl
@@ -10,29 +16,29 @@ CREATE DATABASE AdvertisingProfitControl
--Begin creating the tables for the database. --Begin creating the tables for the database.
CREATE TABLE WeekEndingDates CREATE TABLE WeekEndingDates
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
WeekEndingDate DATE NOT NULL --A date is required to be entered. EndingDate DATE NOT NULL --A date is required to be entered.
) )
CREATE TABLE AdItems CREATE TABLE AdItems
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
AdItem VARCHAR(255) NOT NULL, Name VARCHAR(256) NOT NULL,
Description VARCHAR(255) --Optional field may be used later one. Description VARCHAR(256) --Optional field may be used later one.
) )
CREATE TABLE AdSpecials CREATE TABLE AdSpecials
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
AdSpecial VARCHAR(255) NOT NULL, Name VARCHAR(256) NOT NULL,
Description VARCHAR(255) --Optional field may be used later one, most likely in a tool tip. Description VARCHAR(256) --Optional field may be used later one, most likely in a tool tip.
) )
CREATE TABLE Projections CREATE TABLE Projections
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--Sold and sale price need to be stored as a string since they need to support # Bin(s) --Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting respectively. --and $#.##/# formatting.
Sold VARCHAR(128), Sold VARCHAR(128),
SalePrice VARCHAR(128), SalePrice VARCHAR(128),
--Might be over kill to store money like this but better safe then sorry. --Might be over kill to store money like this but better safe then sorry.
@@ -45,35 +51,37 @@ RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute. --A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps? RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys. --Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id), FkAdItemId INTEGER NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), FkAdSpecialId INTEGER NOT NULL,
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) --FkAdSpecialId INTEGER NOT NULL FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE Inventory CREATE TABLE Inventory
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--All values minus the primary key and foreign keys are set as strings to allow --All values minus the primary key and foreign keys are set as strings to allow
--specifying the number of bins of product are present. --specifying the number of bins of product are present.
BeginningInventory VARCHAR(255), BeginningInventory VARCHAR(256),
Recieved VARCHAR(255), Recieved VARCHAR(256),
TotalInventory VARCHAR(255), TotalInventory VARCHAR(256),
EndingInventory VARCHAR(255), EndingInventory VARCHAR(256),
--The attributes of the row can not be null, and neither can the foreign keys. --The attributes of the row can not be null, and neither can the foreign keys.
RowPosition INTEGER NOT NULL, RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute. --A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps? RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys. --Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id), FkAdItemId INTEGER NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), FkAdSpecialId INTEGER NOT NULL,
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) --FkAdSpecialId INTEGER NOT NULL FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE ActualSales CREATE TABLE ActualSales
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--Sold and sale price need to be stored as a string since they need to support # Bin(s) --Sold and sale price need to be stored as a string since they need to support # Bin(s)
--and $#.##/# formatting respectively. --and $#.##/# formatting.
Sold VARCHAR(128), Sold VARCHAR(128),
SalePrice VARCHAR(128), SalePrice VARCHAR(128),
--Might be over kill to store money like this but better safe then sorry. --Might be over kill to store money like this but better safe then sorry.
@@ -86,42 +94,43 @@ RowPosition INTEGER NOT NULL,
--A precision of two digits should be everything we need for the row attribute. --A precision of two digits should be everything we need for the row attribute.
RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps? RowAttribute INTEGER NOT NULL, --Rename to "HeaderId"? Point to the header group's ID number perhaps?
--Now begin all the foreign keys. --Now begin all the foreign keys.
FkAdItemId INTEGER FOREIGN KEY REFERENCES AdItems(Id), FkAdItemId INTEGER NOT NULL FOREIGN KEY REFERENCES AdItems(Id),
FkAdSpecialId INTEGER FOREIGN KEY REFERENCES AdSpecials(Id), FkAdSpecialId INTEGER NOT NULL,
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) --FkAdSpecialId INTEGER NOT NULL FOREIGN KEY REFERENCES AdSpecials(Id), --Can be NULL
FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE Suppliers CREATE TABLE Suppliers
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Supplier VARCHAR(255), Name VARCHAR(256),
Description VARCHAR(255) --Optional field may be used later one. Description VARCHAR(256) --Optional field may be used later one.
) )
CREATE TABLE Invoices CREATE TABLE Invoices
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
InvoiceDate DATE NOT NULL, InvoiceDate DATE NOT NULL,
InvoiceNumber INTEGER NOT NULL, InvoiceNumber INTEGER NOT NULL,
InvoiceNetAmountAtCost DECIMAL(19,2), InvoiceNetAmountAtCost DECIMAL(19,2),
InvoiceNetAmount DECIMAL(19,2), InvoiceNetAmount DECIMAL(19,2),
InvoiceNote VARCHAR(255), InvoiceNote VARCHAR(256),
--Keys are never allowed to be null. --Keys are never allowed to be null.
FkSupplierId INTEGER FOREIGN KEY REFERENCES Suppliers(Id), FkSupplierId INTEGER NOT NULL FOREIGN KEY REFERENCES Suppliers(Id),
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE Notes --Comment is a reserved word so the table and column had to be renamed to "Note" instead. CREATE TABLE Notes --Comment is a reserved word so the table and column had to be renamed to "Note" instead.
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
--The current version (1.0.0.0) only allows the user to enter a max of 256 characters into the comments box. --As of version 1.0.0.0, the user can only enter a max of 256 characters into the comments box.
Note VARCHAR(256) NOT NULL, Remark VARCHAR(256) NOT NULL,
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE WeeklySales CREATE TABLE WeeklySales
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Sunday DECIMAL(19,2), Sunday DECIMAL(19,2),
Monday DECIMAL(19,2), Monday DECIMAL(19,2),
Tuesday DECIMAL(19,2), Tuesday DECIMAL(19,2),
@@ -131,12 +140,12 @@ Friday DECIMAL(19,2),
Saturday DECIMAL(19,2), Saturday DECIMAL(19,2),
TotalSales DECIMAL(19,2), TotalSales DECIMAL(19,2),
--Keys are never allowed to be null. --Keys are never allowed to be null.
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE Taxable CREATE TABLE Taxable
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
Sunday DECIMAL(19,2), Sunday DECIMAL(19,2),
Monday DECIMAL(19,2), Monday DECIMAL(19,2),
Tuesday DECIMAL(19,2), Tuesday DECIMAL(19,2),
@@ -146,7 +155,7 @@ Friday DECIMAL(19,2),
Saturday DECIMAL(19,2), Saturday DECIMAL(19,2),
Total DECIMAL(19,2), Total DECIMAL(19,2),
--Keys are never allowed to be null. --Keys are never allowed to be null.
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
--This table contains items that didn't fit in the other tables. --This table contains items that didn't fit in the other tables.
@@ -154,17 +163,18 @@ FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id)
--advertising profit control sheet (the Weekly Inventory Control). --advertising profit control sheet (the Weekly Inventory Control).
CREATE TABLE CostAnalysis CREATE TABLE CostAnalysis
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
SalesPerManHour DECIMAL(19,2), SalesPerManHour DECIMAL(19,2),
--Allows percentages between %1000.00 (10,000 percent) and %0.1235. --Allows percentages between %1000.00 (10,000 percent) and %0.1235. DECIMAL(x,y) x is the total number of digits
SalaryPercentage DECIMAL(6,2), --The percentage of the manager's salary that was made on this week's sale items. --you want to represent and y is the number of digits to display after the decimal point.
SalaryPercentage DECIMAL(6,6), --The percentage of the manager's salary that was made on this week's sale items.
SalaryDollar DECIMAL(19,2), -- SalaryDollar DECIMAL(19,2), --
Supplies DECIMAL(19,2), --Total of costs from invoices (?). Supplies DECIMAL(19,2), --Total of costs from invoices (?).
FkDateId INTEGER FOREIGN KEY REFERENCES WeekEndingDates(Id) FkDateId INTEGER NOT NULL FOREIGN KEY REFERENCES WeekEndingDates(Id)
) )
CREATE TABLE Version CREATE TABLE Version
( (
Id INTEGER NOT NULL PRIMARY KEY, Id INTEGER IDENTITY PRIMARY KEY, --The IDENTITY key word in SQL tells the engine to create a unique number when a record is added.
VersionNumber VARCHAR(32) --32 characters should be more then enough to store a version number. VersionNumber VARCHAR(32) --32 characters should be more then enough to store a version number.
) )