Syntax for naming foreign keys - sql

Using:
ALTER TABLE dbo.Table1Name
ADD FOREIGN KEY (colname)
REFERENCES dbo.Table2Name (colname)
I get a foreign key with a name like:
FK___colname__673F4B05
I want it to be named:
FK_Tabl1Name_Table2Name,
...so that it will be easy to read when browsing the DB structure in SSMS. I know I can go back into the GUI and do this, but I want to be able to script it.
So What's the SQL sytnax for adding a name to the FK? Nothing I've found online seems to bother with this.

Here's how you can assign your chosen name to the foreign key constraint:
ALTER TABLE dbo.Table1Name
ADD CONSTRAINT FK_Tabl1Name_Table2Name
FOREIGN KEY (colname) REFERENCES dbo.Table2Name (colname)

Related

Liquibase drop composite key command

How to drop composite key in liquibase using SQL commands ? My composite key doesn't have a name for it.
I tried searching liquibase documentation but it says about primary keys only.
A composite key is a primary key. So if you found a way to drop a primary key, then use it.
As you want to use SQL command, not Liquibase XML, JSON etc., then this is a question how to drop a composite key in SQL. An answer depends on a database engine you use. For example in MySQL, you can do that like this:
ALTER TABLE CITIES DROP PRIMARY KEY;
A primary key from the CITIES table will be deleted even if it is a composite key.
To make it runnable from Liquibase, you just add a comment line with Liquibase meta data. For example:
--changeset Harsh:1
ALTER TABLE CITIES DROP PRIMARY KEY;

HeidiSQL does not allow me to create a foreign key, what can I do?

I'm trying to create some tables in HeidiSQL, the main key allows me to create it without any problem but when trying to create a foreign key I get error (1064) I would like to know if they could help.
Table 1. Where the main key is located
Table 2. Where the foreign key should go
Log in with Root
Create the tables
fill the tables with the given information
Create the main key in id_clientes
Create the foreign key in the table t_clientes put in the cell with the same name bearing the main key (id_clientes)
Those are at least the steps I have been given for this task but I will appear error 1064, even if I try to switch to another cell does not allow me the foreign key.
try this, I believe it will work
ALTER TABLE t_clientes
ADD CONSTRAINT id_clientes_fkey FOREIGN KEY (id_clientes)
REFERENCES t_ventas(id_clientes) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT

Inserting new record and skip if foreign key conflict in sql server 2008 R2

I have the problem similar to this one SQL Server foreign key conflict in a multi values statement? However, in sql server 2008.
While I am reading data from csv file, there is some id already not exist in parent and thus return this error:
INSERT statement conflicted with the FOREIGN KEY constraint
May I know if there is a way similar to MySQL insert ignore. Such that I can simply skip the problematic data.
I accept that if there is no method other than creating a stored procedure with a new temp table (insert into a table without foreign key first, and then re-insert with where foreign_id exists in (select id from parent)).
As I really cannot find any in documentation, asking for ensuring I didn't miss anything.
One general solution which comes to mind would be to temporarily turn off the foreign key constraints, and do the insert. Then, afterwards, you may run a cleanup script/query to remove or rectify child records which are pointing to parents which do not exist. Once your data is in good shape, then turn on the foreign key constraints again.
Read How can foreign key constraints be temporarily disabled using T-SQL? to learn how to disable/enable a foreign key constraint for a single table:
ALTER TABLE MyTable NOCHECK CONSTRAINT MyConstraint -- disable
ALTER TABLE MyTable WITH CHECK CHECK CONSTRAINT MyConstraint -- enable

Why do I get SQL state:23503 when I try to add a constraint to an existing table in Postgres?

I have table Advisor which is a special Userand contains only id and user_id (for now!) and I'm trying to make user_id a foreign key with the following script:
ALTER TABLE advisor
ADD CONSTRAINT advisor_user_id_fkey
FOREIGN KEY (user_id) REFERENCES "user" (id);
Which I think should work, however I get this error:
ERROR: insert or update on table "advisor" violates foreign key constraint "advisor_user_id_fkey"
SQL state: 23503
Detail: Key (user_id)=(44) is not present in table "user".
I thinks this is weird because I'm saying it should refer to user.id and not user.user_id, but obviously I'm doing something wrong.
Does anyone have any idea about this? Thanks.
update: if anyone is wondering why "user" and not user, well pgAdmin doesn't like user, because it thinks it's the owner of the database.
Your error message states that you have an entry in your "advisor" table (44) which does not exist in your "user" table.
Your Foreign Key is defined on "advisor" and stipulates that the "user" table is the parent table. Perhaps you have that Foreign Key defined backwards?
About the "user" issue, "user" is a reserved word, that's why pgadmin is having trouble with it. If you have the ability to change that, I recommend it. See reserved words at: http://www.postgresql.org/docs/current/static/sql-keywords-appendix.html.

MS SQL Server - What is the value of WITH CHECK in a foreign key constraint?

When I have SQL Server Management Studio generate a table creation script for me, the foreign key constraints are a bit different than how I would write them.
Here is one:
ALTER TABLE [dbo].[GeoBytesCountries]
WITH CHECK
ADD CONSTRAINT [FK_GeoBytesCountries_MapReferenceId]
FOREIGN KEY ([MapReferenceId])
REFERENCES [dbo].[GeoBytesMapReferences] ([MapReferenceId])
GO
ALTER TABLE [dbo].[GeoBytesCountries]
CHECK CONSTRAINT [FK_GeoBytesCountries_MapReferenceId]
GO
I would write this foreign key constraint without "WITH CHECK" and the 2nd "CHECK CONSTRAINT" statement and expect to get the same functionality.
Can someone explain to me the value of the using "WITH CHECK" and a separate "CHECK CONSTRAINT" statement when you are writing a foreign key constraint for a table?
Or is the code below completely / functionally equivalent to the code above?
ALTER TABLE [dbo].[GeoBytesCountries]
ADD CONSTRAINT [FK_GeoBytesCountries_MapReferenceId]
FOREIGN KEY ([MapReferenceId])
REFERENCES [dbo].[GeoBytesMapReferences] ([MapReferenceId])
GO
The way I see it, the two step approach allows you to at least keep more "bad" data from getting in assuming the with check part fails. That is, your constraint will exist and apply to DML from that point forward, but you may have to do some cleanup on your existing data to make it a trusted constraint.