Does specifying a foreign key make it an index? - sql

I have a table T with a primary key id and foreign key f. Is f automatically indexed when it is specified as a foreign key? Do I need explicitly add an index for f ?

No index is created so yes, you need add explicitly add an index.
Edited to add...
I probably ought to add that the source table/column for the data in table T must have a unique index. If you try and make an FK to a column that isn't a unique index (either as a PK or with a UNIQUE constraint), the FK can't be created.

No, it is a constraint, not an index.
see Are foreign keys indexed automatically in SQL Server?

In case of the foreign key constraint, the foreign key f in table T would be a primary key in the referenced table say T2. In SQL Server a clustered index would be automatically created when T2 got created.
cheers

Related

do not allow duplicates

I am importing data from an access table to SQL. I have a primary key is SQL "quoteID" which obviously doesn't allow duplicates but I'm looking to add that requirement to another field.
Can't seem to find where to set that? perhaps it has to do with the field type??
You can require that a column be unique using a unique constraint or unique index:
alter table t add constraint unq_t_col unique (col);
or:
create unique index unq_t_col on t(col);
Make the field a primary key of the table. Or put an index on that field that enforces unique values.

SQL FOREIGN KEY's

How do prevent a table with 2 FOREIGN key's from having repeated rows with the same value.
Thks in advance.
Use a unique index or constraint:
alter table example
add constraint unq_example_fk1_fk2 unique (fk1, fk2);
A unique constraint and unique index do essentially the same thing. So, you can also do:
create unique index unq_example_fk_fk on example(fk1, fk2);

Oracle SQL Foreign Key constraint that ignores null or historic values

I am trying to add a constrain to a database table that I want to modify. I want to add a constraint on a column so it references a primary key of another tables. Easy enough, I just have to add a foreign key constraint. The problem is that the column already has some values that are null or something that is not part of the table I will be referencing.
My question is how do I add a constraint that references a primary key but can also accept null values (the primary key always has a value) and how to ignore the existing values so far. Is it possible? If the second part is not, I am thinking I could always write a script that updates all the nonsense values (they have a format of sort if that I can reg ex) to null so they only thing I have to figure out is how to add a foreign key constraind that also accepts null values
Firstly, there's nothing stopping you from adding a referential constraint on a column that has NULLs - foreign key constraints are only enforced for non-NULL values.
Secondly, if there are existing values that do not exist in the parent table, and you can't fix them, you do have the option in Oracle to make the constraint only validated for newly inserted or updated rows, using the NOVALIDATE option, e.g.
ALTER TABLE x ADD CONSTRAINT fk FOREIGN KEY (id) REFERENCES parent (id) NOVALIDATE;
The only downside to using the NOVALIDATE option is that the query optimizer will not rely on the FK constraint, and will execute queries with the assumption that there may be rows that do not have a matching parent row.
It would be a good idea if you are able to fix the missing values, afterwards to alter the constraint to VALIDATE.
Primary keys can not contain NULL values in all databases (proof)
To add a foreign key you should do something like:
ALTER TABLE table1
ADD CONSTRAINT fk_table1_2
FOREIGN KEY (column1)
REFERENCES table2(column2);

Adding Constraint with multiple foreign keys

I have a SQL database opened with visual studio, and I need to add some constraints to a table already created. I need a foreign key, which already has a foreign key from a third table. To explain better ,
Table ANIMALI needs a foreign key from table GABBIA, which has already a foreign key from table STANZA. This was the code I came up with:
ALTER TABLE ANIMALE ADD CONSTRAINT REF_ANIMA_GABBI_FK FOREIGN KEY (n_stanza, n_gabbia) REFERENCES GABBIA(n_stanza, n_gabbia);
This gives me an error, n_stanza is a column id not valid. I think it's about the fact that the ID for the class GABBIA is taken from joining n_gabbia and n_stanza, the latter being a key in class STANZA.
Can anyone help me out?
In order for your ALTER TABLE statement to work as written, both tables (not classes) "ANIMALE" and "GABBIA" must include the columns "n_stanza" and "n_gabbia".
In addition, in the table "GABBIA", there must be either a primary key constraint or a unique constraint on the pair of columns "n_stanza" and "n_gabbia". That is, you need something like either primary key (n_stanza, n_gabbia) or unique (n_stanza, n_gabbia) in the table "GABBIA".

Add Sort Order for FOREIGN KEY CONSTRAINT on SQL Server

How do I add the Foreign Key Sort order in the following statement:
ALTER TABLE [dbo].[ActionLog]
WITH CHECK ADD CONSTRAINT [FK_ActionLog_Order] FOREIGN KEY([OrderID])
I want the OrderID to be descending.
A foreign key constraint only ensures the values already exist in the table referenced, not order. For referential integrity, the database doesn't care what order of the data is.
The only way to ensure order in a resultset is to use an ORDER BY clause.
Having an order on a foreign key is nonsense. A foreign key is a way to impose a rule that the value in the OrderId field (in your example) must exist in another table. It has nothing to do with the clustering of your table (which is the only way to impose an order in a table).
Incidentally, you haven't shown the complete statement as there shuold be a REFERENCES table(column) at the end of your ADD CONSTRAINT statement.
If you really want the data in your table to be stored in OrderID order then you need to add a clustering index, such as
CREATE UNIQUE CLUSTERED INDEX CIX_Action_log
ON Action_log (OrderID)
GO
But I have to question your motives for doing this.
A foreign key constraint on ActionLog.OrderID referencing Orders(OrderId) will require that the columns referenced in Orders do form a unique key, thus there will need to be some kind of constraint or index on the referenced table before you can apply the foreign key constraint in the first place. At that point, you could specify the index order on the referenced table. Such a constraint is for getting from ActionLog to Orders efficiently, and typically does not have any/much say in accessing ActionLog.
If you need to pull ActionLog by OrderID, you would have to add an index yourself.