SQlite3 multi-column UNIQUE INDEX not having expected effect [duplicate] - sqlite

This question already has answers here:
How to set unique constraint over multiple columns when any one can be null?
(3 answers)
Closed 4 months ago.
I have a unique constraint on one of my tables:
CREATE UNIQUE INDEX `role_contextid_targetid_ownerid_type_endedat_unique` on `role` (`contextId`, `targetId`, `ownerId`, `type`, `endedAt`)
You can see it is part of the db definition here:
But for some reason it is still allowing multiple entries which share all of their contextId, targetId, ownerId, type, endedAt values. Notice the last three items below:
Am I misunderstanding what a multi-column unique index is supposed to do? In not, why did SQLite allow me to add items that break these rules?

Here's what docs are saying
For the purposes of unique indices, all NULL values are considered different from all other NULL values and are thus unique.
So, it considers NULLS as different values which makes all the problem lines unique from the DB's point of view
dbfiddle

Related

Creating keys without values [duplicate]

This question already has answers here:
Create an empty child record in Firebase
(2 answers)
Closed 4 years ago.
If I want to create keys that have no values, just to have list of certain data for each user in my case.
So it should look like something like that:
database
|
user_id6
|____data1
|____data2
In my example, each user should have a list of data, where the data is they key. Is it possible? Or I should just create a key with some arbitrary value for example:
database
|
user_id6
|____data1: true
|____data2: true
Nodes in Realtime Database can't have "no value", otherwise they would cease to exist. Assigning some boolean value, as you're showing, is one way to represent your data.

Can a prepared statement use multiple values [duplicate]

This question already has answers here:
PreparedStatement IN clause alternatives?
(33 answers)
Closed 5 years ago.
Consider an SQL statement like
Select * from items where id in (123,456,789)
Can I use a prepared statement like
Select * from items where id in ?
and then supply the parameter as a set or list? Maybe I'd need parentheses around the "?".
I'm planning to use this in R, but I guess it's a general query for JDBC.
Two partial work-arounds:
Create a function that changes in (?) to in (?,?,?), depending on the length of the supplied list of values, then break that array into individual values for binding.
Pros: one query; binding is straight-forward
Cons: not feasible with large lists; have to wrap your queries in query-manglers, not fool-proof
Upload values to a temp table and change your query to
select * from items where id in (select val from temptable)
Pros: deal with arbitrary number of values; no need to trick SQL; binding is just as one would do for a multi-row insert
Cons: multiple calls; requires temp table and clean-up; might be problematic integrating with more complex queries (??)

Can a Primary key column accept the Null value? I'm using sqlite studio for learning and it accepts [duplicate]

This question already has answers here:
NULL permitted in Primary Key - why and in which DBMS?
(6 answers)
Closed 6 years ago.
Can a Primary key column accept the Null value? I'm using SQLiteStudio for learning and it accepts.
It is accepting multiple null values in the table of 5 columns, where the following query is executed:
Insert into EmployeeDB (Name, Age)
Values ('Raja3', '85');
Here 2 columns are given a value, so all other columns are updated as Null in which one column is defined as PRIMARY Key and one as UNIQUE.
Well, Unless the Column is an Integer Primary Key, SQLite (Only SQLite) allows Primary Key to Possess a Null Value. NULL values are considered distinct from all other values, including other NULLs. This can be resolved by adding NOT NULL constraint to the Column Field.
Primary key can't accept NULL values. Unique key can accept only ONE NULL values.
Can i know which column is defined as primary key and which one is defined as Unique key. If possible show me the Structure of the table
Primary key column does not accept null values. It is not possible. Can you show properties of all columns in this table please?

SQLite query: get index of row after sort where

I need to find the index in a table of a specific row and also select it.
I need both, the index and the row.
Any ideas?
EDIT: by 'index' I refer to position in the table after being ordered.
This question could be a duplicate, but I don't know what to look for.
SQL database are fundamentally unordered. There is no index or row number in a table. Unless you explicitly order the results with ORDER BY in a SELECT clause there is no guarantee that the rows will be returned to you in the same order in two successive queries.
To uniquely identify a row, you must ensure that you know which column (or combination of column values) are uniquely identify a row and include these terms in the WHERE clause.

InnerJoin Query giving "Failed to enable constraints" error [duplicate]

This question already has answers here:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
(26 answers)
Closed 8 years ago.
I have this query running against 2008R2. tblJoin is the table in between two tables(tblIncident and tblPatron) for many-to-many relationship.
While I am building the query in VS2012, in the Query Builder window when I execute the query it runs fine and gives me the desired results. However in the last step when I click on TestQuery and enter the parameter it gives an error:
"Failed to enable constraints.."
I checked the datatype and they all match against each other against the tables,
SELECT tblIncident.Inci_ID,
_tblJoin.PatronID,
_tblJoin.LName,
_tblJoin.FName,
_tblJoin.MI
FROM tblJoin INNER JOIN
tblIncident ON tblJoin.InciID = tblIncident.Inci_ID AND tblJoin.InciID = tblIncident.Inci_ID
WHERE (tblIncident.Inci_ID = #Inci_ID)
You need Check all the MaxLength property of the columns in your DataTable.
The column that I changed from "nvarchar(512)" to "nvarchar(MAX)" still had the 512 value on the MaxLength property so I changed to "-1" and it works!!.
You can solve this issue from this discussion
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
There have many answers ....
Thanks Ramesh and Vignesh. Vignesh's comment made me think about the duplicate foreign key. So I kept the query but removed the foreign key (Incident ID), that made it worked. This is how my query looks like:
SELECT tblJoin.PatronID, tblJoin.LName, tblJoin.FName, tblJoin.MI FROM tblJoin
INNER JOIN tblIncident ON tblJoin.InciID = tblIncident.Inci_ID
WHERE (tblIncident.Inci_ID = #Inci_ID)

Resources