-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesDB_Create-Insert.sql
More file actions
80 lines (69 loc) · 2.2 KB
/
SalesDB_Create-Insert.sql
File metadata and controls
80 lines (69 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
=============================================================
Database Creation and Table Setup Script
=============================================================
Script Purpose:
This script creates a new SQL Server database named 'MyDatabase'.
If the database already exists, it is dropped to ensure a clean setup.
The script then creates three tables: 'customers', 'orders', and 'employees'
with their respective schemas, and populates them with sample data.
WARNING:
Running this script will drop the entire 'MyDatabase' database if it exists,
permanently deleting all data within it. Proceed with caution and ensure you
have proper backups before executing this script.
*/
USE master;
GO
-- Drop and recreate the 'MyDatabase' database
IF EXISTS (SELECT 1 FROM sys.databases WHERE name = 'MyDatabase')
BEGIN
ALTER DATABASE MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE MyDatabase;
END;
GO
-- Create the 'MyDatabase' database
CREATE DATABASE MyDatabase;
GO
USE MyDatabase;
GO
-- ======================================================
-- Table: customers
-- ======================================================
DROP TABLE IF EXISTS customers;
GO
CREATE TABLE customers (
id INT NOT NULL,
first_name VARCHAR(50) NOT NULL,
country VARCHAR(50),
score INT,
CONSTRAINT PK_customers PRIMARY KEY (id)
);
GO
-- Insert customers data
INSERT INTO customers (id, first_name, country, score) VALUES
(1, 'Maria', 'Germany', 350),
(2, ' John', 'USA', 900),
(3, 'Georg', 'UK', 750),
(4, 'Martin', 'Germany', 500),
(5, 'Peter', 'USA', 0);
GO
-- ======================================================
-- Table: orders
-- ======================================================
DROP TABLE IF EXISTS orders;
GO
CREATE TABLE orders (
order_id INT NOT NULL,
customer_id INT NOT NULL,
order_date DATE,
sales INT,
CONSTRAINT PK_orders PRIMARY KEY (order_id)
);
GO
-- Insert orders data
INSERT INTO orders (order_id, customer_id, order_date, sales) VALUES
(1001, 1, '2021-01-11', 35),
(1002, 2, '2021-04-05', 15),
(1003, 3, '2021-06-18', 20),
(1004, 6, '2021-08-31', 10);
GO