Rename table in Sql Server and updating references - sql-server

Hi I need to rename a table, and add some columns to it.
This table has a PK column (Id) and a Self-referencing column (ParentId). These constraints use the old table name in their names.
There are also other tables that use this tables PK as foreign keys.
So, what would be the correct way to do this, asssuming that I need to rename TableA->TableB?
Drop constraints in all tables that reference TableA.Id
sp_rename 'dbo.TableA', 'TableB'
Add constraints that were dropped with new names?
Or, there is some other way?

Related

Foreign Key with wordpress

I have a table (multisite network) where I use blog_id as one of the columns which is also needed on some of my custom php functions. Until now, I haven't used FK which should be appropriate for my tables.
So, can I FK to wordpress wp_blogs table? if yes, if ever I delete that blog will it also delete the entries on my other tables that references wp_blogs?
CREATE TABLE wp_blogs (blog_id INT NOT NULL...)
FOREIGN KEY (blog_id)
REFERENCES wp_options(blog_id)
ON DELETE CASCADE;
If you created a table like above then every time when blog_id will remove from wp_options then all rows from wp_blogs will remove too.
When you remove rows from wp_blogs then no rows won't remove from wp_options
Read about ON DELETE CASCADE in MySQL documentation.

Update table with two columns as unique constraints in SQLite, NOT create table again

I want to update table in my SQLite database and add UNIQUE contraint to two columns in my Data table.
Here is what I was trying , but throws error.
ALTER TABLE Data
ADD CONSTRAINT Data UNIQUE(id, dateofday);
All previous answers are on create table which I don't want. Any Suggestion?
It is not possible.You cannot add a constraint to an existing table in sqlite. You can only rename table or add columns to a table.
Link for your reference : http://www.sqlite.org/lang_altertable.html
However you can use this alternate approach to achieve the same.
Create a temporary table with the same columns as your current table with all the constraints you need.
Then move all the data from the old table to the new temporary table with constraints defined using
INSERT INTO Destination SELECT * FROM Source;
Check if you have copied all the data from the original table to temporary table.
Drop your original table and rename the temporary table to the name of the original table.

Drop column with PK constraint in empty table in a script

Scenario
A table in SQL Server has two or more columns, but the original column with the primary key constraint is no longer needed. So now you want to write a script to drop the original column w/ a PK constraint and put the PK constraint on a different column.
In this example, the table is empty.
Problem
You can't drop the first column without first dropping the PK constraint.
And you can't drop the PK constraint in SQL Server without the exact name of it. (more info here)
....But you don't know the automatically generated name of the PK constraint.
NOTE: If the table is not empty, see this solution:
SQL Server 2008 Script to Drop PK Constraint that has a System Generated Name
(In most cases, this is the best solution.)
Question
The above solution will work, but what is another way to script dropping a column with a PK constraint when you don't know the constraint's name in an empty table?
Another strategy -- besides figuring out the system generated name of the PK constraint so you can drop it as described here -- is to drop the empty table and recreate it without the original column with the primary key constraint, instead putting it on the new column.
To drop the column with an unknown PK constraint name:
Generate a script to drop the table and re-create it from scratch
Remove the OriginalColumn column from the CREATE TABLE query
Put the PK constraint on the NewColumn column in the script
Run the script to drop and re-create it without the original column -- effectively dropping OriginalColumn and "moving" the PK constraint from OriginalColumn to NewColumn
???
Profit!

Create script for deleting tables with foreign keys in order

I need to create a script (SQL Server 2012) that deletes tables from one specific schema, I'm not using cascade delete. I'm for example getting all the tables from sys.tables for that specific schema. Is there any way of getting the tables in order that I delete first the ones with FK and after the main ones? like cascade delete but in a script. I know I can use "nocheck constraint all" but I prefer to do it directly.
Thank you.
Not possible sadly.
You can have a loop of FKs so that no sequence of deletes will work.
The simplest is a pair of tables where each references the other.
-- Pathological foreign keys
-- There is no order in which you can drop these tables
CREATE TABLE one(a INT PRIMARY KEY, b INT)
CREATE TABLE two(a INT PRIMARY KEY, b INT)
ALTER TABLE one ADD FOREIGN KEY (b) REFERENCES two(a)
ALTER TABLE two ADD FOREIGN KEY (b) REFERENCES one(a)

Delete from multiple tables SQL

I trying to figure out how I can delete from multiple tables in SQL Server.
I have one table containing only one primary key and three foreign keys for the three table I want to delete from. The other three table does not contain any foreign keys.
The stored procedure have one parameter, a specific primary key from one of the tables. I want to delete from the other tables WHERE tableID = #tableID. The constraints between the tables are set to cascade.
Is it possible with only that parameter to delete from all four tables?
I've tried with inner join, outer join, temptable..
Table Table Table Table
Pk Pk(Fk1) Pk(Fk2) Pk(Fk3)
Fk1 Column Column Column
Fk2 Column Column Column
Fk3
I have the table 2 Pk as parameter.
if your foreign keys were created with DELETE CASCADE, once you delete from the main table, all the related rows on the foreign tables will be delete too.
It seems like this is how it is configured, isnt it working?

Resources