I have a Microsoft Access ADP-type database, linking to a SQL Server 2012 database, which was recently upgraded from Office XP (2002) to Office 2010. The upgrade was mostly successful except for an issue with a Combo Box on a form with an updateable data source - In Access XP/2002, the user could select values from the dropdown list and the value would be updated in the table. However, when the user tries to modify the record using the 'tblL.LMID' Combo Box in Access 2010, an error briefly flashes in the status bar at the bottom of the screen (and the record is not updated):
"Control can't be edited; it's bound to AutoNumber field 'LMID'"
I understand this is normal functionality if the field in question was an Identity column in SQL Server, but that's not the case here. However, the 'tblL.LMID' field does get used as a join in the SQL query behind the scenes.
The data source on the form is as follows:
SELECT dbo.tblLM.OpID, dbo.tblL.*, dbo.tblLM.DR
FROM dbo.tblL INNER JOIN dbo.tblLM ON dbo.tblL.LMID = dbo.tblLM.LMID
WHERE (dbo.tblLM.DR = 1)
ORDER BY dbo.tblL.DS
The tables involved in the query are as follows:
CREATE TABLE [dbo].[tblL](
[LID] [int] IDENTITY(1,1) NOT NULL,
[LMID] [int] NOT NULL,
[DS] [nvarchar](10) NOT NULL)
CREATE TABLE [dbo].[tblLM](
[LMID] [int] IDENTITY(1,1) NOT NULL,
[OpID] [int] NULL,
[DR] [bit] NULL DEFAULT ((1)))
As per the table structure, tblL.LMID is a simple column (not Autonumber/Identity), and we should be able to modify it like we did in the Access XP/2002 version of the application.
I would happily accept any assistance on this issue, much appreciated! :)
The problem was with the query itself. To resolve, we had to replace the "dbo.tblL.*" select with specific column names, as well as giving the problematic column an alias:
SELECT dbo.tblLM.OpID, dbo.tblL.LMID as l_MID, dbo.tblLM.DR
FROM dbo.tblL INNER JOIN dbo.tblLM ON dbo.tblL.LMID = dbo.tblLM.LMID
WHERE (dbo.tblLM.DR = 1)
ORDER BY dbo.tblL.DS
We then updated the combo box to use the new alias ("l_MID"), and then it started working correctly.
Put the form into design view; delete the current combobox. Create a new combobox and follow the wizard.
Related
Hi everyone I am having some problems with my SQLite database in my java program. I am trying to retrieve data from a couple of tables but it says my table doesn't exist. I have checked using DB Browser and it's definitely there, so I'm not sure what I'm doing wrong. This is the error I receive:
[SQLITE_ERROR] SQL error or missing database (no such table: staff_clocked_in.clock_in_time)
SELECT * FROM staff, staff_clocked_in.clock_in_time WHERE staff.staff_id = staff_clocked_in.staff_id;
I'm sure my tables exist and there is data in both tables, here is a screenshot of my db browser.
If it helps, this is how I have setup my tables:
STAFF:
CREATE TABLE IF NOT EXISTS staff (staff_id INTEGER PRIMARY KEY NOT NULL, first_name TEXT NOT NULL, last_name TEXT NOT NULL, job_title TEXT NOT NULL);
STAFF_CLOCKED_IN:
CREATE TABLE IF NOT EXISTS staff_clocked_in (staff_id INTEGER PRIMARY KEY NOT NULL REFERENCES staff(staff_id), clock_in_time DATETIME NOT NULL);
Can anyone see anything wrong with my query? I'm not good with databases so hopefully it's just something simple.
The error message is correct staff_clocked_in.clock_in_time is not a table.
You should use staff_clocked_in instead which is your table.
So the fixed query should look like
SELECT *
FROM staff, staff_clocked_in
WHERE staff.staff_id = staff_clocked_in.staff_id;
I'm using SQLite, which doesn't support adding a constraint to an existing table.
So I can't do something like this (just as an example):
ALTER TABLE [Customer]
ADD CONSTRAINT specify_either_phone_or_email
CHECK (([Phone] IS NOT NULL) OR ([Email] IS NOT NULL));
Are there any workarounds for this scenario?
I know:
I can add a constraint for a new table, but it isn't new (and it's generated by my ORM, EF Core)
I can do a "table rebuild" (rename table, create new one, copy old data, drop temp table) but that seems really complex
Ideas
Can I somehow make a copy of the table into a new table, with some schema changes?
Or "get" the schema somehow, and edit it in a SQL script, then add a table with that schema?
To make a copy of a table with some schema changes, you have to do the creation and the copying manually:
BEGIN;
CREATE TABLE Customer_new (
[...],
CHECK ([...])
);
INSERT INTO Customer_new SELECT * FROM Customer;
DROP TABLE Customer;
ALTER TABLE Customer_new RENAME TO Customer;
COMMIT;
To read the schema, execute .schema Customer in the sqlite3 command-line shell.
This gives you the CREATE TABLE statement, which you can edit and execute.
To change the table in place, you can use a backdoor.
First, read the actual table definition (this is the same as what you would get from .schema):
SELECT sql FROM sqlite_master WHERE type = 'table' AND name = 'Customer';
Add your CHECK constraint to that string, then enable write access to sqlite_master with PRAGMA writable_schema=1; and write your new table definition into it:
UPDATE sqlite_master SET sql='...' WHERE type='table' AND name='Customer';
Then reopen the database.
WARNING: This works only for changes that do not change the on-disk format of the table. If you do make any change that changes the record format (such as adding/removing fields, or modifying the rowid, or adding a constraint that needs an internal index), your database will blow up horribly.
I have a system which stores data from an online SQL Server database in local storage. Data records are uploaded and downloaded using a web service. I am using an ADO.Net Entity Data Model in my code.
On some upload requests for one table the routine fails when I try to call it giving an error message "The value 'null' cannot be parsed as the type 'Guid'." This only happens occasionally and I have not worked out how to repeat the problem. I have logged it 80 times in the last month and in that time the routine has been called successfully 1200 times.
I have five fields in the database record for this table that are defined as uniqueidentifiers. Two of these are 'NOT NULL' and the other three are 'NULL'. Here is the 'CREATE TABLE' query showing the guid fields in this table:
CREATE TABLE [dbo].[Circuit](
[CircuitID] [uniqueidentifier] NOT NULL,
[BoardID] [uniqueidentifier] NOT NULL,
[RCDID] [uniqueidentifier] NULL,
[CircuitMasterID] [uniqueidentifier] NULL,
[DeviceID] [uniqueidentifier] NULL,
CONSTRAINT [PK_CircuitGuid] PRIMARY KEY NONCLUSTERED
(
[CircuitID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
GO
ALTER TABLE [dbo].[Circuit] WITH CHECK ADD CONSTRAINT [FK_Circuit_RCD] FOREIGN KEY([RCDID])
REFERENCES [dbo].[RCD] ([RCDID])
GO
ALTER TABLE [dbo].[Circuit] CHECK CONSTRAINT [FK_Circuit_RCD]
GO
ALTER TABLE [dbo].[Circuit] WITH CHECK ADD CONSTRAINT [FK_CircuitGuid_Board] FOREIGN KEY([BoardID])
REFERENCES [dbo].[Board] ([BoardID])
GO
ALTER TABLE [dbo].[Circuit] CHECK CONSTRAINT [FK_CircuitGuid_Board]
GO
The data uploaded for the guid fields in this table looks like this:
{"__type":"Circuit:#WaspWA","BoardID":"edb5f774-5e5d-490c-860b-73c3419628cf","CircuitID":"e95bbfa3-2af6-49a5-94dd-c98924ec9a62","CircuitMasterID":null,"DeviceID":"daf12fce-675c-46d9-94c4-ed28c63cdf30","RCDID":null}
This record was created on one machine uploaded to the online SQL Server database and then downloaded to another machine.
I have other similar tables in the database which never give any problems. It is just this table which I am getting error messages from. The two fields which are defined as 'NOT NULL' (BoardID and CircuitID) always have data in them and are never null.
Is there something obvious that I have missed here?
The problem was that the value 'null', a string, was being written into my local copy of CircuitMasterID rather than null. So when I tried to write this to SQL it didn't like it. The SQL error message shows null in quotes but I was not sure whether this was because it was a string or because the error message put the value in quotes to delineate it.
The value 'null' had found its way into the CircuitMasterID field because I had written the value null out into some HTML and when this was saved back to the field it became 'null'. I am storing data in local storage and this does not give very good type control. Note to self 'must add better type control'.
I am having a strange problem with Entity Framework and SQL Server that I cannot figure out.
I am building an online store in ASP.NET MVC 5.
I am inserting statistics about a search into a table called SearchResults - it has this structure:
[SearchID] [int] IDENTITY(1,1) NOT NULL,
[SearchTerm] [varchar](5000) NULL,
[SearchDate] [datetime] NULL,
[Results] [int] NULL
I am just doing a simple EF insert with this C# code in the Search action of a controller, which gets posted to with a search term:
var s = new SearchResult() { SearchTerm = search, SearchDate = DateTime.Now, Results = results };
db.SearchResults.Add(s);
db.SaveChanges();
Results is an int with the count of the products found by the search.
Whenever I do a search, the same search gets inserted exactly 3 times, with slightly different times for each insert. The weird part is that occasionally, it will only insert one entry (as expected). I can't figure out why it is doing this.
I've run an SQL trace, and when it inserts 3, there is only one call to the DB. This is what is in the trace:
exec sp_executesql N'INSERT [dbo].[SearchResults]([SearchTerm],[SearchDate], [Results])
VALUES (#0, #1, #2)
SELECT [SearchID]
FROM [dbo].[SearchResults]
WHERE ##ROWCOUNT > 0 AND [SearchID] = scope_identity()',N'#0 varchar(5000),#1 datetime2(7),#2 int',#0='dew',#1='2015-02-16 16:32:53.4649185',#2=2
The weird part is the datetime shown in the insert is the value for the third insert.
I am at a complete loss for why this is happening. I've tried everything I can think of, but I am still getting repeats on insert.
Maybe you're looking at the wrong piece of code.
Had you logged or debugged the calls to the controller, how many times the first snippet of code you posted get executed?
Sorry for novice question.
I have created my tables using SQLite database browser, but:
I do not know how can I specify my foreign keys using the application?
How can I create a relationship diagram between tables?
I know this question has been asked long ago but I found it. Its built right into GUI. You just need to drag and make those Name, Type tabs little bit small to make space for the Foreign Key tab. Place your mouse pointer at the end and drag the header.
My version of SQLite Browser is Version 3.7.0.
I couldn't find a way of defining foreign key constraints using the "Database Structure" tab. I'd strongly recommend defining table definitions and constraints using a script rather than building them using the graphical editor - it makes it much easier to create new databases and to track changes to the schema.
By way of an example, assume we have two tables: one defining file names and one specifying the method used for compression, we can add a foreign key constraint to the file_definition table when defining it.
CREATE TABLE [compression_state] (
[compression_state_id] INTEGER PRIMARY KEY NOT NULL,
[value] TEXT NOT NULL
);
CREATE TABLE [file_definition] (
[file_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[compression_state_id] INTEGER NOT NULL,
[name] TEXT NOT NULL,
FOREIGN KEY(compression_state_id) REFERENCES compression_state(compression_state_id)
);
However, by default, SQLite will not enforce the constraint, therefore every time you connect to the database, you must issue the following command to enable constraint checking.
PRAGMA foreign_keys = ON;
Further details in the documentation.
If the tables already exist and you don't want to build a complete script then you're out of luck, SQLite doesn't support adding foreign keys once the table has been generated, see here: SQL Features That SQLite Does Not Implement
Go to edit table definition window
Click on Add field
Name it with Type : Integer
Scroll right and find Foreign Key column
Double click under Foreign Key column in new row
Select master table and its id field
Click OK
Click Write Changes
From the SQLite Documentation :
CREATE TABLE artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER -- Must map to an artist.artistid!
);
and in the end :
CREATE TABLE track(
trackid INTEGER,
trackname TEXT,
trackartist INTEGER,
FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);
In the DB Browser for SQLite Environment (v 3.8.0 - Sqlite v 3.9.2) when you add the DB fields for the track table along with
the PK ,AI and other columns you can find a Foreign Key Column.
In there , and for this example, you just add artist(artistid) in the trackartist row.
Then the foreign key constraint is created.
In DB Browser, in the Edit Table Definition window, you can double click the blank area of Foreign Key and a text box will activate. You can add your Foreign Key there.
it's really very easy , just do this
Go to edit table definition window
Right click on the table that you want to relate ( foreign table )
choose modify table
on Constraints tab select add constraints button and choose foreign key
you can relate tables here and then back to fields tab and do
Name it with Type : Integer
Scroll right and find Foreign Key column
Double click under Foreign Key column in new row
Select master table and its id field
Click OK
Click Write Changes
Triggers in SQLite3 enforces foreign key constraints. Link https://www.sqlite.org/cvstrac/wiki?p=ForeignKeyTriggers would help you out to solve your first question.
I am not sure whether this is entirely right but here's what I did:
I added the variable "UserID" in the fields tab an checked the box "primary key"
The I went to the constraints tab and added a foreign key Type constraint on the "UserID"
Then I went back to fields tab and double clicked on the foreign key field that opened and added the name of the table where that key is and the name of the variable