Is there a way to "bypass" the SQL collate logic? I want two different bytearrays to give me two different keys no matter what and without SQL trying to collate them.
I prefer to use the nvarchar data type if possible.
The most generic COLLATE setting I have found is 'Latin1_General_100_BIN2' but even that gives me a conflict in this example:
CREATE TABLE [dbo].[test] (
[Feature] [nvarchar](450) COLLATE Latin1_General_100_BIN2 NOT NULL,
CONSTRAINT [PK_dbo.test] PRIMARY KEY CLUSTERED ([Feature] ASC)
)
insert into test values ('a')
insert into test values ('a ')
I get the error
Violation of PRIMARY KEY constraint 'PK_dbo.test'. Cannot insert duplicate key in object 'dbo.test'. The duplicate key value is (a ).
I am using the SQL server in MS Azure.
I do not recommend that you use the Feature field as the primary key.
If possible, you can try to use below script.
declare #tb table(id int identity,name varchar(20))
insert into #tb(name) values('a'),(' a'),('a ')
select * from #tb --where name=' a' and datalength(name)=datalength(' a')
Running result:
In the database, for spaces, currently only len and datalength can be used to filter data. When inserting data in the primary key, space characters should be filtered out. So it should not be supported.
For more details or questions, you can raise a support on the portal, and the official will give you documentation or a satisfactory answer.
Related
I have a table with 5 columns and I'd like to insert a new row. The problem is, I do not yet have the values for all 5 columns.
Is it possible to add information to the first and third columns and add the rest later?
This is not referring to a specific database management system. I'd just like to know if it's possible.
Technically, it's not.
However, you can insert dummy values (like NULLs or defaults) into the problem columns and then update them later when you know the correct values.
It depends on your table definition, if column accept null value you don't have to provide a value for it in insert statement.
It depends on table definition.
Some tables require value by design, other not.
It is also worth to mention, that database may be configured to throw error when required fields are not set. In other cases there is only warning and fields are initialized by NULL value despite being defined as "NOT NULL".
Some of fields cannot be omitted at all. Those are mainly primary key fields.
Yes u could do that as follow (MS SQL Server):
CREATE TABLE #TM
([ID] INT IDENTITY(1, 1),
[Col1] INT,
[Col2] VARCHAR(MAX),
[Col3] VARCHAR(MAX),
[Col4] VARCHAR(MAX),
[Col5] VARCHAR(MAX)
);
INSERT INTO #TM
([Col1],
[Col3]
)
SELECT '1',
'2';
Later, you could update the Data in rest of columns with help of ID Column (i.e. unique identity column) which is auto increment.
However, it would insert NULL values in other columns which are not defined in column list.
Why can I not use a variable to name a new table?
As a beginning SQL project, I'm making a personal finance database. Each account will have a corresponding table in the database. There is also a table listing all the current accounts. See (simplified) code sample below:
CREATE TABLE accountList
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NCHAR(30) NOT NULL UNIQUE,
[Active] BIT NOT NULL
)
INSERT INTO accountList(name, active)
VALUES
('Bank_One_Checking', 1);
CREATE TABLE Bank_One_Checking
(
[Id] BIGINT NOT NULL PRIMARY KEY IDENTITY,
[payee] NCHAR(30) NOT NULL UNIQUE,
[category] NCHAR(30) NOT NULL UNIQUE,
[amount] INT NOT NULL DEFAULT 0.00
)
This code works. I want to set the account name to a variable (so it can be passed as a parameter to a stored procedure). See code below:
DECLARE #accountName nchar(30);
SET #accountName = 'Bank_One_Savings';
INSERT INTO accountList(name, active)
VALUES
(#accountName, 1);
CREATE TABLE #accountName
(
[Id] BIGINT NOT NULL PRIMARY KEY IDENTITY,
[payee] NCHAR(30) NOT NULL UNIQUE,
[category] NCHAR(30) NOT NULL UNIQUE,
[amount] INT NOT NULL DEFAULT 0.00
)
Line 6 in that code (CREATE TABLE #accountName) produces an error
Incorrect syntax near #accountName, expecting '.', 'ID', or 'QUOTEID'.
Why won't it insert the variable into the command?
SQL doesn't allow tables to be variables. You could use dynamic SQL, if you like, but I strongly recommend against it.
Your code has several flaws. You should learn not only to fix them but why they are wrong.
You need a "master" table, where AccountName is a column. Multiple tables with the same structure is almost always a sign of poor database design.
Strings should be designed using VARCHAR() or NVARCHAR(), unless they are short or known to be the same length (say an account number that is always 15 characters). Fixed-length strings just waste space.
I find it unlikely that a column named category would be unique in such a table. It seems to violate what uniqueness means.
Integers are not appropriate for monetary amounts in most of the world (use decimal or money). And, they shouldn't be initialized to constants with a decimal point.
I have created a table and set its PK to IDENTITY(1,1).
But I want the PK to begin/start with alphabets e.g CRMSON0, CRMSON1, CRMSON2... and so on.
But I wasn't able to find the solution for Microsoft SQL Server.
on Microsoft website, details I could find were about IDENTITY(seed,increment) or IDENTITY(data type,seed,increment).
Can anyone suggest?
You can create computed column composed from CRMSON + ID value:
CREATE TABLE #table1 (ID INT IDENTITY(0,1)
,col INT
,col_pk AS CONCAT('CRMSON', ID) PRIMARY KEY);
INSERT INTO #table1(col) VALUES(3), (4);
SELECT *
FROM #table1;
LiveDemo
The best solution is to use
an ID INT IDENTITY(1,1) column to get SQL Server to handle the automatic increment of your numeric value
a computed, persisted column to convert that numeric value to the value you need
So try this:
CREATE TABLE dbo.YourTable
(ID INT IDENTITY(1,1) NOT NULL PRIMARY KEY CLUSTERED,
CrimsonID AS 'CRIMSON' + CAST(ID AS VARCHAR(10) PERSISTED,
.... your other columns here....
)
Now, every time you insert a row into YourTable without specifying values for ID or CrimsonID:
INSERT INTO dbo.YourTable(Col1, Col2, ..., ColN)
VALUES (Val1, Val2, ....., ValN)
then SQL Server will automatically and safely increase your ID value, and CrimsonID will contain values like Crimson1, Crimson2,...... and so on - automatically, safely, reliably, no duplicates.
That'll work fine - but as others have already pointed out - I would NOT recommend using this column as the clustered, primary key - use the ID column for that, it's ideally suited for such a task! The CrimsonID is bad for mainly two reasons: it's a variable length string, and it's much too wide compared to an int
I have my table definition as follows:
create table [Language](
Id int primary key identity,
Code varchar(11) not null unique,
NativeName nvarchar(50) not null unique
)
And then, I have a long list of statements that insert into that table. The problem is that some of the insert statements conflict on my NativeName column's unique constraint. The weird thing is that the content is not unique at all. For example, if I only insert the following with the table empty:
insert into Language (Code, NativeName) values('am', N'አማርኛ');
insert into Language (Code, NativeName) values('dv', N'ދިވެހިބަސް');
I get for the second insert.
Violation of UNIQUE KEY constraint 'UQ__Language__EB1957A5F98D1F9C'. Cannot insert duplicate key in object 'dbo.Language'. The duplicate key value is (ދިވެހިބަސް).
Does anyone know why unicode characters are causing these issues?
Try declaring the NativeName column with a more specific (binary) collation.
eg:
NativeName nvarchar(50) collate SQL_Latin1_General_CP437_BIN not null unique
In MySql I could have a table with an auto increment column and insert that way:
INSERT INTO tbl VALUES(null, "bla", "bla");
Is there any way to do this with Microsoft SQL Server Express?
Instead of this:
INSERT INTO tbl(`v1`, `v2`) VALUES ("bla", "bla");
Thanks
In Sql Server, you do not need to specify a value for identity columns (I'm guessing this is what the null in mysql is doing).
So, the short syntax for inserting into your table would be:
Insert Into tbl Values('bla', 'bla')
This works regardless of whether the column is declared to be the primary key of the table and also works for any position of the column in the table. That is, even if the column is in the middle as shown below:
Create Table tbl (
[Col1] [varchar](50) NULL,
[Id] [int] IDENTITY(1,1) NOT NULL,
[Col2] [varchar](50) NULL
)
Sql Server will still quite happily interpret the insert statement and take care of the identity insert.
As other posters have mentioned, in Sql Server you actually need to issue the Set Identity_Insert tbl On command to be able to specify a value for identity columns.
I think you are asking whether you can include a reference to the identity column in your insert statement and pass a null or some other magic value in the Values or Select clause. No, you cannot. You cannot pass a value for the identity column unless you use SET IDENTITY_INSERT [table] ON and pass an actual value.
INSERT INTO tbl VALUES(null, 'bla', 'bla');
Works; use single quotes.
From http://msdn.microsoft.com/en-us/library/ms174335.aspx
"If the values in the Value list are
not in the same order as the columns
in the table or do not have a value
for each column in the table,
column_list must be used to explicitly
specify the column that stores each
incoming value."