Primary key no defined access database - wpf

I am developping an application in wpf. I have to work with an existing database in Access.
I used the ORM EntityFramework.
My problem is : in the database, its exists a table with no primary key so I can't add any values in this table.
The Error I get is: no primary key defined. I can change the definition of the table.
How can I solve my problem ? thx

So as to createy a primary key before you begin you must know that
a table can contain only one PRIMARY KEY constraint.
All columns defined within a PRIMARY KEY constraint must be defined as NOT NULL. If nullability is not specified, all columns participating in a PRIMARY KEY constraint have their nullability set to NOT NULL.
Security
Permissions
Creating a new table with a primary key requires CREATE TABLE permission in the database and ALTER permission on the schema in which the table is being created.
Creating a primary key in an existing table requires ALTER permission on the table.

Related

How to resolve "Violation of PRIMARY KEY constraint" after a DB reset

Use Case:
I am performing performance execution on an database and I am trying to do following:
I took an backup of the database (the mdf and ldf file) at the early stage (lets called a "baselinecopy").
After that I execute some performance script .And the database reach to baselinecopy+Additional_Row (let it be "NewDatabase") from the test.
Then I replace the database baselinecopy with NewDatabase & start the server. While trying to perform operation on application it is giving me Violation of PRIMARY KEY constraint to test_order. Cannot insert duplicate key in object.
I check the "IDENTITY" but the table has no identity set.
Any thoughts on this ?
Violation of PRIMARY KEY constraint to test_order. Cannot insert duplicate key in object.
This is exactly what it says.
You have defined a primary key on a table and are attempting to insert a record that contains the same primary key as an existing record.
It does not need to be an identity column to be a primary key column.

How can I drop a constraint with multiple columns as the primary key in SQL Server?

My code looks like this:
CONSTRAINT user_password_username_password PRIMARY KEY (username, password)
I need to drop this constraint because I updated the foreign keys and no longer can use password in the constraint. I also cannot simply drop the table and recreate it because it is linked to other tables. How can I drop this constraint so only username will make up the primary key?
Drop all foreign key constraints referencing the user_password_username_password constraint. Disable will not work; you must drop them. You may need to drop foreign keys that reference other unique keys on the table, but I'm not sure about that.
Drop the user_password_username_password constraint on the table.
Create a new unique/primary key constraint on the table.
Create or recreate any foreign key constraints.
I believe you can do all this in a single transaction, but if it's a large table I'd consider doing this in single user mode. You'll be dropping a primary key which causes an index rebuild for every index and constraint on the table, then potentially creating several new indexes/constraints in the foreign keys, and then creating the new primary key which again forces all the indexes and constraints on the table to rebuild.

Altering multiple objects in in query

The code below is attempting to alter 2 columns at once in a table. Can it be done and am I just doing it wrong?
Alter table verdata
Add Primary Key(Asset_ID)
Add foreign key(Asdes) References AssetDesc(AssetDescription)
To add constraints in alter query,
follow the below,
ALTER TABLE ADD CONSTRAINT adds a table-level constraint to an existing table. Any supported table-level constraint type can be added via ALTER TABLE. The following limitations exist on adding a constraint to an existing table:
When adding a foreign key or check constraint to an existing table, Derby checks the table to make sure existing rows satisfy the constraint. If any row is invalid, Derby throws a statement exception and the constraint is not added.
All columns included in a primary key must contain non null data and be unique.
ALTER TABLE ADD UNIQUE or PRIMARY KEY provide a shorthand method of defining a primary key composed of a single column. If PRIMARY KEY is specified in the definition of column C, the effect is the same as if the PRIMARY KEY(C) clause were specified as a separate clause. The column cannot contain null values, so the NOT NULL attribute must also be specified.
For information on the syntax of constraints, see CONSTRAINT clause. Use the syntax for table-level constraint when adding a constraint with the ADD TABLE ADD CONSTRAINT syntax.
REFERENCE

Referencing the foreign key of another schema

For my project I am using oracle database where I have to work with 2 different database schema. Consider the following scenario please -
I have a schema A and in it I have a table table_a with a primary key apk
And I have another schema B and in it I have a table table_b with a primary key bpk
If both of these tables are in a same database then I can easily make a primary key - foreign key relationship.
But can I make a primary key - foreign key relation ship (or something like this) between these two columns - A.table_a.apk and B.table_b.pbk.
Thanks in advance.
To create a foreign key that references an object in a different schema, you just need to qualify the object name
ALTER TABLE B.table_b
ADD CONSTRAINT fk_b_a FOREIGN KEY (apk) REFERENCES a.table_a( apk )
This also requires that the user B has sufficient privileges on A.table_a. The user would need to have the REFERENCES privilege and would, presumably, need to have the SELECT privilege on the table as well.

Adding column with existing table which have primary key

I need your help...., Please help me
I want to add a new primary key to an existing table which already has 3 columns as composite primary key. But, I do not want to drop the old primary key, since there are many records and the old primary key also have relationship with other table
When I am using this query:
alter table hem154
add indexNO uniqueidentifier default newid()
alter table hem154
add CONSTRAINT pk_hem154_indexNo PRIMARY KEY (PK_indexNO)
Note:
Hem154 ~ Table Name
indexNo ~ Column Name which will to added
I get this runtime error:
Msg 1779, Level 16, State 0, Line 1
Table 'hem154' already has a primary key defined on it.
Msg 1750, Level 16, State 0, Line 1
Could not create constraint. See previous errors.
Please help me, how can I do it???
Thanks
Drop all primary keys and add Again all primary keys
ALTER TABLE hem154
DROP PRIMARY KEY,ADD PRIMARY KEY (col1,col2,indexNO);
You can only have one primary key per table. You can either add a second table that maps your new PK to the old PK, or drop the old PK and add the new one. Dropping the PK won't drop the columns, it just stops using them as the composite key. Any tables that were relying on the old key should probably be updated as well to support whatever answer you decide on.
First DROP existing PRIMARY KEY and ADD new composite PRIMARY KEY.
ALTER TABLE Table1
DROP CONSTRAINT PK_Table1_Col1
GO
and then try your code
There is no easy way of doing this. From your question, I understand that you want to add one additional column to the existing Primary Key. If they have existing relationships, then it is not possible to do this, without dropping those FK constraints first. Even if you do that, you will not be able to reestablish those FK constraints again, as you have modified the PK.
If you do not need to reestablish those FK relationships, then you can do this:
Drop all FK relationships
Drop existing PK (data will not be deleted, only the constraint will be dropped)
Add new column
Recreate PK to include the new column
However, if you cannot drop the existing FK constraints, then I am afraid this is not possible
On Microsoft SQL Server Management Studio:
Select/highlight both columns
Right click either
Press "Set primary key"

Resources