This is my task list:
Design a star or snowflake schema for a data warehouse that will help answer various business questions regarding sales.
Create a database in SQL Server (this is the data warehouse server) based on the star/snowflake schema you designed.
Load data from the source system i.e. the OLTP database shown earlier to the star/snowflake schema database.
The data from most of the tables in the OLTP database have been prepared in a form of SQL statements. Unfortunately, due to company's policy, you are not allowed to load data from the Products and Customer table 'directly'.
The Customer data has been prepared in CSV format
The Product data can only be retrieved from cloud database in JSON format http://bi.edisonsiow.com/ay1516s2/ca1/getProducts.php
You are required to find the most efficient way, which might include coding, to load data from the sources to your SQL Server.
So far i have done this :
CREATE TABLE Customers (
customerNumber Int NOT NULL,
customerName Varchar(50) NOT NULL,
contactLastName Varchar(50) NOT NULL,
contactFirstName Varchar(50) NOT NULL,
phone Varchar (50) NOT NULL,
addressLine1 Varchar (50) NOT NULL,
addressLine2 Varchar (50) NOT NULL,
city Varchar (50) NOT NULL,
state Varchar (50) NULL,
postalCode Varchar (15) NOT NULL,
country Varchar(50) NOT NULL,
salesRepEmployeeNumber Int NOT NULL,
creditLimit Double NOT NULL,
PRIMARY KEY (customerNumber));
CREATE TABLE Offices (
officeCode Varchar(10) NOT NULL,
city Varchar(50) NOT NULL,
phone Varchar(50) NOT NULL,
addressLine1 Varchar(50) NOT NULL,
addressLine2 Varchar(50) NOT NULL,
state Varchar(50) NULL,
country Varchar(50) NOT NULL,
postalCode Varchar(15) NOT NULL,
territory Varchar(10) NOT NULL,
PRIMARY KEY (officeCode));
CREATE TABLE payments (
customerNumber Int NOT NULL,
checkNumber Varchar(50) NOT NULL,
paymentDate Datetime NOT NULL,
amount DOUBLE NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));
CREATE TABLE OrderDetails (
orderNumber Int NOT NULL,
productCode Varchar(15) NOT NULL,
quantityOrdered Int NOT NULL,
priceEach DOUBLE NOT NULL,
orderLineNumber SMALLINT NOT NULL,
PRIMARY KEY (orderNumber, productCode));
CREATE TABLE ProductLines (
productLine Varchar (50) NOT NULL,
textDescription TEXT NOT NULL,
htmlDescription TEXT NOT NULL,
image BLOB NOT NULL,
PRIMARY KEY (productLine));
CREATE TABLE Orders (
orderNumber Int NOT NULL,
orderDate DateTime NOT NULL,
requiredDate DateTime NOT NULL,
shippedDate DateTime NOT NULL,
status Varchar(15) Not null,
comments TEXT NOT NULL,
customerNumber INT NOT NULL,
Primary key(orderNumber));
ALTER TABLE Orders ADD FOREIGN KEY(customerNumber) REFERENCES Customer(customerNumber);
CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));
CREATE TABLE Products (
productCode Varchar(15) Not Null,
productName Varchar(70) Not Null,
productLine Varchar(50) Not null,
productScale Varchar(10) Not null,
productVendor Varchar(50) Not null,
productDescription TEXT Not null,
quantityinStock Int Not null,
Buy Price Double Not null,
MSRP Double Not null,
Primary key (productCode))
9:28PM
CREATE TABLE payments (
customerNumber Int NOT NULL,
checkNumber Varchar(50) NOT NULL,
paymentDate Datetime NOT NULL,
amount Float NOT NULL,
PRIMARY KEY (customerNumber, checkNumber));
CREATE TABLE Customers (
customerNumber Int NOT NULL,
customerName Varchar(50) NOT NULL,
contactLastName Varchar(50) NOT NULL,
contactFirstName Varchar(50) NOT NULL,
phone Varchar (50) NOT NULL,
addressLine1 Varchar (50) NOT NULL,
addressLine2 Varchar (50) NOT NULL,
city Varchar (50) NOT NULL,
state Varchar (50) NULL,
postalCode Varchar (15) NOT NULL,
country Varchar(50) NOT NULL,
salesRepEmployeeNumber Int NOT NULL,
creditLimit Float NOT NULL,
PRIMARY KEY (customerNumber));
ALTER TABLE Payments ADD FOREIGN KEY(customerNumber) REFERENCES Customers(customerNumber); -- here
CREATE TABLE Offices (
officeCode Varchar(10) NOT NULL,
city Varchar(50) NOT NULL,
phone Varchar(50) NOT NULL,
addressLine1 Varchar(50) NOT NULL,
addressLine2 Varchar(50) NOT NULL,
state Varchar(50) NULL,
country Varchar(50) NOT NULL,
postalCode Varchar(15) NOT NULL,
territory Varchar(10) NOT NULL,
PRIMARY KEY (officeCode));
CREATE TABLE OrderDetails (
orderNumber Int NOT NULL,
productCode Varchar(15) NOT NULL,
quantityOrdered Int NOT NULL,
priceEach Float NOT NULL,
orderLineNumber SMALLINT NOT NULL,
PRIMARY KEY (orderNumber, productCode));
CREATE TABLE ProductLines (
productLine Varchar (50) NOT NULL,
textDescription TEXT NOT NULL,
htmlDescription TEXT NOT NULL,
image Float NOT NULL,
PRIMARY KEY (productLine));
CREATE TABLE Orders (
orderNumber Int NOT NULL,
orderDate DateTime NOT NULL,
requiredDate DateTime NOT NULL,
shippedDate DateTime NOT NULL,
status Varchar(15) Not null,
comments TEXT NOT NULL,
customerNumber INT NOT NULL,
Primary key(orderNumber));
ALTER TABLE OrderDetails ADD FOREIGN KEY(orderNumber) REFERENCES Orders(orderNumber); -- here
CREATE TABLE Employees (
employeeNumber Int Not null,
lastName Varchar(50) Not null,
firstName Varchar(50) Not null,
extension Varchar(10) NOt null,
email Varchar(100) Not null,
officeCode Varchar(10) Not Null,
reportsTo Int Not null,
jobTitle Varchar(50) Not null,
Primary key(employeeNumber));
CREATE TABLE Products (
productCode Varchar(15) Not Null,
productName Varchar(70) Not Null,
productLine Varchar(50) Not null,
productScale Varchar(10) Not null,
productVendor Varchar(50) Not null,
productDescription TEXT Not null,
quantityinStock Int Not null,
BuyPrice Float Not null,
MSRP Float Not null,
Primary key (productCode))
ALTER TABLE OrderDetails ADD FOREIGN KEY(productCode) REFERENCES Products(productCode);
I dont know how to load from source system?
In SQL Server Management Studio, exp[and the server-name, and under databases, find your database.
Right-click it, select Tasks, and then Import Data.
Use the import/export wizard to assist you in importing data from your source data.
Related
I'm reverse engineering a script that creates a payroll database from my textbook. I'm rebuilding it into a library management system instead. To start I've drawn out a diagram of the tables/columns and their relationships here. Now following the script from the textbook I use the authors technique to create all the tables except for the BooksIssued table and InvoiceItems table. This is my code
USE master
GO
/******Check to see if database exists******/
IF DB_ID('SET2133810') IS NOT NULL
DROP DATABASE SET2133810
GO
/******Object: Database SET2133810******/
CREATE DATABASE SET2133810
GO
USE SET2133810
GO
/******Object: table*****/
CREATE TABLE VendorsList(
VendorID int IDENTITY(1,1) NOT NULL,
VendorName varchar(50) NOT NULL,
VendorPhone varchar(50) NULL,
VendorContactLName varchar(50) NULL,
VendorContactFName varchar(50) NULL,
VendorAddress varchar(50) NULL,
VendorCity varchar(50) NOT NULL,
VendorState char(2) NOT NULL,
VendorZipCode varchar(20) NOT NULL,
CONSTRAINT PK_VendorsList PRIMARY KEY CLUSTERED (
VendorID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE InvoicesList(
InvoiceID int IDENTITY(1,1) NOT NULL,
VendorID int NOT NULL,
InvoiceNumber varchar(50) NOT NULL,
InvoiceDate smalldatetime NOT NULL,
InvoiceTotal money NOT NULL,
PaymentTotal money NOT NULL,
PaymentDate smalldatetime NULL,
CONSTRAINT PK_InvoicesList PRIMARY KEY CLUSTERED(
InvoiceID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE BookList(
BookID int IDENTITY(1,1) NOT NULL,
BookISBN varchar(50) NOT NULL,
BookTitle varchar(50) NOT NULL,
BookAuthor varchar(50) NOT NULL,
BookPublisher varchar(50) NOT NULL,
BookGenre varchar(50) NULL
CONSTRAINT PK_BookList PRIMARY KEY CLUSTERED(
BookID ASC
)
)
GO
/******Object: Table*****/
CREATE TABLE MembersList(
MemberID int IDENTITY(1,1) NOT NULL,
MemberLName varchar(50) NOT NULL,
MemberFName varchar(50) NOT NULL,
MemberAddress varchar(50) NULL,
MemberCity varchar(50) NOT NULL,
MemberState char(2) NOT NULL,
MemberZipCode varchar(20) NOT NULL,
MemberPhone varchar(50) NOT NULL,
MemberEmail varchar(50) NOT NULL,
CONSTRAINT PK_MembersList PRIMARY KEY CLUSTERED(
MemberID ASC
)
)
GO
I'm confused on how I would create these tables in the same way? Since they both depend on the other tables and the information thats already in them. I've looked around and found Join statements, but none of them were used in a way that would help me with this problem.
Following should be the order to create tables.
BookList
MemeberList
Book Issued
VendorList
InvoiceList
InvoiceItems
Invoice list shoul be created in last as it have references to vendor and invoice items and BookIssues should be created after MemberList and Book List
I am creating a trigger that checks if another column already has the same value. This however returns a print even if there is no other row with the same value. I believe the mistake is in the IF clause, but I can't figure it out. For the database the following SQL is used.
CREATE TABLE Gebruiker
(
gebruikersnaam VARCHAR(25) NOT NULL,
voornaam VARCHAR(25) NOT NULL,
achternaam VARCHAR(25) NOT NULL,
adresregel_1 VARCHAR(255) NULL,
adresregel_2 VARCHAR(255) NULL,
postcode CHAR(7) NULL,
plaatsnaam VARCHAR(255) NULL,
land VARCHAR(255) NULL,
geboortedag CHAR(10) NOT NULL,
mailbox VARCHAR(255) NOT NULL,
wachtwoord VARCHAR(255) NOT NULL,
verkoper BIT NOT NULL,
CONSTRAINT pk_gebruiker
PRIMARY KEY (gebruikersnaam),
)
CREATE TABLE Verkoper
(
gebruikersnaam VARCHAR(25) NOT NULL,
banknaam VARCHAR(255) NULL,
rekeningnummer VARCHAR(32) NULL,
controleoptienaam CHAR(10) NOT NULL,
creditcardnummer INTEGER NULL,
CONSTRAINT pk_Verkoper
PRIMARY KEY (gebruikersnaam),
CONSTRAINT fk_Verkoper_Gebruikersnaam
FOREIGN KEY (gebruikersnaam) REFERENCES Gebruiker(gebruikersnaam),
CONSTRAINT ck_rekening
CHECK (rekeningnummer is NOT NULL OR creditcardnummer is NOT NULL),
CONSTRAINT ck_controleoptie
CHECK (controleoptienaam IN('Post', 'Creditcard'))
)
CREATE TABLE Voorwerp
(
voorwerpnummer NUMERIC(10) NOT NULL,
titel VARCHAR(255) NOT NULL,
beschrijving VARCHAR(255) NOT NULL,
startprijs NUMERIC(5,2) NOT NULL,
betalingswijze VARCHAR(255) NOT NULL,
betalingsinstructie VARCHAR(255) NOT NULL,
plaatsnaam VARCHAR(255) NOT NULL,
land VARCHAR(255) NOT NULL,
looptijd INTEGER NOT NULL,
looptijdbegindag CHAR(10) NOT NULL,
looptijdbegintijdstip CHAR(8) NOT NULL,
verzendkosten NUMERIC(5,2),
verzendinstructie VARCHAR(255) NOT NULL,
verkoper VARCHAR(25) NOT NULL,
koper VARCHAR(25) NULL,
looptijdeindedag CHAR(10) NOT NULL,
looptijdeindetijdstip CHAR(8) NOT NULL,
veilingGesloten BIT NOT NULL,
verkoopprijs NUMERIC(5,2) NOT NULL,
CONSTRAINT pk_voorwerp
PRIMARY KEY (voorwerpnummer),
CONSTRAINT fk_voorwerp_verkoper
FOREIGN KEY (verkoper) REFERENCES verkoper(gebruikersnaam),
CONSTRAINT fk_voorwerp_gebruiker
FOREIGN KEY (koper) REFERENCES gebruiker(gebruikersnaam)
)
CREATE TABLE Bod
(
voorwerpnummer NUMERIC(10) NOT NULL,
euro NUMERIC(5,2) NOT NULL,
gebruikersnaam CHAR(10) NOT NULL,
datum CHAR(10) NOT NULL,
tijdaanduiding CHAR(8) NOT NULL,
CONSTRAINT pk_Bod
PRIMARY KEY (voorwerpnummer, euro),
CONSTRAINT ak_Bod_Gebruiker_BodDag_Tijdstip
UNIQUE (gebruikersnaam, datum, tijdaanduiding),
CONSTRAINT ak_Bod_Voorwerp_BodDag_Tijdstip
UNIQUE (voorwerpnummer, datum, tijdaanduiding),
CONSTRAINT fk_Bod_Voorwerp
FOREIGN KEY (voorwerpnummer) REFERENCES Voorwerp(voorwerpnummer)
)
INSERT INTO Gebruiker
VALUES ('Lars', 'Lars', 'Last_name', NULL, NULL, NULL, NULL, NULL, '04/04/2019', 'lars#mymailbox.cloud', 'MyPassword', 1)
INSERT INTO Verkoper
VALUES ('Lars', 'ING', 'NL42INGB0685', 'Creditcard', 654654665);
INSERT INTO Voorwerp (voorwerpnummer, titel, beschrijving, startprijs, betalingswijze, betalingsinstructie, plaatsnaam, land, looptijd, looptijdbegindag, looptijdbegintijdstip, verzendkosten, verzendinstructie, verkoper, looptijdeindedag, looptijdeindetijdstip, veilinggesloten)
VALUES (3434343434, 'test', 'test', 10.00, 'bank/giro', 'betaald voor levering', 'Arnhem', 'Nederland', 7, '20/04/2019', '13:30:15', 5.00, 'pakket post', 'Lars', '27/04/2019', '12:30:15', 0);
The trigger in question:
CREATE TRIGGER hoger_bod
ON Bod
FOR INSERT, UPDATE
AS
BEGIN
IF EXISTS (SELECT 1
FROM inserted AS i
WHERE i.voorwerpnummer IN (SELECT voorwerpnummer FROM Bod))
BEGIN
PRINT 'This voorwerpnummer already has a row.';
END;
END;
This trigger should return 'This voorwerpnummer already has a row.' when the Bod column has a row with the same voorwerpnummer value.
To test this I've used the following INSERT statement:
INSERT INTO Bod VALUES (3434343434, 5.01, 'Lars', '12/12/2011', '11:11:16')
This returns 'This voorwerpnummer already has a row.', even though there are no rows.
For a second insert:
INSERT INTO Bod VALUES (3434343434, 10.00, 'Lars', '12/12/2011', '11:16:16')
If you'd like to test this; here is a dbfiddle: https://dbfiddle.uk/?fiddle=1777fa6548beef3ac5e701b7bc2c489c
As per #GuidoG, try:
CREATE TRIGGER hoger_bod ON Bod
FOR INSERT,UPDATE
AS
BEGIN
IF (SELECT COUNT(*)
FROM inserted AS i
WHERE i.voorwerpnummer IN (SELECT voorwerpnummer FROM Bod)) >1
BEGIN
PRINT 'This voorwerpnummer already has a row.';
END;
END;
I have this uni assignment where I am given the script for the database as well as the one for the bulk insert and a dataset of about 1.300.000 records. With the database set up, i ran the bulk insert script and I am getting a bulk load data conversion error.
I asked all my friends if they had a similar experience but none of them had the same issue. I tried to look into that, it would seem that what causes this error to happen is the Date field in the MainTable, so i tried to find how to set the default date to DD/MM/YYYY and to use the / instead of -, all I found was some SELECT that formatted the getdate() function and the SET DATEFORMAT dmy command but that didn't fix my errors. I deleted the database and repeated the process at least 3 times now and I'm getting the same error every time.
CREATE TABLE MainTable
(
bookCode int NULL,
bookDt date NULL,
payCode int NULL,
payMethod char(2) NULL,
custCode int NULL,
custName varchar(30) NULL,
custSurname varchar (30) NULL,
custPhone varchar (20) NULL,
staffNo int NULL,
staffName varchar (30) NULL,
staffSurname varchar (30) NULL,
totalCost numeric(19, 2) NULL,
campCode char(3) NULL,
campName varchar (50) NULL,
numOfEmp int NULL,
empNo int NULL,
catCode char (1) NULL,
areaM2 int NULL,
unitCost numeric(4,2) NULL,
startDt date NULL,
endDt date NULL,
noPers int NULL,
costPerRental numeric(19, 2) NULL
);
SET DATEFORMAT dmy
BULK INSERT mainTable
FROM 'C:\DATA\GeneratedData.txt'
WITH (FIRSTROW = 2,FIELDTERMINATOR= ',', ROWTERMINATOR = '\n');
And a couple of records from the dataset:
2615981,14/08/2018,1,CC,990,Christie,BUCKNER,(+30)000-556-7301,5020,Zaria,RACE,45,ROS,Rosibos,200,151,C,30,15,15/08/2018,17/08/2018,1,45
2616347,17/08/2018,3,CA,403,Ashli,MAXWELL,(+30)000-114-8689,5010,Yovonnda,CAZARES,45,ROS,Rosibos,200,151,C,30,15,18/08/2018,20/08/2018,1,45
The error I am getting is this:
Msg 4864, Level 16, State 1, Line 3
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 2 (bookDt).
I am getting the same error what appears to be every single row in the text file.
Instead of "Set DateFormat dmy", try the following:
Set Language N'british'
Like I said in my comment, it's often easier to insert the data into a string type column in a staging table, and then convert the values afterwards. I've only done this with the Date columns here, but you can do it to the whole lot if you wish:
USE Sandbox;
GO
CREATE SCHEMA stg;
GO
CREATE TABLE stg.MainTable (bookCode int NULL,
bookDt varchar(10) NULL,
payCode int NULL,
payMethod char(2) NULL,
custCode int NULL,
custName varchar(30) NULL,
custSurname varchar(30) NULL,
custPhone varchar(20) NULL,
staffNo int NULL,
staffName varchar(30) NULL,
staffSurname varchar(30) NULL,
totalCost numeric(19, 2) NULL,
campCode char(3) NULL,
campName varchar(50) NULL,
numOfEmp int NULL,
empNo int NULL,
catCode char(1) NULL,
areaM2 int NULL,
unitCost numeric(4, 2) NULL,
startDt varchar(10) NULL,
endDt varchar(10) NULL,
noPers int NULL,
costPerRental numeric(19, 2) NULL);
GO
BULK INSERT stg.MainTable
FROM 'C:\DATA\GeneratedData.txt'
WITH (FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n');
GO
CREATE TABLE dbo.MainTable (bookCode int NULL,
bookDt date NULL,
payCode int NULL,
payMethod char(2) NULL,
custCode int NULL,
custName varchar(30) NULL,
custSurname varchar(30) NULL,
custPhone varchar(20) NULL,
staffNo int NULL,
staffName varchar(30) NULL,
staffSurname varchar(30) NULL,
totalCost numeric(19, 2) NULL,
campCode char(3) NULL,
campName varchar(50) NULL,
numOfEmp int NULL,
empNo int NULL,
catCode char(1) NULL,
areaM2 int NULL,
unitCost numeric(4, 2) NULL,
startDt date NULL,
endDt date NULL,
noPers int NULL,
costPerRental numeric(19, 2) NULL);
GO
INSERT INTO dbo.MainTable
SELECT bookCode,
CONVERT(date, bookDt, 103),
payCode,
payMethod,
custCode,
custName,
custSurname,
custPhone,
staffNo,
staffName,
staffSurname,
totalCost,
campCode,
campName,
numOfEmp,
empNo,
catCode,
areaM2,
unitCost,
CONVERT(date, startDt, 103),
CONVERT(date, endDt, 103),
noPers,
costPerRental
FROM stg.MainTable;
TRUNCATE stg.MainTable;
I have this DDL:
CREATE TABLE [dbo].[AdminTest] (
[AdminTestId] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[CreatedBy] INT NOT NULL,
[CreatedDate] DATETIME NOT NULL,
[ModifiedBy] INT NOT NULL,
[ModifiedDate] DATETIME NOT NULL,
[TestLevelId] INT NOT NULL,
[TestStatusId] INT NOT NULL,
[ExamId] INT NOT NULL,
[Text] NVARCHAR (MAX) NULL,
[Sequence] INT DEFAULT ((1)) NOT NULL,
[Release] NVARCHAR (50) DEFAULT ((1)) NOT NULL,
[Version] ROWVERSION NOT NULL,
[Price] MONEY DEFAULT ((0)) NOT NULL,
[ReleaseDate] DATETIME NULL,
[Code] VARCHAR (10) DEFAULT (LEFT(newid(), (5))) NOT NULL
);
Is there a way that I can require the ReleaseDate to have a value IF the value of the Release column is 2 ?
While ALTER:
ALTER TABLE AdminTest
ADD CONSTRAINT DF_Check CHECK (Release=2) ;
While Creation
CREATE TABLE ADMIN(
Release int
CONSTRAINT DF_Check CHECK (Release=2)
)
You can also define check constraint inline as a column constraint, like this -
CREATE TABLE [dbo].[AdminTest] (
[AdminTestId] INT IDENTITY (1, 1) NOT NULL,
[Release] NVARCHAR (50) DEFAULT ((1)) NOT NULL,
[ReleaseDate] DATETIME NULL CONSTRAINT CK_Release CHECK (Release='2') ,
[Code] VARCHAR (10) DEFAULT (LEFT(newid(), (5))) NOT NULL
);
I want to allow a column to accept only two values i.e. Open and Close statuses.How would I go through with it ?
CREATE TABLE Tasks(
ID INT IDENTITY(1,1),
Short_Name VARCHAR(25) NOT NULL,
DateCreated DATETIME NOT NULL,
DateModified DATETIME NULL,
CreatedBy INT NOT NULL,
ModifiedBy INT NULL,
Status VARCHAR(12) DEFAULT('Open') NOT NULL,
Description VARCHAR(255) NULL,
)
Try this
CREATE TABLE Tasks(
ID INT IDENTITY(1,1),
Short_Name VARCHAR(25) NOT NULL,
DateCreated DATETIME NOT NULL,
DateModified DATETIME NULL,
CreatedBy INT NOT NULL,
ModifiedBy INT NULL,
Status VARCHAR(12) DEFAULT('Open') NOT NULL,
Description VARCHAR(255) NULL,
CONSTRAINT chk_status_val CHECK(Status IN ('Open', 'Close'))
)