创建一个数据库,至少里面有5个表,每个表至少有十条数据,至少有两个表具有外键:
时间: 2024-03-14 11:49:53 浏览: 113
好的,我理解了您的问题。创建一个数据库并添加数据可以使用各种不同的工具和方法,我将提供一些示例代码以供您参考。请注意,此处使用的是 SQL Server 2019。
首先,我们需要创建数据库并连接到它:
```sql
CREATE DATABASE MyDatabase;
GO
USE MyDatabase;
GO
```
现在我们可以创建表。以下是5个表的示例代码,每个表都有10个数据行:
```sql
-- Table 1: Customers
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
Phone VARCHAR(20)
);
INSERT INTO Customers VALUES
(1, 'John', 'Doe', 'johndoe@example.com', '555-1234'),
(2, 'Jane', 'Doe', 'janedoe@example.com', '555-5678'),
(3, 'Bob', 'Smith', 'bobsmith@example.com', '555-9876'),
(4, 'Alice', 'Jones', 'alicejones@example.com', '555-4321'),
(5, 'Bill', 'Johnson', 'billjohnson@example.com', '555-8765'),
(6, 'Sally', 'Brown', 'sallybrown@example.com', '555-2345'),
(7, 'Mike', 'Davis', 'mikedavis@example.com', '555-7890'),
(8, 'Karen', 'Wilson', 'karenwilson@example.com', '555-3456'),
(9, 'David', 'Lee', 'davidlee@example.com', '555-9012'),
(10, 'Amy', 'Taylor', 'amytaylor@example.com', '555-6789');
-- Table 2: Orders
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT REFERENCES Customers(CustomerID),
OrderDate DATE,
Total DECIMAL(10,2)
);
INSERT INTO Orders VALUES
(1, 2, '2021-01-01', 100.00),
(2, 5, '2021-01-02', 200.00),
(3, 8, '2021-01-03', 300.00),
(4, 1, '2021-01-04', 400.00),
(5, 6, '2021-01-05', 500.00),
(6, 3, '2021-01-06', 600.00),
(7, 7, '2021-01-07', 700.00),
(8, 4, '2021-01-08', 800.00),
(9, 9, '2021-01-09', 900.00),
(10, 10, '2021-01-10', 1000.00);
-- Table 3: Products
CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(50),
Price DECIMAL(10,2)
);
INSERT INTO Products VALUES
(1, 'Widget', 10.00),
(2, 'Gadget', 20.00),
(3, 'Thingamajig', 30.00),
(4, 'Doohickey', 40.00),
(5, 'Whatchamacallit', 50.00),
(6, 'Doodad', 60.00),
(7, 'Gizmo', 70.00),
(8, 'Contraption', 80.00),
(9, 'Apparatus', 90.00),
(10, 'Device', 100.00);
-- Table 4: OrderDetails
CREATE TABLE OrderDetails (
OrderDetailID INT PRIMARY KEY,
OrderID INT REFERENCES Orders(OrderID),
ProductID INT REFERENCES Products(ProductID),
Quantity INT,
Price DECIMAL(10,2)
);
INSERT INTO OrderDetails VALUES
(1, 1, 1, 2, 20.00),
(2, 1, 2, 3, 60.00),
(3, 2, 3, 1, 30.00),
(4, 3, 4, 4, 160.00),
(5, 4, 5, 2, 100.00),
(6, 5, 6, 1, 60.00),
(7, 6, 7, 3, 210.00),
(8, 7, 8, 2, 160.00),
(9, 8, 9, 1, 90.00),
(10, 9, 10, 5, 500.00);
-- Table 5: Employees
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE,
Salary DECIMAL(10,2),
ManagerID INT REFERENCES Employees(EmployeeID)
);
INSERT INTO Employees VALUES
(1, 'Alice', 'Smith', '2020-01-01', 50000.00, NULL),
(2, 'Bob', 'Johnson', '2020-02-01', 60000.00, 1),
(3, 'Charlie', 'Brown', '2020-03-01', 70000.00, 1),
(4, 'David', 'Lee', '2020-04-01', 80000.00, 2),
(5, 'Emily', 'Jones', '2020-05-01', 90000.00, 2),
(6, 'Frank', 'Davis', '2020-06-01', 100000.00, 3),
(7, 'George', 'Wilson', '2020-07-01', 110000.00, 3),
(8, 'Hannah', 'Taylor', '2020-08-01', 120000.00, 3),
(9, 'Isabelle', 'Brown', '2020-09-01', 130000.00, 3),
(10, 'Jack', 'Johnson', '2020-10-01', 140000.00, 3);
```
这些表中的两个表具有外键:Orders表中的CustomerID列和OrderDetails表中的OrderID列。这些外键确保了数据的完整性,因为它们确保了这些列中的值与另一个表中的值相匹配。
希望这可以回答您的问题!
阅读全文