How to drop a table with foreign key constraint in SQL Server - azure-sql-database

I would like to drop a table with foreign key constraint and re-create the same table (with all the relationships) and with additional data (which I have from “Generate Scripts…”)
How can I do it? I'm using an Azure SQL Server.
If I disable the constraint, truncate the table, insert new data, and re-enable it? Will this work?
Any leads, please? Thanks.

One of the ways is to create a .sqlproj. Inside this, you can create the tables, stored procedures, indexes, data population, and much more. The deployment from .sqlproj will take care of any changes you do to the data store. Please refer to the links below.
URL:
https://learn.microsoft.com/en-us/azure/devops/pipelines/targets/azure-sqldb?view=azure-devops&tabs=yaml
https://learn.microsoft.com/en-us/sql/ssdt/download-sql-server-data-tools-ssdt?view=sql-server-ver15

Related

SQL Express Alter table add primary key auto increment

I'm trying to alter my table named Vettura to modify field ID and making it both Primary and Auto increment, i have already looked in other answers on other similiar conversation, but i can't find the right answer, can you pls help me?
You might not be able to alter the table this way depending on what data is there and if there are dependencies.
If you are using SSMS, I would recommend scripting the database to an SQL file including both the schema and the data and setting DROP and CREATE. Then you can edit the table definition(s) and regenerate the database.
By the way, for SQL-Server, the Auto increment is called IDENTITY.

How to transfer data using SSIS

I am new to SSIS packages and just require assistance on how to transfer data from one data source onto my own database.
Below is my data flow:
Now I have a ODBC Source (Http_Requests Source) where I take data from a PostgreSQL database table (see screenshot below for table columns and data):
Below is the OLE DB destination where it has the table I want to transfer the data to (this table is currently blank):
Now I tried to start debugging to extract the data but I get a few errors (displayed below):
I am a complete novice so I would like some guidance on what I need to include in order to get this SSIS package to transfer data across. Would I need to include a merge statement and how do I apply it. I heard you can write a merge as a proc and call on the proc as a sql command. Does that mean I will need to write a proc in SSMS and then call on it within the OLE DB Destination?
If somebody can provide an example and screenshot then that would be very helpful as I am really new to SSIS.
Thank you,
Check constraint on destination table or disable them before running it.
Below are query you can use.
-- Disable all table constraints
ALTER TABLE YourTableName NOCHECK CONSTRAINT ALL
-- Enable all table constraints
ALTER TABLE YourTableName CHECK CONSTRAINT ALL
Tick keep identity
box or drop primary key on the table. After you apply the changes do not forget to refresh metadata by opening the mappings in sis.
the error means that PerformanceId is an IDENTITY column on your destination table. IDENTITY columns are read only unless you tell it otherwise. So if we were in tSQL to be able to insert IDENTITY we would turn on IDENTITY_INSERT. Because you are in SSIS you can accomplish the same thing by checking the "keep identity" box.
HOWEVER when ever you get an error like this it is usually a sign that you should NOT be mapping ID to Performance ID. The question you have to ask is the Identity from your source supposed to be the identity of the destination table? Usually not, most of the time it would be another column as a surrogate key. Then you have to understand if it is even possible. because if there is a unique constraint or primary key then the identity cannot repeat which means you have to know that your source's id column will not cause a duplicate primary key violation.
More than likely the actual fix if for you to uncheck ID from the source and ignore the value.
The column PerformanceID (in the target) is almost certainly an identity column and that is why it is not working. You may not want to transfer it (and have SQL Server generate values for PerformanceID or you can check 'Keep Identity.'

TRUNCATE TABLE query unable to execute in SSIS because of foreign key

I have a table Person which contains 2 fields.In my another database i have a Participant Table(also have 2 columns).From Participant Table i have to insert values into Person Table.
but before every insertion,i want truncate the person Table.
I have try it out with linking Execute Sql task to Data flow task.But it is showing error that a Primary Foreign key relation exists there.
If I understand correctly, SSIS has nothing to do with your real problem, which is that you want to truncate a table that is referenced by a foreign key constraint. That question has already been answered: Cannot truncate table because it is being referenced by a FOREIGN KEY constraint?
If a table in sql server has foreign key references then you can't truncate. instead in your execute sql task use delete without a where clause.
delete from person_table
If you are really adamant about truncating the table, you could drop the foreign key constraints, then truncate the table then recreate the foreign key constraints. Providing of course, that the user you are running the package as has the privileges to do so.
Create an "Execute SQl" task and run DELETE FROM person
after this task, run your import.
DELETE FROM will give the same result as TRUNCATE TABLE, but if the table has a foreign key pointing to it, it cant be truncated. You have to use the delete command
You won't be able to delete either unless cascading deletes is turned on (or you delete the child records first). Why is this a problem you ask, why can't I do what I want to do? Because if you do then you may lose the integrity of the data. Suppose I have records in table2 which relate to records in table 1. Suppose further that table1 has an autogenerated id. If I could truncate that table, then I leave those records i ntable 2 hanging out without any record to reference them, they have become orphaned. Well but I'm putting the data back in you say. But then they will have new id numbers and you will still lose the relatinoship tothe related data.
Can you drop the FK and truncate and insert and recreate the FK. Yes you can but it is a poor practice and you should not unless you are also recreating those related records.
The best practice is to use a MERGE statement to update or insert depending on what you need.
In SSIS Transfer SQL Server Objects Task Set Property DeleteFirst to TRUE

easy button for adding column

I right click on my table in ssms 2008 and select Script Table as / Drop and Create Table to new window and I try to run the script but get an error:
Could not drop table because it is referenced by a foreign key constraint
What was the point of the Drop and Create generate script then?
Thanks,
rod.
The point of the Drop and Create generate script is exactly what you'd think - it gives you an easy way to script out dropping and re-creating a table. However you can't drop a table if other tables reference it via foreign key constraints, which is why you're getting the error message.
If you're just trying to add a column, you can right-click the table in Enterprise Manager and click Modify and just add the column in design view. There's no need to drop the table just to add a column. (And it's especially an awful approach if the table has data in it.)
The easiest way to add a column to an existing table? Write the ALTER TABLE statement yourself instead of relying on SQL Server Management Studio to do it for you:
ALTER TABLE YourTableName
ADD ColumnName int

SQL Server: Can INFORMATION_SCHEMA Tell Me When a SQL Object Was Created?

Given:
I added a non-nullable foreign key to a table.
I settled on a way to populate the foreign key with default values.
I checked in both changes to a re-runnable DB creation script.
Other developers ran this script, and now they have the foreign key populated with default values on their developer machines.
A few days later however...
I changed my mind.
Now I'd like to populate the foreign key's default values differently.
My Question:
Can SQL Server or INFORMATION_SCHEMA
tell me when SQL objects were created?
Ideally, I'd like to drop and re-add the foreign key if it was created before a certain date/time.
Any help or alternative strategies would be greatly appreciated.
Obviously, I'd like to avoid going to each developer's cube, asking them to drop the foreign key manually.
For SQL 2000 sysobjects (for SQL 2000), column crdate
For SQL 2005+ sys.objects, create_date and modify_date