How can to set column field with related column field in another table? - sql

I have the following two sql tables:
The GlassesID Column in Glasses table is the primery key defined as auto Increaseble.
The GlassesColor table has GlassesID(not auto Increaseble) colomn that defined as Foreign Key.
When Glasses table get a record (from stored procedure)GlassesID automatecly get value.
The GlassesColor.GlassesID column must be set with value from Glasses.GlassesID Column.
My question is how can i implement this? i,e... How can i set column field with related column field in another table?

Immediately after you insert a record into Glasses table, get the ID from Glasses table
declare #GlassesID as int
select #GlassesID = scope_identity();
Then you can use #GlassesID to insert into GlassesColor table.
insert GlassesColor(GlassesID, .....)
values(#GlassesID, .....);

Related

SSMS will not let me create a PK FK relationship

First, let me say that I am a newbie and that I have read many other posts with the same problem. I have a table called "AllPeople", and in that table I have an integer column called "Ethnicity", which I want to be a foreign key that points at a record in my "RefEthnicities" table. I keep getting the following message:
Unable to create relationship 'FK_AllPeople_RefEthnicities'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_AllPeople_RefEthnicities". The conflict occurred in database "MVC-Cemeteries-Dev", table "dbo.RefEthnicities", column 'ID'.
I set up the relationship in the "AllPeople" table and told it that the primary key is the ID column in the "RefEthnicities" and the foreign key is the "Ethnicity" column in the "AllPeople" table. What am I doing wrong? My RefEthnicities table is new; no data in it.
While in design mode I set the ID field in the "RefEthnicities" table set as primary key by clicking the small box to the left of the name "ID", and down below in this same window in the column properties tab, I told it to set the index specification to "yes".
I am sure it is something simple that I am doing but I can't figure it out.
Error Message
Constraint Folder
Setting Up PK FK Link
As my limited information in the question, there 2 possibilities
NULL or Blank '' value for column Ethnicity in table AllPeople
SELECT A.Ethnicity,A.*
FROM dbo.AllPeople A
WHERE ISNULL(A.Ethnicity,'')=''
Some values column Ethnicity in table AllPeople don't have parent in column ID in table RefEthnicities
SELECT A.Ethnicity,R.ID, *
FROM dbo.AllPeople A
LEFT JOIN RefEthnicities R
ON A.Ethnicity=R.ID
WHERE R.ID IS NULL
If you get any rows in two queries, then you need to fix data in column Ethnicity in table AllPeople.
Read
Ok this still makes no sense. If I create two brand new tables with the following:
table1
ID primary key int not nullable
value varchar
table2FK
table2
ID primary key int not nullable
value varchar
and in table1 I make a relationship between table2FK and Table2.ID, it works perfect with no data saved in the tables. If I use the exact same process in my AllPeople and RefEthnicicties tables, I get the error. This makes no sense. What am I missing?
adam
That fixed it. many thanks. I had a record in my AllPeople table for ethnicity that had a value of 0. Since I didn't have a record in the RefEthnicity Table with an ID of 0, it was telling me that I couldn't do this.
adam

INSERTING a single field into a table, referenced from another table

I need to insert a field in a that references an id field in another table.
The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)
INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'
There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.
table - codetype
id bigserial primary key
codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column
Table - typecategory
ID big serial primary key
categorydescription varchar
If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.
Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL
maybe
UPDATE codetype c
SET c.typecategoryid = t.id
FROM typecategory t
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types'

SQL server inserting values from one table to another

I am trying to insert a column of data from one table to another. The table I am trying to insert into is called MU.PROVIDERS. The table I am inserting from is called Sheet1$. (Imported from an excel sheet). The columns they have in common is called "NPI", a key that is common for all providers, so I am trying to insert based on this key. Not every NPI value in the Sheet1$ will put a corresponding RELAY_ID value into the 'MU.PROVIDERS' table. (There are more NPI's in the 'MU.PROVIDERS' than in Sheet1$) My query is as follows:
INSERT INTO [MU.PROVIDERS] (RELAY_ID)
SELECT h.RELAY_ID
FROM Sheet1$ as h JOIN
[MU.PROVIDERS] as i ON h.NPI = i.NPI;
I am getting the error:
Cannot insert the value NULL into column 'NPI', table 'MU.PROVIDERS'; column does not allow nulls. INSERT fails.
I do have the NPI column set as the primary key on the 'MU.PROVIDERS' table, but I am not inserting anything into this column so I do not understand the problem.
I think you want an update. Insert adds new rows. Update changes the values of columns:
UPDATE i
SET relay_id = h.relay_id
FROM mu.Providers i JOIN
Sheet1$ h
ON h.NPI = i.NPI;
You may have defined the NPI column as the primary key.
But the primary key needs a unique value to identify a row.
If you don't provide the value with your INSERT statement you should define the NPI column as IDENTITY(1,1) to automagically create a new identity value.

SQL sever, Make a existing column primary and set to auto increment

I have old SQL sever table with 5000 rows. It has a column called OrderID which has the data type int. But this table doesn't have a primary key and OrderID is not on the sorted order. Can you please tell me how can I make this OrderID column the primary key and make it auto increment
You can't add identity to an existing column.
Your best option is to create a new table with the same structure and an identity column, set identity_insert on and then copy your records from the old one into the new one.
Check out this answer from the MS SQL Forum
You can't add identity to existing column. Create a new column "new_OderId" , copy data from "OderId" column paste in "new_OderId" column.
#add new column to Order_table
alter table Order_table add new_OderId int
#copy data from OrderId to new_OrderId
update Order_table set new_OrderId=OderId
#drop OderId column
alter table Order_table drop column OrderId

How to insert a row into a table that has only a single autoincrement column?

My table has only a single column calld id. This column is an autoincrementing key (I am using it for sequencing). I want to do something like: insert into sequencer; but this gives me SQL errors because I guess I need to have a values portion. But there are no other columns in the table and I want the id column to be autoincremented. I'd also rather not hack this by just adding another dummy column. Thanks.
INSERT INTO table SET id = NULL;
or
INSERT INTO table VALUES (NULL);
When inserting NULL into an auto-increment column, mysql will generate a new number instead.