SQL Server 建表语句
-- 切换当前数据库上下文为 ProConfigDB(后续所有操作都在这个数据库中进行)
use ProConfigDB
go
-- 创建用户表 SysAdmins 之前,先检查该表是否存在
-- sysobjects 是系统表,存储了数据库中所有对象(如表、视图等)的信息
if exists(select * from sysobjects where name = 'SysAdmins')
drop table SysAdmins -- 如果存在,就删除它(避免重复创建报错)
go
-- 开始创建用户表 SysAdmins
create table SysAdmins
(
-- SysAdminId:管理员唯一标识,使用自增列(从10开始,每次+1),并设为主键(唯一且非空)
SysAdminId int identity(10,1) primary key,
-- SysAccount:系统账号,最大100个字符,必须唯一(unique),长度至少4位,且不能为空
SysAccount varchar(100) unique check(len(SysAccount)>=4) not null,
-- AdmiinName:管理员姓名(注意这里拼写是 AdmiinName,可能是笔误,应为 AdminName)
AdmiinName varchar(100) not null,
-- AdminPwd:密码,最多8个字符(注意:varchar(8) 表示最多8字节,英文没问题,中文可能存不下!)
-- 限制密码长度在6到10之间(但字段只允许8字符,所以实际只能是6~8)
-- 建议:如果要支持6~10位,应改为 varchar(10) 或 nvarchar(10)
AdminPwd varchar(8) check(len(AdminPwd)>=6 and len(AdminPwd)<=10) not null
)
go
-- 检查项目表 Projects 是否存在,存在则删除
if exists(select * from sysobjects where name = 'Projects')
drop table Projects
go
-- 创建项目表 Projects
create table Projects
(
-- ProjectId:项目ID,自增(从100开始,每次+1),设为主键
ProjectId int identity(100,1) primary key,
-- ProjectName:项目名称,最多100个字符,不能为空
ProjectName varchar(100) not null
)
go
-- 检查设备类型表 EquipmentType 是否存在,存在则删除
if exists(select * from sysobjects where name = 'EquipmentType')
drop table EquipmentType
go
-- 创建设备类型表
create table EquipmentType
(
-- ETypeId:设备类型ID,自增(从10开始),主键
ETypeId int identity(10,1) primary key,
-- ETypeName:设备类型名称,不能为空
ETypeName varchar(100) not null
)
go
-- 检查协议类型表 ProtocolType 是否存在,存在则删除
if exists(select * from sysobjects where name = 'ProtocolType')
drop table ProtocolType
go
-- 创建协议类型表
create table ProtocolType
(
-- PTypeId:协议类型ID,自增(从10开始),主键
PTypeId int identity(10,1) primary key,
-- ETypeId:外键,引用 EquipmentType 表的 ETypeId 列
-- 表示“这个协议属于哪种设备类型”
ETypeId int references EquipmentType(ETypeId),
-- PTypeName:协议类型名称,使用 nvarchar 支持中文(推荐存储中文用 nvarchar)
PTypeName nvarchar(100) not null
)
go
-- 检查设备表 Equipments 是否存在,存在则删除
if exists(select * from sysobjects where name = 'Equipments')
drop table Equipments
go
-- 创建设备表 Equipments
create table Equipments
(
-- EquipmentId:设备唯一ID,自增(从1000开始),主键
EquipmentId int identity(1000,1) primary key,
-- ProjectId:外键,引用 Projects 表的 ProjectId
-- 表示“这个设备属于哪个项目”
ProjectId int references Projects(ProjectId),
-- ETypeId:外键,引用 EquipmentType 表的 ETypeId
-- 表示“这个设备是什么类型”
ETypeId int references EquipmentType(ETypeId),
-- PTypeId:外键,引用 ProtocolType 表的 PTypeId
-- 表示“这个设备使用什么通信协议”
PTypeId int references ProtocolType(PTypeId),
-- EquipmentName:设备名称,支持中文(用 nvarchar)
EquipmentName nvarchar(100) not null,
-- IPAddress:IP地址,字符串形式(如 "192.168.1.100")
IPAddress varchar(100) not null,
-- PortNo:端口号,字符串(因为可能写成 "COM1" 或 "502",所以用字符串更灵活)
PortNo varchar(50) not null,
-- SerialNo:设备序列号
SerialNo varchar(50) not null,
-- BaudRate:波特率(如 9600, 115200)
BaudRate int not null,
-- DataBit:数据位(如 7, 8)
DataBit int not null,
-- ParityBit:校验位(可能为 0=无校验, 1=奇校验, 2=偶校验,允许为空)
ParityBit int null,
-- StopBit:停止位(如 1, 2)
StopBit int not null,
-- OPCNodeName:OPC 节点名称(工业通信常用)
OPCNodeName varchar(100) not null,
-- OPCServerName:OPC 服务器名称
OPCServerName varchar(100) not null,
-- IsEnable:是否启用(通常用 0/1 表示 否/是)
IsEnable int not null,
-- Comments:备注信息,支持中文
Comments nvarchar(200) not null
)
go
完整
use master
go
-- 如果存在这个数据库名称 ,否则删除
if exists(select * from sysdatabases where name = 'ProConfigDB')
drop database ProConfigDB
-- 创建数据库
create database ProConfigDB
on primary
(
name='ProConfigDB_data', -- 数据库文件名称
filename='D:\\data\\ProConfigDB_data.mdf', --指向的文件
size=100MB, -- 初始大小
filegrowth=50MB -- 自动扩展大小
)
log on
(
name='ProConfigDB_data_log', -- 数据库文件名称
filename='D:\\data\\ProConfigDB_data.ldf', --指向的文件
size=10MB, -- 初始大小
filegrowth=5MB -- 自动扩展大小
)
go
use ProConfigDB
go
-- 创建用户表(SysAdmins)
if exists(select * from sysobjects where name = 'SysAdmins')
drop table SysAdmins
go
create table SysAdmins
(
SysAdminId int identity(10,1) primary key,
SysAccount varchar(100) unique check(len(SysAccount)>=4)not null,
AdmiinName varchar(100) not null,
AdminPwd varchar(8) check(len(AdminPwd)>=6 and len(AdminPwd)<=10) not null
)
go
-- 创建项目表
if exists(select * from sysobjects where name = 'Projects')
drop table Projects
go
create table Projects
(
ProjectId int identity(100,1) primary key,
ProjectName varchar(100) not null,
)
go
-- 创建设备类型表
if exists(select * from sysobjects where name = 'EquipmentType')
drop table EquipmentType
go
create table EquipmentType
(
ETypeId int identity(10,1) primary key,
ETypeName varchar(100) not null,
)
go
-- 创建协议类型表
if exists(select * from sysobjects where name = 'ProtocolType')
drop table ProtocolType
go
create table ProtocolType
(
PTypeId int identity(10,1) primary key,
ETypeId int references EquipmentType(ETypeId), -- 外键 ,设备类型ID
PTypeName nvarchar(100) not null
)
go
-- 创建设备表
if exists(select * from sysobjects where name = 'Equipments')
drop table Equipments
go
create table Equipments
(
EquipmentId int identity(1000,1) primary key,
ProjectId int references Projects(ProjectId),
ETypeId int references EquipmentType(ETypeId),
PTypeId int references ProtocolType(PTypeId),
EquipmentName nvarchar(100) not null,
IPAddress varchar(100) not null,
PortNo varchar(50) not null,
SerialNo varchar(50) not null,
BaudRate int not null,
DataBit int not null,
ParityBit int null,
StopBit int not null,
OPCNodeName varchar(100) not null,
OPCServerName varchar(100) not null,
IsEnable int not null,
Comments nvarchar(200) not null
)
go
-- 创建设备表
if exists(select * from sysobjects where name = 'CommunicationsGrop')
drop table CommunicationsGrop
go
create table CommunicationsGrop
(
CGId int identity(1000,1) primary key,
EquipmentId int references Equipments(EquipmentId),
CGName nvarchar(100) not null,
StartAddress varchar(100) not null,
CGLength int not null,
IsEnable int not null,
Comments nvarchar(200) not null
)
go
-- 创建变量表
if exists(select * from sysobjects where name = 'Variable')
drop table Variable
go
create table Variable
(
VariableId int identity(100000,1) primary key,
VariableName varchar(100),
CGId int references CommunicationsGrop(CGId),
StartAddress varchar(100) not null,
DataType varchar(100) not null,
IsMaxAlarm bit not null,
IsMinAlarm bit not null,
AlarmMax varchar(100) not null,
AlarmMin varchar(100) not null,
IsFiled bit not null,
AlarmMaxNode nvarchar(200) not null,
AlarmMinNode nvarchar(200) not null,
Scale float not null,
Offset float not null,
Comments nvarchar(200)
)
go
-- 检查表是否存在
SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'Projects';
-- 插入数据
use ProConfigDB
go
insert into SysAdmins(SysAccount , AdmiinName , AdminPwd)
values ('Luna001','陈平安','123456'),('Luna002','徐凤年','123456')
insert into EquipmentType(ETypeName) values ('以太网') , ('串口') , ('OPC')
insert into ProtocolType(ETypeId , PTypeName) values (10 , '西门子S7') , (10 , '三菱MC') ,
(10, '三菱A1E') , (10 , '欧姆龙FinsTCP') , (10 , 'ModbusTCP') , (10 , 'ModbusRTUOverTCP') ,
(11 , '三菱FXSerial') , (11 , 'ModbusRTU') , (12 , 'OPCDA') , (12 , 'OPCUA')
select * from ProtocolType
给初学者的几点建议:
-
字段命名建议统一:
比如AdmiinName 很可能是AdminName的拼写错误,注意检查。 -
密码字段长度问题:
你定义AdminPwd varchar(8),但约束是6~10 位,这会导致 无法存入9或10位密码。
✅ 建议改为:varchar(10) 或更好的做法是 存储密码哈希值(如 SHA256) ,而不是明文! -
中文存储用
nvarchar:
varchar 是单字节编码,可能无法正确存储中文;nvarchar是 Unicode,推荐用于中文字段。 -
外键最好显式命名(便于后期维护):
CONSTRAINT FK_Equipments_Projects FOREIGN KEY (ProjectId) REFERENCES Projects(ProjectId) -
sysobjects 是旧系统表,新项目建议用:IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'SysAdmins')
如果你还有哪一行不太明白,或者想了解“为什么用 go”、“identity 是什么”等概念,也可以继续问我!祝你学习顺利!

如果现在给我一个选择,永远不见你还是娶你,我会选择娶你。也许这有点浪漫过头,但好多人结婚的理由比这还少。
浙公网安备 33010602011771号