Problems with auto generated SQL Server Compact Keys - windows-phone-7

I use VS2010 to generate a SQL Server Compact database with two simple tables.
Both have a column TheID (int). On one I set unique to true on the other not.
And I set primary key on this column.
CREATE TABLE [TestTab] (
[TheID] int NOT NULL
, [TheVal] nvarchar(100) NOT NULL
);
GO
ALTER TABLE [TestTab] ADD CONSTRAINT [PK_TestTab] PRIMARY KEY ([TheID]);
GO
For the other (with unique set on the column) I get:
CREATE TABLE [TestTab2] (
[TheID] int NOT NULL
, [TheVal] nvarchar(100) NOT NULL
);
GO
ALTER TABLE [TestTab2] ADD CONSTRAINT [PK_TestTab2] PRIMARY KEY ([TheID]);
GO
CREATE UNIQUE INDEX [UQ__TestTab2__00000000000033E9] ON [TestTab2] ([TheID] ASC);
GO
Next I use the SQL Server Compact Toolbox to create a datacontext.
Selecting data works fine - but when I try to update the code it breaks on dc.SubmitChanges().
Simply my WP7 application ends.
A try / catch doesn't help.
TestTab tT = (from A in dC.TestTabs select A).FirstOrDefault();
if(tT != null) {
tT.TheText += "1";
dC.SubmitChanges();
}
If I drop the unique index, the code works fine.
Any ideas why this happens?
Manfred

It is a LINQ to SQL on Windows Phone bug... Your index is a duplicate (the primary key definition also adds an index) - see my blog post here: http://erikej.blogspot.com/2012/04/windows-phone-local-database-tip.html. To get auto generated values, setting a primary keys is not enough, ypou must define the column as IDENTITY as well.

Related

How do I correct "SQL Error: ORA-02270: no matching unique or primary key for this column-list" errors?

I'm working on a project in which I'm writing queries from this database that I am attempting to create in PLSQL. However, before I can do that I must create the db, and some of the table-create statemens I'm trying to run are returning errors. Specifically, the errors seem to indicate that some of the primary keys I'm referencing don't exist. When I check to see if that is true I see the likely issue. For example, I try to do
create table group_disforum ( df_id int,
ig_id int,
constraint gdf_FK foreign key(ig_id) references Course_Interest_group(interest_gid),
constraint gdf_PK primary key(df_id, ig_id),
comments varchar(150)
)
setting the foreign key to the primary key(interest_gid) in Course_Interest_group.
However, the Course_Interest_group is this:
create table Course_Interest_group( interest_gid int,
gname varchar(20),
courseid int,
facultyid int,
past_gpa float,
constraint IG_PK primary key(interest_gid, courseid, facultyid),
constraint IG_FK1 foreign key(courseid) references course(courseid),
constraint IG_FK2 foreign key(facultyid) references User_Faculty(userid)
);
The primary key for Course_Interest_group is not just interest_gid, but interest_gid, courseid, and facultyid.
There are several other instances of the same issue.
My question is, when referencing Course_Interest_group how can I reference just interest_gid? Is it possible? Should I change something?
I would like to keep the current Course_Interest_group pk of (interest_gid, courseid, facultyid) intact but I will modify it if needed.
my code
my output
If in the course_interest_group table interest_gid uniquely identifies each row in the table then interest_gid should be the primary key. Similarly, in the group_disforum if df_id uniquely identifies each row in the table then df_id should be the primary key. Modifying the primary keys of both these tables should solve the issue.
I think the way around for your question will be a TRIGGER to customize the data Integrity. You can create a before INSERT Trigger which will check first whether the data in your case (Course_Interest_group(interest_gid)). So basic structure of your Trigger will be like. Hope this may be a workwround.
SET SQLBL ON;
SET DEFINE OFF;
CREATE OR REPLACE TRIGGER group_disforum_tg BEFORE
INSERT OR
UPDATE
/*OF ig_id*/
ON group_disforum FOR EACH ROW
DECLARE lv_chck PLS_INTEGER;
BEGIN
SELECT COUNT(1)
INTO lv_chck
FROM Course_Interest_group
WHERE interest_gid = :new.interest_gid;
IF lv_chck = 0 THEN
RAISE_APPLICATION_ERROR(-20001,'Parent key (interest_gid) not found in group_disforum_tg',TRUE);
END IF;
END;
/

How to alter primary key column to autoincrement in derby using eclipse

I create a table and set one Integer column as a primary key.Now I want to set Auto Increment by 1 to that column.I am using this query but getting error.
ALTER TABLE APP.DocumentGroupCategory
ALTER ID INTEGER NOT NULL
GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1);
How can I alter column without drop table and create once again?

Trigger for incrementing a date and inserting into another table

