Need help adding a constraint to check values across tables in SQL Server 2000 - sql-server-2000

I have two tables and I want to use the database to enforce a constraint against them.
Table A has a one to many relationship with table B, that is many table B rows belong to exactly one table A row. Table B has a column of the Table A primary key that it belongs to. Anyway, I want to add a column to both table A and table B where the value of it in a table A record must equal the value of it in the corresponding table B records.
I want to tell SQL Server 2000 to disallow updates or additions of table B records unless the values in the sister columns are the same. How can I do this? I tried using a check constraint but it doesn't like subqueries. Will I have to venture into the world of triggers? Is there another way?
Any help would be appreciated.
Thanks
Isaac

What you're trying to do sounds like a foreign key relationship - but you already seem to have that (Table B has a column of Table A primary key).
I don't think you can define such a constraint - CHECK constraints cannot span tables, as far as I know. So in the end, you will probably need to use triggers instead on both tables to achieve this.

Related

Is this ERD correct?

This ERD is part of my school work and something doesn't seem right. The ERD table 'Course' looks like its referencing 2 tables. Is the column titled 'Qual_Code' in table 'Course' from the 'Prerequisite' table, the 'Qualification' table or both? I don't think its both because you cannot have a single column with a foreign key that references two different tables.
Help because I have to write the SQL codes for this!
At the first glance, it appears that the column qual_code of the COURSE table is redundant. When writing the DDL code for this, you can include or exclude this column - see the following examples:
course table with qual_code column
course table without qual_code column
The PREREQUISITE table maps courses to qualifications. The PK constraint in this table will not allow NULLs in either of the columns ie if a row gets INSERTed, it must contain values for both course_num and qual_code.
I don't think its both because you cannot have a single column with a
foreign key that references two different tables.
It seems that you are misinterpreting this. The PK column of the QUALIFICATION table is referenced twice (FK columns reference a PK or UNIQUE column, not the other way round -> see the DDL code on DBfiddle).

Find primary key in star schema table

I have encountered a situation while developing a star schema. I have a table like this
Name
Email
amy
amy#gmail.com
jess
amy#gmail.com
I want to find the key column as foreign key for Fact table as you can see there is a duplication of records if look individually but unique if consider both column as a key column
Your help will be highly regarded
I am assuming from your question that the table is a dimension? If that is the case then it should have a surrogate key as its PK (as every dimension table should) and you use this to join to fact tables.

How to make a column in a Postgres table with limited selection

Noob to SQL here. Is there any way to constrain (not in terms of primary/foreign key) the selection of a column? Specifically, I want to a table called “occupation” with only two options, either clown or doctor.
There are two methods. The simplest is a check constraint:
alter table t
add constraint chk_t_occupation
check (occupation in ('clown', 'doctor'));
The second method is to have an occupations table with two rows. You can then have an occupationId column with a foreign key reference to this table.

Insert into tables with primary and foreign key at same time

Very new to SQL and have spent a day on this already.
Here are my two tables:
Centre(cid, name, location, nurse_supervisor)
Nurse(nid, name, centre_id, certificate)
I have a big problem. The (nurse_supervisor) in Centre is a foreign key to Nurse (nid).
The (centre_id) in Nurse is a foreign key to (Centre cid).
I can't figure out how to populate these tables. I have tried:
INSERT ALL, which produces "A foreign key value has no matching primary key value"
I have tried removing the foreign key constraints and adding them after populating the tables but when I do that it says I can't add a constraint to tables with preexisting data.
I tried removing NOT NULL - but realized that was silly as the constraints will be enforced anyways.
Everything I look through says populate the parent table first and then the child, but these tables are linked to each other.
I am using SQL developer.
This is a poor schema design, but one way to get around it would be to:
Make both centre_id and nurse_supervisor columns NULL in the two table definitions
Insert all rows into both tables, but with NULL for those two columns
Update centre_id to the correct value for each row in the Nurse table
Update nurse_supervisor to the correct value for each row in the Centre table

Question About seting MS Access Relationship type

I have two tables A,B like
A(A_pk_id,A_name), B(A_pk_id,B_pk_id,B_Name);
Here A_pk_id is primary key of table A, And table B has composite primary key comprising of two fields A_pk_id(Table's primary key),B_pk_id,
Now whenever i tried to define relationship between these two tables [on A_pk_id] MS Access 2007 set their relationship type 1 to 1, but i want it to be 1 to many, cardinality of table must be set to 1.
Can any body guide how i could accomplish my goal.
Regards
Ahsan
If you're getting a 1:1 relationship, it means you're joining two fields that both have a unique index on them (regardless of whether both are PK or not). The many side needs to have a non-unique index.