I need to move one table from one database (used for development) to another (used by user applications/the purpose of the database).
So far I do it with the select or into statement.
That works fine with just one drawback.
When doing this I lose my primary (PK) and foreign keys (FK).
Now I'm handling that by alter the tables after copying them, see the example below, but, it would be beautiful and more effective (and secure copying process) if I could do a copy of the tables with PK and FK intact all the way.
select *
into DB2.dbo.Ta
from DB1.dbo.Ta
alter table DB2.dbo.Ta add primary key (id)
alter table DB2.dbo.Ta add foreign key (Tb_id) references DB2.dbo.Tb
(Where the attribute Tb_id is the PK in table Tb, already existing in the database DB2.)
I work the Microsoft SQL server standard version 2017 and Microsoft SQL server express database engines with Microsoft SQL Studio Management version 2018.
In SQL Server 2014, I want to export data from one database to another database. Those databases are in different servers. Even though I enable identity insert, it is not creating primary or foreign keys.
Can you help me about this? Why doesn't it create keys?
i have two sites that sell the same products (same database structure and everything). One of them has recently had all its products updated and we need to take the data from three tables on the updated site and override those same three tables on the out of date site. is there a way to do this is Sql Server MS? ive tried truncating the tables in sql to then do a "insert into db1table select * from db2table" but i just get errors from the foreign keys. Someone showed me a trick a while in visual studio enterprise for comparing data where vs spits out a .sql file you can run to update your table but i only have vs professional 2010 and this doesnt seem to be part of the package.
Assuming you have the same foreign key constraints in both the databases and if foreign key constraints are the only issue -
why don't you just
1) Disable all the foreign keys
2) Load the data
3) Enable all the foreign keys
I come from Oracle background, it should not be very hard to disable/enable all foreign key constraints using a script at one go in SQL Server too.
I've been using SQL Server for years, so I'm not a noob. I've recently been having problems where I can't add a foreign key to certain tables in my database using SQL Management Studio and SQL Server 2008 Express. The table I'm trying to reference is there and it has a primary key (with the primary key constraint created), but it doesn't show up in the list of tables that I can choose from when I'm trying to add the FK.
If I try and add the FK through plain old T-SQL, the query succeeds, but if I view the new FK using the UI, the primary key table dropdown is empty. The FK is there and it actually does work if I try to insert some data that would violate the constraint.
Anyone know why this would be happening?
This sounds like a tool issue (SSMS), not an engine issue
Thoughts:
close/reopen SSMS (caching?)
patched to same version as server install?
different schema etc?
Edit, after comment and it's SSMS caching:
You can also right-click on the table node and refresh so SSMS updates the cache. This problem goes back to SQL Enterprise Manager and SQL 2000. No known fix after 10 years...
Visual Studio 2005 doesn't provide an interface for creating relationships between tables in a SQL Server CE database (I'm using version 3.0) and you can't open a Compact Edition DB using Management Studio as far as I know. Any ideas?
Unfortunately there is currently no designer support (unlike for SQL Server 2005) for building relationships between tables in SQL Server CE. To build relationships you need to use SQL commands such as:
ALTER TABLE Orders
ADD CONSTRAINT FK_Customer_Order
FOREIGN KEY (CustomerId) REFERENCES Customers(CustomerId)
If you are doing CE development, i would recomend this FAQ:
EDIT: In Visual Studio 2008 this is now possible to do in the GUI by right-clicking on your table.
Visual Studio 2008 does have a designer that allows you to add FK's. Just right-click the table... Table Properties, then go to the "Add Relations" section.
You need to create a query (in Visual Studio, right-click on the DB connection -> New Query) and execute the following SQL:
ALTER TABLE tblAlpha
ADD CONSTRAINT MyConstraint FOREIGN KEY (FK_id) REFERENCES
tblGamma(GammaID)
ON UPDATE CASCADE
To verify that your foreign key was created, execute the following SQL:
SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
Credit to E Jensen (http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=532377&SiteID=1)
Alan is correct when he says there's designer support. Rhywun is incorrect when he implies you cannot choose the foreign key table. What he means is that in the UI the foreign key table drop down is greyed out - all that means is he has not right clicked on the correct table to add the foreign key to.
In summary, right click on the foriegn key table and then via the 'Table Properties' > 'Add Relations' option you select the related primary key table.
I've done it numerous times and it works.
create table employee
(
empid int,
empname varchar(40),
designation varchar(30),
hiredate datetime,
Bsalary int,
depno constraint emp_m foreign key references department(depno)
)
We should have an primary key to create foreign key or relationship between two or more table .
I know it's a "very long time" since this question was first asked. Just in case, if it helps someone,
Adding relationships is well supported by MS via SQL Server Compact Tool Box (https://sqlcetoolbox.codeplex.com/). Just install it, then you would get the option to connect to the Compact Database using the Server Explorer Window. Right click on the primary table , select "Table Properties". You should have the following window, which contains "Add Relations" tab allowing you to add relations.
Walkthrough: Creating a SQL Server Compact 3.5 Database
To create a relationship between the tables created in the previous procedure
In Server Explorer/Database Explorer, expand Tables.
Right-click the Orders table and then click Table Properties.
Click Add Relations.
Type FK_Orders_Customers in the Relation Name box.
Select CustomerID in the Foreign Key Table Column list.
Click Add Columns.
Click Add Relation.
Click OK to complete the process and create the relationship in the
database.
Click OK again to close the Table Properties dialog box.