I want to create a trigger, in Oracle. When the dateOrdReceived in my order table is updated or inserted the trigger takes this date whatever it may be and updates it by 14 days into another table productList ordDateDelivery so that it equals to
dateOrdReceived + 14 days = new ordDateDelivery
I did have a couple of attempts and guessed I'd need a query which would join my two tables. I also learned that maybe using DATEADD would allow me add 14 days but altogether I can't quite get it right.
My trigger attempt
`CREATE OR REPLACE TRIGGER "PRODUCTLIST_DATE_DELIVERY"
BEFORE
insert or update on "PRODUCTLIST"
for each row
begin
select p.dateOrdRecieved, o.ordDateDelivery
from productList p JOIN orders o
ON p.ordID = o.ordID;
new.OrdDateDelivery := DATEADD(day,14,new.p.dateOrdRecieved)
end;
/
ALTER TRIGGER "PRODUCTLIST_DELIVERY_DATE" ENABLE
and my tables for this trigger are as follows
PRODUCTLIST TABLE
CREATE TABLE "PRODUCTLIST"
( "ORDID" NUMBER(3,0) NOT NULL ENABLE,
"PRODUCTID" NUMBER(3,0) NOT NULL ENABLE,
"QUANTITY" NUMBER(4,2) NOT NULL ENABLE,
"ORDDATEDELIVERY" DATE,
"DISCOUNT" NUMBER(3,0),
"TOTALCOST" NUMBER(4,2),
CONSTRAINT "PK_PRODUCTLIST" PRIMARY KEY ("ORDID", "PRODUCTID") ENABLE
)
/
ALTER TABLE "PRODUCTLIST" ADD CONSTRAINT "FK_ORDERS" FOREIGN KEY ("ORDID")
REFERENCES "ORDERS" ("ORDID") ENABLE
/
ALTER TABLE "PRODUCTLIST" ADD CONSTRAINT "FK_PRODUCTS" FOREIGN KEY ("PRODUCTID")
REFERENCES "PRODUCT" ("PRODUCTID") ENABLE
/
ORDERS TABLE
CREATE TABLE "ORDERS"
( "ORDID" NUMBER(3,0) NOT NULL ENABLE,
"DATEORDRECIEVED" DATE,
"CUSID" NUMBER(3,0) NOT NULL ENABLE,
PRIMARY KEY ("ORDID") ENABLE
)
/
ALTER TABLE "ORDERS" ADD CONSTRAINT "FK_CUSTOMER" FOREIGN KEY ("CUSID")
REFERENCES "CUSTOMER" ("CUSID") ENABLE
/
DATEADD() is not an Oracle function... Oracle's datetime arithmetic is based around the day. If you add 1 to a date it increments the date by one day, adding 1.5 by 36 hours etc.
Now, your trigger.
You can't automatically update or insert a record into another table. The trigger is "on" one table, which means you need to create the DML in order to add or update it into that table.
update productlist
set dateOrdRecieved = :new.OrdDateDelivery + 14
where ordid = :new.ordid
The :new. here references the new data of the table on which the trigger is on. It's a specific "variable" that you can access rather than a general concept of what you're trying to achieve. You can't use it to assign data to other tables directly, though you can use it as a means of doing so.
Next you need to consider where your trigger is. You're looking to update PRODUCTLIST whenever ORDERS is changed, this means that the trigger needs to be on the table ORDERS.
create or replace trigger productlist_date_delivery
before insert or update on orders
for each row
begin
update productlist
set OrdDateDelivery = :new.dateOrdRecieved + 14
where ordid = :new.ordid;
end;
/
Notice a few extra differences to your own:
I use :new. instead of new.
I'm not selecting from the table; there's no need to do this as the data is already available. It's also impossible as you're selecting data that Oracle's trying to update, it forbids this to ensure integrity.
I haven't used cased identifiers. There's no need to do this; Oracle upper-cases everything by default. It's also really painful if everything's not upper case as you have to remember
Every statement ends in a semi-colon.
If you're having problems I recommend Tech on the Net, it has a good basic guide. As always though, there's the documentation on the CREATE TRIGGER statement.

Oracle Parent Table with 2 Possible Children Tables [duplicate]

I have requirement for a web app that states that a user should be able to either upload an instruction document(s) (.pdf, .doc, .txt) or provide text for the instructions. The user can upload a document and provide text, or they can do one-or-the-other, but they have to do something (not nullable). How would this be designed in a database? Would this be considered a complete sub-type (see below)?
This is tiny part of a larger schema, so I just posted what I felt was necessary for this particular question.
Ypercube's answer is fine, except this can, in fact, be done purely through declarative integrity while keeping separate tables. The trick is to combine deferred circular FOREIGN KEYs with a little bit of creative denormalization:
CREATE TABLE Instruction (
InstructionId INT PRIMARY KEY,
TextId INT UNIQUE,
DocumentId INT UNIQUE,
CHECK (
(TextId IS NOT NULL AND InstructionId = TextId)
OR (DocumentId IS NOT NULL AND InstructionId = DocumentId)
)
);
CREATE TABLE Text (
InstructionId INT PRIMARY KEY,
FOREIGN KEY (InstructionId) REFERENCES Instruction (TextId) ON DELETE CASCADE
);
CREATE TABLE Document (
InstructionId INT PRIMARY KEY,
FOREIGN KEY (InstructionId) REFERENCES Instruction (DocumentId) ON DELETE CASCADE
);
ALTER TABLE Instruction ADD FOREIGN KEY (TextId) REFERENCES Text DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE Instruction ADD FOREIGN KEY (DocumentId) REFERENCES Document DEFERRABLE INITIALLY DEFERRED;
Inserting Text is done like this:
INSERT INTO Instruction (InstructionId, TextId) VALUES (1, 1);
INSERT INTO Text (InstructionId) VALUES (1);
COMMIT;
Inserting Document like this:
INSERT INTO Instruction (InstructionId, DocumentId) VALUES (2, 2);
INSERT INTO Document (InstructionId) VALUES (2);
COMMIT;
And inserting both Text and Document like this:
INSERT INTO Instruction (InstructionId, TextId, DocumentId) VALUES (3, 3, 3);
INSERT INTO Text (InstructionId) VALUES (3);
INSERT INTO Document (InstructionId) VALUES (3);
COMMIT;
However, trying to insert Instruction alone fails on commit:
INSERT INTO Instruction (InstructionId, TextId) VALUES (4, 4);
COMMIT; -- Error (FOREIGN KEY violation).
Attempting to insert the "mismatched type" also fails on commit:
INSERT INTO Document (InstructionId) VALUES (1);
COMMIT; -- Error (FOREIGN KEY violation).
And of course, trying to insert bad values into Instruction fails (this time before commit):
INSERT INTO Instruction (InstructionId, TextId) VALUES (5, 6); -- Error (CHECK violation).
INSERT INTO Instruction (InstructionId) VALUES (7); -- Error (CHECK violation).
I think that this cannot be done with Declarative Referential Integrity alone - not if your design has these 3 separate tables.
You'll have to ensure that all Insert/Delete/Update operations are done within transactions (stored procedures) that enforce such a requirement - so no row is ever inserted or left in table Instruction without a relative row in either one of the 2 other tables.
If you don't mind having nullable fields, you could merge the 3 tables into one and use a CHECK constraint:
CREATE TABLE Instruction
( InstructionID INT NOT NULL
, Text VARCHAR(255) NULL
, Filepath VARCHAR(255) NULL
, PRIMARY KEY (InstructionID)
, CONSTRAINT Instruction_has_either_text_or_document
CHECK (Text IS NOT NULL OR FilePath IS NOT NULL)
) ;
If a user submitted text, could your application save it as a .txt file? This way you would only have to worry about dealing with files.
Something feels a bit off here
There is no UserID in this schema, so it should be added to the
Instruction table.
If a user does not upload anything, there will (should) be no entry
for that user in the Instruction table.
So the problem -- as stated -- is not about placing constraints
on these three tables.
When loading this structure, use a stored procedure and/or a transaction -- to make sure that at least one of the child record gets populated. Though, this has nothing to do with the business requirement that user has to upload something.

insert row without set primary column

I have that query :
INSERT INTO GOST (ASSORTMENTID, ROZMIAR, GOST)
VALUES ( 54,'S','MjgwMzktODkgMTc0LTk2')
I want insert new row in table GOST, but I don't want to specify column with primary key - GOSTID. I want that database set next id value.
When I run this code I have that error:
validation error for column GOSTID, value "* null *"
I understand that I should set GOSTID column in INSERT query, yes ?
It is possible to run this without this parameter ?
I think a sample script worths more than 1000 words:
Go to a shell interface in the firebird server machine, cd to a folder where you have read/write permissions, start isql or isql-fb (depends on your system and firebird version) and run this script:
create database 'netmajor.fdb' user 'sysdba' password 'masterkey';
set autoddl off;
create table netmajor_example (
netmajor_id integer not null
, str_data varchar(200)
, int_data integer
, constraint pk_netmajor_example
primary key (netmajor_id)
);
create generator netmajor_gen;
set term ^;
create trigger netmajor_pkassign
for netmajor_example
active before insert position 1
AS
begin
if (new.netmajor_id is null) then
new.netmajor_id = gen_id(netmajor_gen, 1);
end
^
commit work^
set term ; ^
insert into netmajor_example (str_data, int_data) values ('one', 1);
insert into netmajor_example (str_data, int_data) values ('twenty', 20);
commit work;
select * from netmajor_example;
Take a look at the results, which in my machine are:
; NETMAJOR_ID STR_DATA INT_DATA
;============ ============================ ============
; 1 one 1
; 2 twenty 20
IF you have questions, don't hesitate to contact. Best regards.
Obviously, your primary key is a NOT NULL column, which means, it's always required. You cannot insert a row without giving a value for the primary key (unless it were an "auto-number" column which gets automatically set by the database system).
Use "before insert" trigger to set value for primary key. Firebird doesn't have "auto-increment" field type, so you need take care of it by yourself.
See http://www.firebirdfaq.org/faq29/ for tutorial how to do this. Some DB applications (eg Database Workbench) can create the trigger and generator automatically.

Resources