Adding a column to a table with a primary key - sql

I'm trying to replace a column in an existing table (product table) from a category, to a productCategoryID which is also a primary key, from the ProductCategory table. I've pasted my progress so far, but am stuck.
alter table product
Add ProductCategoryID smallint
constraint PK_ProductCategory Primary Key (ProductCategoryID)
references ProductCategory(ProductCategoryID)
Thanks in advance for your time and help!

You have to first add a column. Then you can add a constraint to the table. These steps can't be slammed together. And your notion of a primary key referencing a column in another table makes no sense. That is a foreign key. I suspect you want something along these lines.
alter table product
Add ProductCategoryID smallint
alter table product
add constraint FK_ProductCategory Foreign Key (ProductCategoryID)
references ProductCategory(ProductCategoryID)

Related

Away around Error: There is no unique constraint matching given keys for referenced table

Been at this for some time now. I'm getting this psql error when attepmting to create a joins table (product_type_combo):
There is no unique constraint matching given keys for referenced table product
I'm receiving this because the field (customer) I'm referencing from the table product does not have a UNIQUE constraint.
CREATE TABLE product
(
id SERIAL PRIMARY KEY,
name VARCHAR(128) NOT NULL,
customer VARCHAR,
CONSTRAINT product_c01 UNIQUE (name)
);
CREATE TABLE product_type_combo
(
customer VARCHAR REFERENCES product(customer),
type_id INT REFERENCES type(id),
PRIMARY KEY (customer, type_id),
CONSTRAINT product_type_combo_c01 UNIQUE (customer, type_id)
);
I don't want to have a UNIQUE constraint on the customer field on the product table since I want duplicates to appear there. I only want the customer & type combo in the joins table to have a constraint.
Is there a way around this?
This is silly:
customer VARCHAR REFERENCES product(customer),
You have a primary key on the table, an id column. You should be using that.
You could define customer to be unique -- the lack of a unique constraint on a column used for a foreign key reference is the cause of the error. However, I strongly, strongly recommend that you use the primary key.
The error is coming from the Foreign Key (REFERENCES) constraint, for reasons listed in the PostgreSQL manual:
A foreign key must reference columns that either are a primary key or form a unique constraint.
Since your "customer" column is not unique, it cannot be the target of a foreign key reference. It also suggests a problem in your data model:
If the relationship is between a product and a type, then referencing the "id" from the products table would make sense.
If the relationship is between a customer and a type, then there should be a table listing all the possible customers, and you can create foreign keys referencing that from both "product" and "product_type_combo" (which would presumably be better named "customer_type_combo").

Cannot find primary key column in table

I added a constraint primary key to my table and the query ran successfully. The dilemma I am facing is I cannot locate the primary key column in my table.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( "Address_of_New_Home","Sq_Ft","Build_Spec", "Realtor_Sale","Can","Actual_Sale_Date")
When I do:
select * from salesdata
It shows all the columns from before and no primary key column (pk_salesdata).
And even more baffling is when:
select pk_salesdata from salesdata
Database shows:
ERROR: column "pk_salesdata" does not exist
I want to add primary key column to the table. I humbly request assistance of databasers.
You create PRIMARY KEY, but...
you create composed primary key from columns "Address_of_New_Home", "Sq_Ft", "Build_Spec", "Realtor_Sale", "Can", "Actual_Sale_Date" - it is not good idea
your primary key have an alias name pk_salesdata, but it's only constraint name
you didn't create new column
If you would like new synthetic primary key column you have to use command:
ALTER TABLE salesdata ADD COLUMN mynewautoincrementid SERIAL PRIMARY KEY;
Primary Kay it's constraint in table when you add a primary key to any column you can select column name to show data
It looks like you've wrapped the columns you want to include in your primary keys in double quotes. That's not how this command works. Drop the quotes re-run the command and see what happens.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( Address_of_New_Home,Sq_Ft,Build_Spec, Realtor_Sale,Can,Actual_Sale_Date)
It might be easier to add a primary index through the SQL GUI.
Here's the MS documentation page for creating a primary key.
https://msdn.microsoft.com/en-us/library/ms189039.aspx
Note: Since your adding a primary key don't expect it to be available as a column.

ALTER COLUMN Command doesn't work SQL Server

i want to add to a primary key in one table a references to the primary key of another table.
my code:
CREATE TABLE[payment]
(ID int Primary key)
CREATE TABLE [tab]
(ID int Primary key references tab2(ID))
Alter Table payment
alter column ID
ADD constraint fk_payment
references tab(ID)
i get the error that the syntax near constraint is wrong, but i don't know what to change
because of the not changeable order of the table Alter table is the only option. to reference from one table to the other doesn't work cause I've references from that table to another one already.
i need two one-to-one-relations from one table to another
If you want to add a FK constraint, just use this code:
ALTER TABLE dbo.payment
ADD CONSTRAINT fk_payment
FOREIGN KEY(ID) REFERENCES dbo.tab(ID)
You don't need to alter the column or table - just add the constraint

Add a foreign key to an existing table

I have a created table I would like to add another foreign key to.
create table serie(
time char(8),
result varchar(2),
mnr varchar(2),
primary key(time, mnr),
foreign key(mnr) references Target(mnr)
)engine=innodb;
This is the table as it is now.
I would like to add another key to another table. Is the best solution to remove the table and just recreate it with the new key or is it possible to just update it?
Use a query like this.
ALTER TABLE serie
ADD CONSTRAINT result FOREIGN KEY (UserID)
REFERENCES ActiveDirectories(id);

How to add composite primary key to table

create table d(id numeric(1), code varchar(2))
After I create the above table how can I add a composite primary key on both fields and also a foreign key?
In Oracle, you could do this:
create table D (
ID numeric(1),
CODE varchar(2),
constraint PK_D primary key (ID, CODE)
);
alter table d add constraint pkc_Name primary key (id, code)
should do it. There's lots of options to a basic primary key/index depending on what DB your working with.
The ALTER TABLE statement presented by Chris should work, but first you need to declare the columns NOT NULL. All parts of a primary key need to be NOT NULL.
You don't need to create the table first and then add the keys in subsequent steps. You can add both primary key and foreign key while creating the table:
This example assumes the existence of a table (Codes) that we would want to reference with our foreign key.
CREATE TABLE d (
id [numeric](1),
code [varchar](2),
PRIMARY KEY (id, code),
CONSTRAINT fk_d_codes FOREIGN KEY (code) REFERENCES Codes (code)
)
If you don't have a table that we can reference, add one like this so that the example will work:
CREATE TABLE Codes (
Code [varchar](2) PRIMARY KEY
)
NOTE: you must have a table to reference before creating the foreign key.
If using Sql Server Management Studio Designer just select both rows (Shift+Click) and Set Primary Key.