Tag Info

Hot answers tagged

13

Oracle: Since Oracle doesn't index entries where all indexed columns are null, you can use a function-based unique index: create table foo(bar integer, chk char(1) not null check (chk in('Y', 'N'))); create unique index idx on foo(case when chk='Y' then 'Y' end); This index will only ever index a single row at most. Knowing this index fact, you can ...


9

SQL Server: How to do it: The best way is a filtered index. Uses DRI SQL Server 2008+ Computed column with uniqueness. Uses DRI See Jack Douglas' answer. SQL Server 2005 and before An indexed/materialised view which is like a filtered index. Uses DRI All versions. Trigger. Uses Code, not DRI. All versions How not to do it: Check constraint with a ...


9

I think this is a case of structuring your database tables correctly. To make it more concrete, if you have a person with multiple addresses and you want one to be the default, I think you should store the addressID of the default address in the person table, not have a default column in the address table: Person ------- PersonID Name etc. DefaultAddressID ...


7

In Oracle, one way to enforce this sort of constraint in a declarative fashion would be to create a materialized view that is set to refresh fast on commit whose query identifies all the invalid rows (i.e. BookAspectRating rows that have no match in BookAspect_view). You can then create a trivial constraint on that materialized view that would be violated ...


7

I think you can, using a "diamond" relationship diagram: CREATE TABLE Artist ( artistID INT NOT NULL , name VARCHAR(100) NOT NULL , PRIMARY KEY (artistID) ) ; CREATE TABLE Album ( artistID INT NOT NULL , albumID INT NOT NULL , title VARCHAR(100) NOT NULL , PRIMARY KEY (artistID, albumID) , FOREIGN KEY (artistID) REFERENCES Artist (artistID) ) ; ...


6

This business rule can be enforced in the model using only constraints. The following table should solve your problem. Use it instead of your view: CREATE TABLE BookAspectCommonTagLink ( BookID INT NOT NULL , AspectID INT NOT NULL , TagID INT NOT NULL --TagID is deliberately left out of PK , PRIMARY KEY (BookID, AspectID) , FOREIGN ...


5

Possible approaches using widely implemented technologies: 1) Revoke 'writer' privileges on the table. Create CRUD procedures that ensure the constraint is enforced at transaction boundaries. 2) 6NF: drop the CHAR(1) column. Add a referencing table constrained to ensure its cardinality cannot exceed one: alter table foo ADD UNIQUE (bar); create table ...


5

This kind of problem is another reason why I asked this quiestion: Application Settings in Database If you have an application setting table in your database you could have an entry that would reference the ID of the one record you want to be considered 'special'. Then you would just look-up what the ID is from your settings table, in this way you dont ...


4

The restrictions on truncating tables include: You cannot truncate the parent table of an enabled foreign key constraint. You must disable the constraint before truncating the table. An exception is that you can truncate the table if the integrity constraint is self-referential. This is presumably because truncate is DDL and doesn't do any checks ...


4

No, there is no value in adding an additional "primary key" to this table. Your joins are only ever going to refer to ProducerID and ProductID, so it is just dead weight. IMHO. Though I agree with @Shark that the join table doesn't even seem to be needed here, unless you are going out of your way to not change the schema of the existing tables in any way. ...


4

For those who use MySQL, here is an appropriate Stored Procedure: DELIMITER $$ DROP PROCEDURE IF EXISTS SetDefaultForZip; CREATE PROCEDURE SetDefaultForZip (NEWID INT) BEGIN DECLARE FOUND_TRUE,OLDID INT; SELECT COUNT(1) INTO FOUND_TRUE FROM PostalCode WHERE isDefault = TRUE; IF FOUND_TRUE = 1 THEN SELECT ID INTO OLDID FROM PostalCode ...


4

Your table will work fine for this purpose, but you probably want to add an index. If the primary reason for using this table is to take an outside_ticket_id and get the corresponding ticket_id's I would add the following clustered index: CREATE CLUSTERED INDEX [CL_Lookup_OD_ID] on [lookup](outside_data_id) GO If the primary lookup will be the other way ...


4

