Is there a simple solution for duplicating table rows in SQL Server as well as all table rows with foreign keys pointing to the cloned table row? I've got a "master" table and a bunch of "child" tables which have a foreign key into the ID of the master table. I need to not only create a perfect copy of the master table, but clone each and every child table referencing the master table. Is there a simpler way to do this than creating a new row in the master table, copying in the information from the row to be cloned, then going through each child table and doing the same with each row pointing to the cloned row in the master table?
I'm using a SQL Server 2005 Database accessed through C# ASP.net MVC 1.0.
If by "simple" you mean is there is a procedure that can be called to do it, no there is not. However, you can use the INFORMATION_SCHEMA views such as INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS to query for the columns in a table or the list of related tables and dynamically build your INSERT statements to copy one row to another. Of course, this does not account for other uniqueness constraints that might be on the tables (e.g. a table with a Name column which requires that the values be unique).
Related
I created a project in ASP.NET MVC with a separate database project which I run every time there is a table change. My only problem is that if I add one column for example, it will drop the entire database and recreate it and delete all data in the table.
Does anyone know of a pre-deployment script or a method I can use to add / remove / rename tables or column and at the same time preserve the integrity of my data? i.e keep my data while modifying my database
You can rename columns using SQL Server functions, but doing this risks breaking scripts used by other functions or stored procedures in your database. I don't endorse this practice, so I'm not posting about it below. Adding or removing columns is fair game.
You can add columns to a table by using the following query:
ALTER TABLE [YourTable]
ADD [ColumnName] [Datatype];
And you can drop columns using this query:
ALTER TABLE [YourTable]
DROP COLUMN [ColumnName];
These SQL commands will preserve the other columns in your table. If you want to change a column name I encourage you to set up a View in your SQL Server client and give the column an alias.
This can be accomplished using:
CREATE VIEW [ViewName]
AS
SELECT [ColumnName] AS [ColumnAlias]
FROM [TableName]
GO
You'd be able to perform SELECTS on the view in just the same way you can query SELECT on a normal table, except you can query the [ColumnAlias] instead of the [ColumnName]. You cannot perform INSERT or DELETE queries on a view, however
I have 2 tables employees and job in a MySQL server. I have 2 CSV files with the same schema as the two tables. Something as below.
Job table(MySQL)
Job(CSV)
Employees(MySQL)
Employees(CSV)
I need to insert the rows from CSV into the MySQL tables. Note that the Id fields in both the tables are on AUTOINCREMENT. I am aware that MySQL does not support merge query and instead has INSERT.. ON DUPLICATE UPDATE. Now the issue with using this is that the Primary keys i.e., the Ids are definitely duplicate but I need them to be inserted as new rows with new Ids. For example, the Admin role should have the Id as 3 and so on. Also, as you can see, the job table is being referenced by the employees table. So the the JobId column in the table should be updated with the respective values. I am not sure how this can be achieved.
Any help appreciated!
I am currently working on MVC 5 CRUD, and I just started last week. What I am encountering is an error when I use the DELETE on one of my tables because I am deleting a row of data on table A but the primary key of table A is a foreign key of table B.
Is there any way that if I delete the data on table A its corresponding data on table B will be also deleted? Thank you.
The MVC part here is irrelevant, your database design is as such that the DELETE would fail regardless of technique used.
As pointed out above by Chino you should be looking at your database, and specifically the relationships between table A and table B, and set these to cascading delete. Meaning that when a row in table A is deleted, the row in table B is deleted too (hence 'cascading')
First You Delete foreign key of table B Data And Then A Table.
I'm having issues adding a Foreign Key to link 2 existing tables together. Table A has data, and I need it to reference Table B (which also has data).
I will need to insert a row (or rows) into Table B which Table A will reference.
In this case it is acceptable to insert a row into Table B and then use that as the default value for the migration. That would require that I know the ID of the row that I'm inserting.
I think that I can handle everything except figuring out the ID of the row that I insert into Table B.
Is it possible to return data inside of Migrations?
I am using a SQLite database. Since drop/rename/reorder columns are not supported in SQLite (using alter table commands), I am writing customized methods for the following tasks: (taking backup of existing table, then creating a new table with matching requirements and so on..) which is described in several other threads.
The following are the DB operations:
DROP columns
RENAME (columns and datatype)
ADD columns
REORDER columns.
I am wondering in what order should these operations be done? My confusion is mainly around whether drop should come before rename columns or the other way?
Also I need some pointers on how to rename columns including datatype and moving data around?
Any thoughts?
First, create a backup of your database in case something goes wrong during the following instructions.
The order of operations should be CREATE newtable, INSERT INTO newtable, DROP oldtable.
Create a new table with the correct column names and datatypes for each column. Then just do something like this:
INSERT INTO new_table(columns, ...)
(SELECT columns, ...
FROM old_table)
You may need to perform casts on different datatypes if the new datatype isn't directly compatible with the old datatype.
You will need to make sure that the columns you select from your old table are in the same order as defined by the columns in your new table in your INSERT INTO statement.
After data is inserted into your new table, you can then verify that all data has been inserted correctly. Be sure to update any foreign key references in your other tables to avoid any issues with foreign key constraints.
Then you can drop the old table and rename the new table to the name of your old table:
DROP old_table;
ALTER TABLE new_table_name RENAME TO old_table_name;