I am trying to read in table schemas from an existing database.
I am reading in all of the tables and columns on each table using the .tables and .columns command. The .columns command returns a variable PRIMARY_KEY which lets me know it is a primary key for the table.
My question is how do I know whether a column is a foreign key to another table (and which table it is a foreign key of)?
To get information about the foreign key constraints of a table, use PRAGMA foreign_key_list.
Related
I am working on EF database first application and I have encounter the situation where delete records from a table which has no primary key.
I have no control over the database and it is not possible to add any key for the DB table.
What is the best approach I can take?
The best approach is ,you need add a primary key.
Why EF need you add a PK?
Because, PK is a only way to identity the row in the table , if not exists a PK , the table may have many same rows(if have PK,it's different,PK is unique,each row will be different),so if your want to delete or update ,which row is your target? if not exists PK ,EF couldn't know how to identity the row ,so you must have PK in the table.
If you can't add one (may be the DB is from customer, you don't have permission), you can change the mapping XML file between EF and DB,To add a relate PK Element for a unique table column.
I need to rename the primary key of an existing table through FluentMigrator so an automapper can automatically detect the column.
For most columns, it's a simple 1) delete any foreign key constraints on that column 2) delete indices for that column and 3) rename the column. I have historically done this by:
Delete.ForeignKey("foreignkeyconstraint").OnTable("mytable");
Delete.Index("UserId").OnTable("mytable");
Rename.Column("UserId").OnTable("mytable").To("UserInfo_id");
However, this doesn't appear to work for primary keys, since I can't delete the automatically created index on that column. What is the correct way to rename a primary key column with FluentMigrator?
Use the following method call to rename your primary key (SQL Server)
Execute.Sql("EXEC sp_rename N'[Current_Primary_Key_Name]', '[New_Primary_Key_Name]', 'object';");
Something like this should work as long as it is not an identity (auto increment) column as well:
Delete.PrimaryKey("PRIMARY KEY").FromTable("mytable");
I try to update two tables using a relation and table adapters generated in dataset designer. But unable to perform child table update, it is not inserted, the problem is that during child table update the identity column value is unknown.
Table "Users" has primary key and identity column UserId. Table "UserInRoles" has column UserId and foreign key to Users.UserId. Here is my code:
usersTableAdapter.Fill(ds.Users);
userInRolesTableAdapter.Fill(this. ds.UserInRoles);
DataRow userRow = ds.Users.NewRow();
userRow["UserName"] = userName; // and fill other userRow fields.
DataRow userRoleRow = ds.UserInRoles.NewRow();
userRoleRow["RoleId"] = selectedRole; // leave unfilled column "UserId", because I thing the relational update should do it.
userRoleRow.SetParentRow(userRow, ds.Relations["FK_UserInRoles_Users"]);
ds.Users.Rows.Add(userRow);
ds.UserInRoles.Rows.Add(userRoleRow);
tableAdapterManager.UpdateAll(ds);
ds.AcceptChanges();
//usersTableAdapter.Update(ds.Users);
//userInRolesTableAdapter.Update(ds.UserInRoles);
I set both relation type, update rule: cascade, delete rule: cascade, accept rule: none. Refresh the datatable option is selected. On database the foreign key is set to cascade update too, and enforce for replication and enforce foreign key to yes.
What I am doing wrong?
I tried Users.GetChanges() after users table update, but don't get any changes. The only way it works now is to fill again users table after update. Tried update table adapters separately, but then get error violating foreign key.
you have missed to create a datarelation object
here how to do it:
http://msdn.microsoft.com/en-us/magazine/cc188919.aspx
I have question, how can i insert a new data into a database that the primary key and foreign key is always equal in value?
ex. i entered my name into Name table and that Name table has PK and FK. every time i insert a new data, the FK was empty. i expect that the value of FK is same as the value of PK even they have different field name.
above is my database relationship. every time i insert new data the EventsID pk(Eventstbl) wont copy to EvnetsID FK(Organizationtbl)
The referential integrity does not work as you described. It better suits functionality of the triggers. The purpose of the PK and foreign key constraint is to prevent insertion of data which is not exist in other table as PK. Therefore, if you want to copy data from Eventstbl to Organizationtbl upon inserting a new record to the former, you need to write a trigger for the insertion event of the Eventstbl. Your PK - FK constraint will work like following, when you insert new record to Organizationtbl, it will check Eventstbl table for the corresponding EventsID. If it does not exist, it will not allow you to insert new record to Organizationtbl. I hope it helps.
Well, you can use a trigger in EventsTbl, an after insert / update trigger. So this trigger could insert / update the other table you need. You can use the INSERTED table to catch the new value of the PK. I hope it helps.
I have two tables. Table1 has a foreign key that represents a valid primary key in another table (Table2). Now the problem is that this foreign key can sometimes can be null (it is suppose to be like that). How can I check the constraint only when the foreign key is not null?
PD> I'm currently programming in C# and I'm using SQL Server Management Studio.
In SQL Server, FK constraints work exactly as you described - they only verify NOT NULL values against the parent table. Just use a foreign key.
You could add an additional "CHECK" constraint to the table that would enable you to pass the value into a function to check the value. Just a thought...
Take a look here for an example link