A structure like yours should probably be solved with: - a multi-column primary key constraint on the m-table (lookup) and - a foreign key constraint referencing the primary key of the 1-table. An optimal index for looking up values in one direction is provided automatically by the primary key of the lookup table. CREATE TABLE ticket ( ticket_id integer ...


4

The simplest approach is to store each relationship exactly once, and enforce that with a check constraint Friend1 CREATE VIEW AllFriendships AS SELECT Friend1, Friend2 FROM Friendships UNION ALL SELECT Friend1 AS Friend2, Friend2 AS Friend1 FROM Friendships If, however, you really need the table with both Friend1,Friend2 and Friend2,Friend1, you could ...


4

One radical solution might be to remove pin_inst completely: part ←────────── pin ↑ ↑ part_inst ←───── pin_inst There is nothing in your question to suggest you actually need the redundant table. For pins associated to a part_inst, look at the pins of the associated part. That would simplify the code to: create table part ( -- ...


3

According to IBM's documentation: "Typically, you need to manually perform integrity processing for a table in three situations: After loading data into a table; when altering a table by adding constraints on the table; and when altering a table to add a generated column." That would be the why you need to do it. We use it right after we use the LOAD ...


3

I imagine that most sports leagues don't change their hierarchy of conference, division, etc... very often so it's probably safe to have each level in its own table. If your goal is a system that can handle different leagues that have different hierarchy depths, then the recursive table approach is probably better. I don't know how easy it would be to do ...


3

In SQL Server 2000 and over you can use Indexed Views to implement complex (or multi-table) constraints like the one you're asking for. Also Oracle has a similar implementation for materialized views with deferred check constraints. See my post here.


3

The structure you've created seems like a perfectly reasonable lookup table. CREATE TABLE lookup1 ( outside_data_id integer NOT NULL , ticket_id integer NOT NULL ); I would probably add an index to this table like: CREATE CLUSTERED INDEX IX_Lookup1 ON Lookup1 (outside_data_id, ticket_id) WITH (ALLOW_PAGE_LOCKS=ON, ALLOW_ROW_LOCKS=ON, ...


3

Personally I would put the TimePoint attributs into the event table. But if you prefer two tables, you should be able to achieve that with foreign keys together with NOT NULL columns. But that will only work if you have a database that supports deferrable constraints (Oracle and PostgreSQL come to mind), otherwise there is no way of inserting new values ...


3

Arguments can be made either way depending on whether you want to handle populating these tables using triggers or the application(s). I think this depends on what exactly you want to store in these logging tables, if you're logging anything specific to your 3 "data" tables then you'd be better off with 3 logging tables (1 logging table per "data" table ...


3

Run this command : SHOW CREATE TABLE log_url\G This will give you a display of the SQL needed to create the table. It will also show you the indexes and constraints for that table. The first index (key 1) in that table must have a column called url_id. From the error message, you are being told that there exists a row in log_url that has 41721 already ...


3

When implementing subtypes of an entity there are various solutions on Oracle: Create one wide table with the attributes of all subentities. Entity type stored in a field and used to enforce business rules ("You may only fill column A for entity type B"). Create one master table with the shared attributes and per subentity another table. And relations back ...


3

Basic answer I suggest to use the geometric type box and combine that with an exclusion constraint (Postgres 9.2+). Should be the perfect solution to your problem. The GiST index this is implemented with (automatically) also supports certain queries. Combine it with with equality on board_id to hold multiple boards in a single table. You'll need the ...


3

Support for array foreign keys was worked on with the goal of getting it into PostgreSQL 9.3, but it didn't make the cut for the release due to performance and reliability problems. It doesn't seem to be being worked on for 9.4. At this time, you need to stick to the usual relational approach of using a "join table" to model an m:n relationship. CREATE ...


3

Since you are using SQL Server Compact Edition, which doesn't support triggers that could solve this issue, my recommendation is either: write your own manual DELETE statements in the proper order, rather than use CASCADE change the design use Express / LocalDB instead of Compact Edition, which is extremely limited and has been deprecated


2

Been a while since I've worked in sybase but (from memory) the following sql should point you in the right direction: select * from sysobjects so inner join syscolumns sc on so.id = sc.id where sc.name = 'field name' you can also check out the sybase books online (system tables)


2

If you have a one-to-many relationship between Producers and Products (in other words, a product can only belong to one producer), than it would make sense to just put a foreign key reference directly in your Products table: One-To-Many create table Producer ( id int identity(1, 1) not null primary key clustered, Name varchar(100) not null ) go ...


2

For OneDayWhen: Assuming the Product and Order_Detail tables: create table PRODUCT ( PRODUCT_ID NUMBER(38,0) not null, PRODUCT_NAME VARCHAR2(100) not null, PRODUCT_DESCR VARCHAR2(4000) null, ACTIVE_FLAG NUMBER(1,0) DEFAULT 1 not null ); create table ORDER_DETAIL ( ORDER_DETAIL_ID ...



Only top voted, non community-wiki answers of a minimum length are eligible