AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 162372
Accepted
SQLserving
SQLserving
Asked: 2017-01-27 11:35:11 +0800 CST2017-01-27 11:35:11 +0800 CST 2017-01-27 11:35:11 +0800 CST

在 SQL Server 中为特殊字段创建范围

  • 772

我有两列看起来像这样:

开始_发布结束_发布
---------- ----------
102+20.45 153+19.22
120+21.25 220+25.30
……

我想为每一列引入一个约束:

  • Start_Pos
    最小范围:100+50.30
    最大范围:150+20.65
  • End_Pos
    最小范围:150+60.30
    最大范围:500+20.75

所有值均以英尺为单位并遵循特殊格式。这是一个建筑惯例,我不是专家,但据我所知,1+00 就是 100 英尺。本质上,采用上述 100+50.30 到 150+20.65 的范围意味着 4970 英尺的距离。我所做的只是删除加号并减去两个数字。希望这是有道理的。

有没有办法在保持格式的同时定义此约束?

sql-server performance
  • 2 2 个回答
  • 142 Views

2 个回答

  • Voted
  1. Best Answer
    Hannah Vernon
    2017-01-27T12:46:39+08:002017-01-27T12:46:39+08:00

    假设左边的数字以+100 为单位进行测量,您也许可以创建这样的约束:

    CREATE TABLE dbo.SurveyData
    (
        StartPostStation int NOT NULL
        , StartPostPlus decimal(38,3) NOT NULL
        , EndPostStation int NOT NULL
        , EndPostPlus decimal(38,3) NOT NULL
        , CONSTRAINT StartMin
            CHECK ((StartPostStation * 100 + StartPostPlus) > 100*100+50.3)
        , CONSTRAINT StartMax
            CHECK ((StartPostStation * 100 + StartPostPlus) < 150*100+20.65)
        , CONSTRAINT EndMin
            CHECK ((EndPostStation * 100 + EndPostPlus) > 150*100+60.3)
        , CONSTRAINT EndMax
            CHECK ((EndPostStation * 100 + EndPostPlus) < 500*100+20.75)
    );
    

    但是,这不允许动态约束,表中的所有行都将被限制为那些测量值,这实际上可能正是您所需要的。

    如果您需要为每个独立行动态调整约束,您可以这样做:

    CREATE TABLE dbo.SurveyData
    (
        StartPostStation int NOT NULL
        , StartPostPlus decimal(38,3) NOT NULL
        , EndPostStation int NOT NULL
        , EndPostPlus decimal(38,3) NOT NULL
        , CONSTRAINT StartMin
            CHECK ((StartPostStation * PostUnits + StartPostPlus) > MinStartPost * PostUnits + MinStartPostPlus)
        , CONSTRAINT StartMax
            CHECK ((StartPostStation * PostUnits + StartPostPlus) < MaxStartPost * PostUnits + MaxStartPostPlus)
        , CONSTRAINT EndMin
            CHECK ((EndPostStation * PostUnits + EndPostPlus) > MinEndPost * PostUnits + MinEndPostPlus)
        , CONSTRAINT EndMax
            CHECK ((EndPostStation * PostUnits + EndPostPlus) < MaxEndPost * PostUnits + MaxEndPostPlus)
        , MinStartPost int NOT NULL
        , MinStartPostPlus decimal(38,3) NOT NULL
        , MaxStartPost int NOT NULL
        , MaxStartPostPlus decimal(38,3) NOT NULL
        , MinEndPost int NOT NULL
        , MinEndPostPlus decimal(38,3) NOT NULL
        , MaxEndPost int NOT NULL
        , MaxEndPostPlus decimal(38,3) NOT NULL
        , PostUnits int NOT NULL
    );
    
    
    INSERT INTO dbo.SurveyData (StartPostStation, StartPostPlus, EndPostStation, EndPostPlus
        , MinStartPost, MinStartPostPlus, MaxStartPost, MaxStartPostPlus
        , MinEndPost, MinEndPostPlus, MaxEndPost, MaxEndPostPlus
        , PostUnits)
    VALUES (120, 49.2, 175, 80.5  --measurements
        , 100, 50.3, 150, 20.65   --valid start post range
        , 150, 60.3, 500, 20.75   --valid end post range
        , 100); --units per post
    

    选择数据将如下所示:

    SELECT StartPost = CONVERT(varchar(10), sd.StartPostStation) + '+' + CONVERT(varchar(50), sd.StartPostPlus)
        , EndPost = CONVERT(varchar(10), sd.EndPostStation) + '+' + CONVERT(varchar(50), sd.EndPostPlus)
    FROM dbo.SurveyData sd;
    

    结果,像这样:

    +------------+------------+
    | 开始发布 | 尾贴 |
    +------------+------------+
    | 120+49.200 | 175+80.500 |
    +------------+------------+
    

    帖子列已分为两部分,使我们能够使用正确的数据类型来存储数字数据。如果我们尝试存储175+80.50在单个字段中,我们最终会使用一个varchar(x)列,它允许各种不良数据的可能性,例如tee+27.-1,很难全面预防。因此,我们将站点存储在一列中,并将该站点的偏移量存储在下一列中。当在屏幕上或报告中向人类展示这些数据时,我们会使用上面显示的串联版本。

    • 8
  2. SQLserving
    2017-01-28T12:14:45+08:002017-01-28T12:14:45+08:00
        Create table dbo.road   
        (  
        ID tinyint not null,   
        StartMile varchar(9) not null,  
        CONSTRAINT StartMile CHECK (StartMile LIKE '[0-9][0-9][0-9][+][0-9][0-9][.][0-9][0-9]'),  
        CONSTRAINT StartMile CHECK (((left([Startmile],(3))*(100))+(Convert(decimal(5,2),(right([Startmile],(5))))))>=(12764.76)),
        (((left([Startmile],(3))*(100))+(Convert(decimal(5,2),(right([Startmile],(5))))))<=(30618.84))
        );   
        Insert into dbo.road values (1,'150+60.20');
    

    除了 Max 提出的建议之外,这对我也有用。

    • 1

相关问题

  • 死锁的主要原因是什么,可以预防吗?

  • 如何确定是否需要或需要索引

  • 我在哪里可以找到mysql慢日志?

  • 如何优化大型数据库的 mysqldump?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve