I get this error when i am trying to create a relational database:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' REFERENCES classes(Name))' at line 1
I used a Update method to update the data base and create new tables, the first Update method works but then the second one breaks it. This is my code
one.Update("CREATE TABLE classes (Name VARCHAR (30) , Primitive1 VARCHAR(30), Primitive2 VARCHAR(30), Primitive3 VARCHAR(30), PRIMARY KEY(Name)) ");
one.Update("CREATE TABLE primitives (Name VARCHAR(30), Size_bytes INT, Description TEXT, FOREIGN KEY (Classes), REFERENCES classes(Name))" );
You may have other problems, but you have an extra comma in the foreign key statement:
FOREIGN KEY (Classes), REFERENCES classes(Name)
should be:
FOREIGN KEY (Classes) REFERENCES classes(Name)
I note that you have no actual column called Classes in the table. Either you need to declare that, or you might mean:
FOREIGN KEY (Name) REFERENCES classes(Name)
EDIT:
And I'm a strong advocate of auto incrementing keys:
CREATE TABLE classes (
ClassesId int auto_increment primary key,
Name VARCHAR(30) not null unique,
Primitive1 VARCHAR(30),
Primitive2 VARCHAR(30),
Primitive3 VARCHAR(30)
);
CREATE TABLE primitives(
PrimitivesId int auto_increment primary key,
Name VARCHAR(30),
Size_bytes INT,
Description TEXT,
ClassId int references classes(ClassId)
);
Note the use of inline references. I'm sure some will disagree, but I find it easier to follow column definitions when they are in one place. Also, this adds auto-incrementing primary keys and uses the same column name for the primary key and foreign key.
Finally, having three columns that look the same -- Primitive1, Primitive2, and Primitive3 -- is usually a poor design. It is better to have another table with one row per class and "Primitive".
one.Update("CREATE TABLE primitives (Name VARCHAR(30), Size_bytes INT, Description TEXT, FOREIGN KEY (Classes) REFERENCES classes(Name))" );
is correct. You had comma , before REFERENCES
Related
I use the MS Sync Framework to sync my SQL Server instance with a local SQL CE file to make it possible working offline with my Windows app.
I use GUIDs as keys. On my table I have a unique index on 2 columns: user_id and setting_id:
usersettings table
------------------
id PK -> I also tried it without this column. Same result
user_id FK
setting_id FK
value
Now I do the following:
I create a new record in this table in both databases - SQL Server and SQL CE with the same user_id and setting_id.
This should work and merge the data together since this can happen in real life. But I get an error when syncing saying the unique key constraint led to an error. The key pair already exists in the table.
A duplicate value cannot be inserted into a unique index. [ Table name = user_settings,Constraint name = unique_userid_settingid ]
Why can't MS sync handle that? It should not try to insert the key pair again. It should update the value if needed.
The issue is if you add the same key pair to different copies of the table, they get different IDs (GUIDs) as primary keys in this usersettings table.
As this is simply a many-to-many table between Users and Settings, there is no need to have that ID as a PK (or even a column at all).
Instead, just use a concatenated key of the two FKs e.g.,
CREATE TABLE [dbo].[usersettings](
[user_id] [UNIQUEIDENTIFIER] NOT NULL,
[setting_id] [UNIQUEIDENTIFIER] NOT NULL,
[value] [varchar](50) NOT NULL,
CONSTRAINT [PK_usersettings] PRIMARY KEY CLUSTERED ([user_id] ASC, [setting_id] ASC) );
Of course, include appropriate field settings (e.g., if you use VARCHARs to store the IDs) and relevant FKs.
As the rows inserted should now be identical on the two copies, it should merge fine.
If you must have a single column as a unique identifier for the table, you could make it meaningful e.g.,
the PK (ID) becomes a varchar (72)
it gets filled with CONCAT(user_ID, setting_id)
As the User_ID and Setting_ID are FKs, you should already have them generated so concatenating them should be easy enough.
Do you get the error during sync, then it should appear as a conflict, that you must solve in code.
https://learn.microsoft.com/en-us/previous-versions/sql/synchronization/sync-framework-2.0/bb734542(v=sql.105)
I also see this in the manual: By default, the following objects are not copied to the client database: FOREIGN KEY constraints, UNIQUE constraints, DEFAULT constraints, and the SQL Server ROWGUIDCOL property. This indicates poor support for your scenario
I suggest you remove the unique constraint from the device table.
I am having problems with my MSSQL database design.
I had 2 tables that looked like this:
CREATE TABLE tenants
(
tenantId INT PRIMARY KEY,
tenantName VARCHAR
)
CREATE TABLE users
(
userId INT PRIMARY KEY,
userName VARCHAR,
tenantId INT,
FOREIGN KEY (tenantId) REFERENCES tentants(tenantId)
)
I wanted to switch from only IDs to GUIDs for uniqueness and changed the tables.
I have read that GUIDs as primary keys are not recommended for performance reasons. So I kept the IDs:
CREATE TABLE tenants
(
tenantId INT PRIMARY KEY,
tenantGuid UNIQUEIDENTIFIER,
tenantName VARCHAR
)
CREATE TABLE users
(
userId INT PRIMARY KEY,
userGuid UNIQUEIDENTIFIER,
userName VARCHAR,
tenantGuid UNIQUEIDENTIFIER,
FOREIGN KEY (tenantGuid) REFERENCES tentants(tenantGuid)
)
Now Entity Framework is going wild just dismissing all relationships because the guids are not part of the primary key.
On old posts I have read that this would be not supported. Is it still not supported?
How am I support to solve this?
Thanks a lot!
Also sorry for the bad formatting, the editor doesn't respect my line breaks :/
soomon
I wanted to switch from only IDs to GUIDs for uniqueness and changed the tables.
You don't really need to switch to GUIDs to ensure uniqueness.Your IDs will be guaranteed to be unique (whichever type they are, say integer) as long as they are set as your primary keys.
I have read that GUIDs as primary keys are not recommended for performance reasons. So I kept the IDs
tenantGuid uniqueidentifier foreign key to tenants.tenantGuid
Assuming you're talking about performance of 'joins' when using GUID as the key then keeping ID's as primary key will not make a difference since queries will be made on the GUID anyway.
Now entity framework is going wild just dismissing all relationships because the guids are not part of the primary key.
How am I support to solve this?
I believe you're better off not solving this and rather choosing either to go back to your previous database design which uses the integer ID. Or ultimately use GUID as your primary key (removing integer ID in the process).
If you still want to us the unique identifier, you need to take note on the below two things
Set the tenants.tenantGuid to UNIQUE KEY and also set the users.tenantGuid as the same data type
Modify the tables as below
CREATE TABLE tenants (
tenantId INT PRIMARY KEY,
tenantGuid UNIQUEIDENTIFIER UNIQUE,
tenantName VARCHAR
)
CREATE TABLE users (
userId INT PRIMARY KEY,
userGuid UNIQUEIDENTIFIER,
userName VARCHAR,
tenantGuid UNIQUEIDENTIFIER,
FOREIGN KEY (tenantGuid) REFERENCES tenants(tenantGuid)
)
Hope this works for you
We are using resource files (.resx) to translate our .NET 4.5 MVC C# application to different languages. This works great for static text that is located in our views. However, we have values that are pulled from our SQL database that need to be translated as well.
An example: Dropdown list with values that are populated from a table in the database.
What is the best practice for translating these values in the database?
The last multilingual application I designed I've used a table for language, and for each table that had any string types (char, varchar etc`) I had a translation table.
Something along these lines:
CREATE TABLE TblLanguage
(
Language_Id int identity(1,1) PRIMARY KEY,
Language_EnglishName varchar(30),
Language_NativeName nvarchar(30),
CONSTRAINT UC_TblLanguage UNIQUE(Language_EnglishName)
)
CREATE TABLE TblSomeData (
SomeData_Id int identity(1,1) PRIMARY KEY,
SomeData_TextColumn varchar(50),
....
)
CREATE TABLE TblSomeData_T ( -- _T stands for translation
SomeData_T_SomeData_Id int FOREIGN KEY TblSomeData(SomeData_Id),
SomeData_T_Language_Id int FOREIGN KEY TblLanguage (Language_Id),
SomeData_T_TextColumn nvarchar(100),
PRIMARY KEY (SomeData_T_SomeData_Id , SomeData_T_Language_Id)
)
My application had English as it's default (or main) language, so I kept the default language in the base table and only the translations on the translation tables. You could, of course, keep the string values only in the translations table if you want to. Note that this does not take into consideration the different date and number formats for each culture, this is done on the presentation layer.
Designing a database with many tables and want to add a general Note table. I want a Note object to be able to attach to several other tables. So one Note can be associated with a particular Contact, maybe a Job, and also a few different Equipment objects. I'd like to be able to filter Note objects by the particular objects they are associated with.
Well, here's one way:
CREATE TABLE NoteTables
(
TableID INT NOT NULL Identity(1,1),
TableName SysName NOT NULL,
CONSTRAINT PK_NoteTables PRIMARY KEY CLUSTERED(TableID)
)
GO
CREATE TABLE TableNotes
(
TableID INT NOT NULL,
RowID INT NOT NULL,
NoteID INT NOT NULL,
CONSTRAINT PK_NoteAttachments PRIMARY KEY CLUSTERED(TableID, RowID, NoteID)
)
GO
CREATE TABLE Notes
(
NoteID INT NOT NULL Identity(1,1),
Note NVARCHAR(MAX),
CONSTRAINT PK_Notes PRIMARY KEY CLUSTERED(NoteID)
)
Note that I am assuming SQL Server and the use of IDENTITY columns here (if Oracle, you can use Sequences instead).
The Notes table contains all of the notes and gives them an ID to use as both a referemce and a primary key.
The NoteTables just list all of the tables that can have note attached to their rows.
TableNotes links the notes to the tables and rows that they are attached to. Note that this design assumes that all of these tables have INT ID columns that can be used for unique referencing.
It would take you two tables. Structure is as easy as the following.
Note table:
NotePK | tableFK | note
And a table that lists all your tables.
Either you create one (then you have full control but need to maintain it) or you take the
sys.tables t
You read it out by SELECT * FROM sys.tables t
the column object_id would be your tableFK in the first table
You can store as many comments as you like. If you want to get the note simply query the note table and filter for your tableFK.
I want to implement a patients data base for our software, and have an issue with the foreign key statement. I am using the latest SQLite DLLs, with C#.
When I try to run below code:
dbConnection = "Data Source=SQLiteMyDatabase.db;foreign keys=true;";
if (connections == 0)
{
cnn = new SQLiteConnection(dbConnection);
cnn.Open();
this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS patients ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name VARCHAR(100) NOT NULL;");
this.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS images ( FOREIGN KEY (patientID) REFERENCES patients(id), nameRed VARCHAR(20) NOT NULL PRIMARY KEY;");
}
I get the error:
SQLite error near "FOREIGN": syntax error
Why does this happen?
In order to create a foreign key, you must first create the column:
CREATE TABLE IF NOT EXISTS images (
nameRed VARCHAR(20) NOT NULL PRIMARY KEY,
patientID INT,
FOREIGN KEY (patientID) REFERENCES patients(id)
);
Please note:
I moved the primary key column (nameRed) first in the list of columns created, but that's just convention. It could go second.
You use the data type VARCHAR and SQlite will accept that, but be aware that it gets mapped to type TEXT, the maximum length will be ignored, and you will be able to store other data types in the column. SQLite is a bit funny when it comes to data types (it has a very well thought-out system but it's quite different from most other SQL databases).
To make things more confusing, you are allowed to declare the PRIMARY KEY as a property of the column (although you can also do it after the list of columns as with the FOREIGN KEY).