Questions tagged [constraint]

A declarative mechanism such as a check or foreign key that enforces some data integrity rule in a database.

Filter by
Sorted by
Tagged with
0
votes
1answer
29 views

FOREIGN KEY Constraint Failed: SQlite3

I have two tables, created as below, sqlite> CREATE TABLE Artists( ...> ArtistId INTEGER PRIMARY KEY, ...> ArtistName TEXT NOT NULL ...> ); sqlite> CREATE TABLE Albums( ...&...
0
votes
1answer
25 views

Query data that does not fulfill constraints in table in Oracle DB

In Oracle I have a lot of tables in a given schema. Some of the table constraints in the schema have been disabled. I have to know for all tables in a given schema with any of the constraints ...
0
votes
1answer
20 views

Constraints between tables on a variable

im aiming to add a constraint on the number of bicycles allowed to be stored in each terminal - the number differing between terminals. These are my original tables create table terminal( ...
0
votes
1answer
47 views

Postgres: foreign key constraint for components of a composite type?

In Postgres, is it possible to set up a foreign key contraint involving components of a composite type, in a situation like the following one? Suppose I have CREATE TABLE main ( -- a and b are ...
0
votes
0answers
20 views

generating a log for Cascade deletes

I have a database which supports a .Net application and i have several parent tables and child tables database design where several tables are linked using FK and there is cascade delete enabled. So ...
0
votes
1answer
30 views

Compound unique constraint with NULL values

With the following table structure CREATE TABLE favorite_comments( id SERIAL PRIMARY KEY NOT NULL, opinion_id BIGINT REFERENCES opinions(id), review_id BIGINT REFERENCES reviews(id), ...
1
vote
0answers
24 views

Constraint creation on a large table is too slow

There's a table with 723M rows, large amount of data and not partitioned. We're adding a constraint to the table as follows: ALTER TABLE ONLY my_table ADD CONSTRAINT partial_constraint EXCLUDE USING ...
0
votes
1answer
28 views

Postgres: Can I associate index with FK constraint?

Postgres FK constraint and index are independent. Recently I was looked for unused indexes and found trick to eliminate indexes needed for constraint enforcing by: WHERE NOT EXISTS (SELECT 1 ...
4
votes
1answer
37 views

Prevent drop column with index on it in PostgreSQL

In PostgreSQL when you drop column in a table, all indexes and constraints which use this column will be also dropped silently. If you overlook such situation, you can get serious problems in ...
0
votes
1answer
17 views

Enforce unique constraint on 1 of n values

Let's say I have a table like this. create table foo ( id serial primary key, kind smallint not null check (kind >= 0 and kind < 3) ); With that check constraint, Postgres will only ...
0
votes
1answer
15 views

How to ensure all records of a header are under the same fiscal year?

Say I have the following schema: BUDGET_YEAR_TARGET INTEGER ID BUDGET_DATE_TARGET INTEGER ID INTEGER BUDGET_YEAR_TARGET_ID--Foreign key DATE DATE How can I ensure that any records ...
-2
votes
1answer
35 views

SQL Check Constrain with ONLY IF statment

I have a table with a check constraint that enforce a not null for the data column if the havedata column is true. is there away to modify the constraint to not allow case 3 to happen. i.e Data is ...
1
vote
1answer
49 views

How do I reference a row which has two foreign keys as its primary key?

I am currently trying to setup a database managing players and teams for a competition. Players are competing in teams of two but may be shuffled. As every team combination can only exist once, I ...
2
votes
0answers
40 views

Design issue in transitive dependencies between tables

This is a made-up scenario, but I imagine that it can be of general interest, so I'll post it here. Imagine the following business rule: A course has a grade scale that determines what grades a ...
2
votes
0answers
59 views

Can you force sets of rows to be unique?

Let's say we have a table like this with a primary key of (nr, key): nr | key | val ---|-----|------ 1 | 1 | a 2 | 1 | a 2 | 2 | b 3 | 1 | a 3 | 2 | c Every set of rows (grouped by nr ...
0
votes
1answer
177 views

Covering and overlapping constraints

Given that there are 15 entities in E1, 20 entities in E2 and 25 entities in E3, what's the maximum and the minimum number of entities in E0? It's also known that the ISA hierarchy follows the ...
0
votes
1answer
16 views

Setting constraint so that new value in “Table A” must already exist in “Table B”, else return error?

I'm trying to set up some kind of constraint so that I can only enter values in "Table A", if that value exists in "Table B". I want this to act sort of like a selection list, where there are only a ...
1
vote
1answer
22 views

Re-adding constraint after ALTER fails on row that exists

I'm getting a failure trying to add a constraint that previously existed on a table after having dropped it to do an alter: mcqueen=# ALTER TABLE ONLY public.mcqueen_base_imagemeta2 mcqueen-# ADD ...
4
votes
1answer
129 views

Add INCLUDE term to primary key index in Postgres 11

I work with Postgres 11 and would like to change the index that backs the primary key of a large table (~750 million rows). The primary key is the bigint column id and I would like to include an extra ...
2
votes
2answers
55 views

How to have a “kinda” unique column

How can I have a MySQL database number column that only allows one 1, but infinite 0's? Some type of constraint or something. Elaboration To clarify what I mean and why I want this: Imagine you ...
0
votes
1answer
50 views

How to list all constraints of a table in PostgreSQL by schema?

Yes, I'm aware there is a question very similar to mine, the difference is that I want all constraints, including not null, which no answer is able to do.
0
votes
1answer
70 views

Exclusion constraint on recursive foreign key (on the example of a Tree)

I have the table with recursive foreign key. It's just hierarchical tree structure: CREATE TABLE tree ( id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, value ...
1
vote
1answer
52 views

Understanding the recreation of index in Table partitioning

I need your help. I have[AdventureWorks2014] database and I wanted to perform partitioning on [Production].[TransactionHistoryArchive] table based on [TransactionDate] column. [Production].[...
3
votes
1answer
1k views

SQLServer add a default value for a column [closed]

I am using MS SQL Server. I have an "action" table with an "action_type" column that I want to set a default value of "to do" for. My DDL in MS SQL Studio errors! I have read many many articles trying ...
1
vote
1answer
107 views

Constraint on multiple columns?

I have a table edges, which describes relations in a graph: CREATE TABLE IF NOT EXISTS edges ( src INT NOT NULL REFERENCES nodes(id) ON UPDATE CASCADE ON DELETE CASCADE ,tgt INT NOT NULL ...
1
vote
1answer
61 views

Purging of unreferenced data via unconditional DELETE, by leaning on foreign key constraints

I'm designing a database from which old data has to be purged regularly for legal reasons, and I'm trying to figure out the best way to organise the purging. Dealing with the main tables is trivial, ...
3
votes
1answer
91 views

Enforcing a “disjoint or identical” constraint

Suppose that F is an Oracle table with two columns, X and Y, such that the constraint CONSTRAINT unique_xy_pairs UNIQUE (X, Y) is satisfied. For each value [x] of F.X, let S[x] represent the set ...
0
votes
1answer
66 views

Add foreign key constraint to an existing table column, referencing a column in a newly created table

Let's say I have one table defined as this CREATE TABLE sales ( agent_id integer references agents(agent_id), sale_date date, amout numeric(10,2) ); Then an ETL process fills ...
1
vote
0answers
65 views

postgres boolean function and check constraint [duplicate]

I been using postgres for a while. Mostly simple stuff. Now trying to implement a small perl function to check for a single valid email address. My employees table should have either a single valid ...
1
vote
1answer
45 views

Can I restrict a SQL Server spatial field to hold values of specified spatial types and SRIDs?

I am trying to understand geography data types so I can integrate my database more closely with GIS processes. From my reading, it appears that a geography field can hold many different types of ...
1
vote
0answers
22 views

Referential integrity based on data more than a table away

Notes on tables A, B, C, D, E B.aid and C.aid reference A.aid. D.cid references C.cid and D.did is the primary key. D is a C. cid and did are pre-assigned and I cannot change them (they need to ...
14
votes
2answers
707 views

Can SQL Server create collisions in system generated constraint names?

I have an application which creates millions of tables in a SQL Server 2008 database (non clustered). I am looking to upgrade to SQL Server 2014 (clustered), but am hitting an error message when under ...
0
votes
1answer
31 views

How to add a constraint to a table using a partial index?

I have a simple table: create table item ( lan text not null, disabled date ); My aim is to ensure uniqueness for the lan column over all rows, where disabled is null. I created a partial ...
1
vote
2answers
38 views

How to get number of PK used as FK?

How to get number of PK used as FK? Table 1 has a column ID (PK). Table 2 & 3 has a column ID (FK - refers Table 1 PK). How to get Value 1 (PK) has used in table 2 & 3 or not without ...
2
votes
2answers
95 views

Add Constraint to check value exists in a non-unique composite key column

I have the following table, which has a composite primary key: CREATE TABLE dbo.Seqs( SequenceKey nvarchar(10) NOT NULL, UsageSequence smallint NOT NULL, col1 bit NOT NULL, col2 ...
1
vote
2answers
117 views

How does ON DELETE CASCADE relate to JSON?

For relational databases, there is the notion of ON DELETE CASCADE. When working with JSON, how do these two concepts relate? Is ON DELETE CASCADE supported for JSON? Can you give an example of a ...
0
votes
1answer
77 views

Cannot import table due to zombie constraint

I tried to refresh my copy of the production database by emptying and importing a SQL file. I've done it hundreds of times, only this time the import failed. I tried dropping & recreating the ...
1
vote
2answers
57 views

Is there Such Thing As The Opposite of Unique Constraint

I want to be constrain my database with what is essentially the opposite of a UNIQUE constraint. I want to only be able to add a new row if some combination of fields has been added before. Note: I ...
1
vote
1answer
28 views

Constrain input values

I am working on limiting input in certain fields in my Postgres database, and I have a working solution using ENUM like so: CREATE TYPE label AS ENUM ('foo','bar'); However, I was browsing around ...
0
votes
0answers
465 views

SQL Default CONSTRAINT not working

I have the table below with some basic information with two defaulted columns CREATE TABLE [dbo].[INSPECTION]( [InspectionID] [int] IDENTITY(1,1) NOT NULL, [LocationID] [int] NOT NULL, [...
1
vote
2answers
278 views

How to name a constraint which was unnamed during the table creation?

I had created the table by specifying the columns as primary key and not null without giving them any constraint names and I have populated the data. I want to give names to them now. How do I do ...
4
votes
3answers
148 views

Enforcing distinct number of trailing spaces

I'm working on converting a SQL Anywhere (ASA) 5.5 based application and its database to Microsoft SQL Server. Existing is a table with a primary key clustered on two columns, code_type char(4) and ...
0
votes
2answers
391 views

Duplicate result when I select with distinct values from foreign key constarint

I want to select values from two different tables which have one to many relationship between them. During select it's retrieving duplicate values even when I use distinct and group by. Above table is ...
1
vote
1answer
37 views

Constraint via Foreign Key

I have the following schema in SQLite: CREATE TABLE calendars ( id INTEGER PRIMARY KEY, title TEXT NOT NULL ); CREATE TABLE entry_types ( id INTEGER PRIMARY ...
-1
votes
1answer
179 views

cascade constraints in Oracle

Can we use the cascade constraints instead of specifying FK in our code I mean can we Use this execute immediate 'ALTER TABLE @PCWSCHEMA@pcwscannermaster DROP CASCADE CONSTRAINTS'; instead of the ...
0
votes
1answer
327 views

Auto incrementing a foreign key value the same as the primary key of the parent table in SQL when inserting data?

I have the following two connected tables as shown below in the Microsoft SQL Server. When inserting data from visual studio, the foreign key in the Reservations table (Customer_ID) should be auto ...
2
votes
1answer
217 views

Making a column unmodifiable/non-updatable after its row's initial creation?

What is the most idiomatic way to make a column unmodifiable/non-updatable after its initial creation via INSERT in Oracle? CREATE TABLE my_table ( id NUMBER NOT NULL CONSTRAINT ...
1
vote
1answer
325 views

Select subquery CHECK constraint DB2

I have 2 tables: TVSHOW and ACTOR. I want to reject records if the inserted actor in TVSHOW does not exist in the ACTOR table using DB2. db2 "ALTER TABLE TVSHOW ADD CONSTRAINT check_actor CHECK (...
2
votes
0answers
53 views

Is this “idiosyncratic” method to manage constraints declarations beneficial?

I just wanted to get feedback from actual DBAs, based on their experience and knlowledge of database theory, in regards to this. At my company, we have a large .sql file that holds all of our schema ...
3
votes
2answers
1k views

Find all `NaN` values in a postgresql database

Numerics and floats can store NaN https://www.postgresql.org/docs/current/static/datatype-numeric.html I have a working program with a PostgreSQL database that does not have any constraint about NaN. ...