Certainly! Below is a comprehensive guide to completing Assignment 3.1 and Assignment 3.2 for your Yelp dataset project using Microsoft SQL Server. This guide includes the necessary SQL scripts to create the required tables with appropriate constraints and instructions on how to insert sample data while respecting foreign key dependencies.
Assignment 3.1: Creating Tables and Constraints
1. Overview
You need to create six tables: business, checkin, tip, review, user_yelp, and friendship. Each table has specific attributes with defined data types and constraints, including primary keys and foreign keys.
2. Order of Table Creation
To ensure that foreign key constraints are successfully established, it's crucial to create the tables in an order that respects their dependencies. Here's the recommended order:
- user_yelp
- business
- friendship
- checkin
- tip
- review
3. SQL Scripts for Table Creation
Below are the SQL CREATE TABLE statements for each table with the specified attributes and constraints.
a. Create user_yelp Table
CREATE TABLE user_yelp (
user_id CHAR(22) PRIMARY KEY,
name VARCHAR(35) NOT NULL,
review_count INT DEFAULT 0 CHECK (review_count >= 0),
yelping_since DATETIME NOT NULL DEFAULT GETDATE(),
useful INT DEFAULT 0 CHECK (useful >= 0),
funny INT DEFAULT 0 CHECK (funny >= 0),
cool INT DEFAULT 0 CHECK (cool >= 0),
fans INT DEFAULT 0 CHECK (fans >= 0),
average_stars DECIMAL(3,2) CHECK (average_stars BETWEEN 1 AND 5)
);
b. Create business Table
CREATE TABLE business (
business_id CHAR(22) PRIMARY KEY,
name VARCHAR(60) NOT NULL,
address VARCHAR(75),
city VARCHAR(30) NOT NULL,
postal_code VARCHAR(7),
stars DECIMAL(2,1) CHECK (stars BETWEEN 1 AND 5),
review_count INT DEFAULT 0 CHECK (review_count >= 0)
);
c. Create friendship Table
CREATE TABLE friendship (
user_id CHAR(22),
friend CHAR(22),
PRIMARY KEY (user_id, friend),
FOREIGN KEY (user_id) REFERENCES user_yelp(user_id)
ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (friend) REFERENCES user_yelp(user_id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
d. Create checkin Table
CREATE TABLE checkin (
checkin_id INT PRIMARY KEY,
business_id CHAR(22) NOT NULL,
date DATETIME NOT NULL DEFAULT GETDATE(),
FOREIGN KEY (business_id) REFERENCES business(business_id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
e. Create tip Table
CREATE TABLE tip (
tip_id INT PRIMARY KEY,
user_id CHAR(22) NOT NULL,
business_id CHAR(22) NOT NULL,
date DATETIME NOT NULL DEFAULT GETDATE(),
compliment_count INT DEFAULT 0 CHECK (compliment_count >= 0),
FOREIGN KEY (user_id) REFERENCES user_yelp(user_id)
ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (business_id) REFERENCES business(business_id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
f. Create review Table
CREATE TABLE review (
review_id CHAR(22) PRIMARY KEY,
user_id CHAR(22) NOT NULL,
business_id CHAR(22) NOT NULL,
stars INT NOT NULL CHECK (stars BETWEEN 1 AND 5),
useful INT DEFAULT 0 CHECK (useful >= 0),
funny INT DEFAULT 0 CHECK (funny >= 0),
cool INT DEFAULT 0 CHECK (cool >= 0),
date DATETIME DEFAULT GETDATE(),
FOREIGN KEY (user_id) REFERENCES user_yelp(user_id)
ON DELETE NO ACTION ON UPDATE NO ACTION,
FOREIGN KEY (business_id) REFERENCES business(business_id)
ON DELETE NO ACTION ON UPDATE NO ACTION
);
4. Explanation of Constraints
- Primary Keys: Each table has a primary key as specified, ensuring unique identifiers for records.
- Foreign Keys: Foreign keys enforce referential integrity between tables. For example,
checkin.business_id must exist in business.business_id.
- Data Types and Constraints: Data types match the specified domains. Constraints like
NOT NULL, DEFAULT, and CHECK ensure data validity.
Assignment 3.2: Inserting Sample Data
1. Overview
You need to insert sample data into the created tables. The insertion order must respect foreign key dependencies to avoid violations. The recommended insertion order is:
- user_yelp
- business
- friendship
- checkin
- tip
- review
2. Insertion Ordering Rationale
user_yelp and business: These are the primary tables without dependencies.
friendship: Depends on user_yelp.
checkin and tip: Depend on business and, for tip, also on user_yelp.
review: Depends on both user_yelp and business.
3. Methods for Data Insertion
You can choose between two methods: Command Prompt (bcp utility) or T-SQL BULK INSERT. Below are examples for both.
a. Using T-SQL BULK INSERT
Assuming your CSV files are located in D:\Datasets\ and named accordingly (e.g., user_yelp.csv, business.csv, etc.), here are sample BULK INSERT statements:
-- 1. Insert into user_yelp
BULK INSERT dbo.user_yelp
FROM 'D:\Datasets\user_yelp.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
-- 2. Insert into business
BULK INSERT dbo.business
FROM 'D:\Datasets\business.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
-- 3. Insert into friendship
BULK INSERT dbo.friendship
FROM 'D:\Datasets\friendship.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
-- 4. Insert into checkin
BULK INSERT dbo.checkin
FROM 'D:\Datasets\checkin.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
-- 5. Insert into tip
BULK INSERT dbo.tip
FROM 'D:\Datasets\tip.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
-- 6. Insert into review
BULK INSERT dbo.review
FROM 'D:\Datasets\review.csv'
WITH (
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
FIRSTROW = 2,
FORMAT = 'CSV',
TABLOCK
);
Notes:
FIRSTROW = 2 assumes that the first row in your CSV files contains headers.
FIELDTERMINATOR and ROWTERMINATOR are set based on standard CSV formatting. Adjust if your files use different delimiters.
TABLOCK improves performance by acquiring a bulk update lock for the duration of the bulk copy operation.
b. Using Command Prompt (bcp Utility)
Alternatively, you can use the bcp utility from the command prompt. Below are sample commands for each table. Replace DBName with your actual database name and adjust file paths as necessary.
-- 1. Import user_yelp
bcp DBName.dbo.user_yelp in "D:\Datasets\user_yelp.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
-- 2. Import business
bcp DBName.dbo.business in "D:\Datasets\business.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
-- 3. Import friendship
bcp DBName.dbo.friendship in "D:\Datasets\friendship.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
-- 4. Import checkin
bcp DBName.dbo.checkin in "D:\Datasets\checkin.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
-- 5. Import tip
bcp DBName.dbo.tip in "D:\Datasets\tip.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
-- 6. Import review
bcp DBName.dbo.review in "D:\Datasets\review.csv" -S YourServerName -b 1000 -m 100 -T -c -F 2
Parameters Explained:
DBName.dbo.TableName: Fully qualified name of the target table.
in: Specifies the direction of the data transfer (from a data file into SQL Server).
"D:\Datasets\TableName.csv": Path to the CSV file.
-S YourServerName: Specifies the SQL Server instance.
-b 1000: Batch size (number of rows per batch).
-m 100: Maximum number of errors allowed before bcp aborts.
-T: Uses trusted connection (Windows authentication).
-c: Uses character data type.
-F 2: Starts reading from the second row (assuming the first row contains headers).
Note: Ensure that the SQL Server service account has read permissions on the directory containing the CSV files.
4. Execution Order
Execute the bulk insert commands in the following sequence to respect foreign key constraints:
- user_yelp
- business
- friendship
- checkin
- tip
- review
Executing in this order ensures that all referenced records exist before dependent records are inserted, preventing foreign key violations.
Additional Tips
-
Transaction Handling: Consider wrapping your bulk insert operations within a transaction to ensure data integrity. If an error occurs, you can roll back the transaction.
BEGIN TRANSACTION;
-- Bulk insert statements here
COMMIT TRANSACTION;
-
Error Handling: Monitor the number of rows affected and handle any errors reported during the bulk insert operations.
-
Performance Optimization:
- Indexes: Disable non-clustered indexes on the target tables before bulk inserting and rebuild them afterward to improve performance.
- Batch Size: Adjust the batch size (
-b parameter) based on your system's performance and memory capacity.
-
Data Validation: After insertion, validate the data to ensure that all constraints are satisfied and that the data has been loaded correctly.
By following this guide, you should be able to successfully create the required tables with the appropriate constraints and load your sample Yelp data into the Microsoft SQL Server database on the Cypress server. Ensure that you have the necessary permissions and that your SQL Server instance is correctly configured to handle bulk operations.
If you encounter any issues or have further questions, feel free to ask!