假设我们要为宠物管理系统设计一个数据库,这个数据库包含用户,宠物,客户,食品,宠物分类等信息。请用SQLY语句实现创建数据库,创建表,注意表中要实现各种完整性如建立主键,外键,check约束等,最后向
时间: 2024-10-18 13:06:44 浏览: 32
假设我们正在设计一个名为`PetCareDB`的数据库,用于存储宠物管理系统的相关信息。首先,我们需要创建几个表来对应用户、宠物、客户、食品和宠物分类。这里我会给出基本的表结构和SQL创建语句,同时添加一些完整性约束:
```sql
CREATE DATABASE PetCareDB;
USE PetCareDB;
-- 创建用户表 (Users)
CREATE TABLE Users (
UserID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE,
Password VARCHAR(100) CHECK (Password != ''),
CreatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 创建客户表 (Customers)
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
UserID INT,
FOREIGN KEY (UserID) REFERENCES Users(UserID),
Address VARCHAR(200),
PhoneNumber VARCHAR(20) UNIQUE,
CreatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT chk_customer_phone CHECK (PhoneNumber REGEXP '^[0-9]{10}$')
);
-- 创建宠物分类表 (PetCategories)
CREATE TABLE PetCategories (
CategoryID INT PRIMARY KEY,
CategoryName VARCHAR(50) NOT NULL,
Description TEXT
);
-- 创建宠物表 (Pets)
CREATE TABLE Pets (
PetID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
OwnerID INT,
Species VARCHAR(50),
BirthDate DATE,
CategoryID INT,
FOREIGN KEY (OwnerID) REFERENCES Users(UserID),
FOREIGN KEY (CategoryID) REFERENCES PetCategories(CategoryID),
HealthStatus ENUM('Healthy', 'Sick') CHECK (HealthStatus IN ('Healthy', 'Sick')),
CreatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 创建食品表 (FoodItems)
CREATE TABLE FoodItems (
FoodID INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
Brand VARCHAR(50),
Category VARCHAR(50),
Calories INT,
FOREIGN KEY (Category) REFERENCES PetCategories(CategoryID),
CreatedOn TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 创建宠物食品购买记录表 (PetFoodPurchase)
CREATE TABLE PetFoodPurchase (
PurchaseID INT PRIMARY KEY,
PetID INT,
FoodID INT,
Quantity INT,
DateBought DATE,
FOREIGN KEY (PetID) REFERENCES Pets(PetID),
FOREIGN KEY (FoodID) REFERENCES FoodItems(FoodID)
);
```
阅读全文