I'm trying to make a simple table in a database.
CREATE TABLE [dbo].[klanten]
(
[Klant_naam] TEXT NOT NULL PRIMARY KEY,
[Klant_adres] TEXT NULL,
[klant_gsm] TEXT NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] TEXT NOT NULL
)
When I try to update it, the following error pops-up.
As the documentation warns:
ntext , text, and image data types will be removed in a future version
of Microsoft SQL Server. Avoid using these data types in new
development work, and plan to modify applications that currently use
them. Use nvarchar(max), varchar(max), and varbinary(max) instead.
So, try this instead:
CREATE TABLE [dbo].[klanten] (
[Klant_naam] varchar(max) NOT NULL PRIMARY KEY,
[Klant_adres] varchar(max) NULL,
[klant_gsm] varchar(max) NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] varchar(max) NOT NULL
)
Well, this doesn't quite work either, because there is a limit of 900 bytes for index keys. How about using a surrogate key and reasonable column lengths?
CREATE TABLE [dbo].[klanten] (
Klant_Id int not null identity(1, 1) primary key,
[Klant_naam] varchar(255) NOT NULL unique,
[Klant_adres] varchar(max) NULL,
[klant_gsm] varchar(max) NULL,
[klant_gewicht] INT NOT NULL,
[klant_lengte] INT NOT NULL,
[klant_klacht] varchar(max) NOT NULL
);
Try to change "TEXT" data type to "NCHAR()" or any other similar to this.
This error usually occured when you try to update database, but some fields can't be updated. For example, I have 5 fields in a table:
[Id] INT IDENTITY (1, 1) NOT NULL,
[Path] NVARCHAR (MAX) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[Url] NVARCHAR (MAX) NULL,
[SecureUrl] NVARCHAR (MAX) NULL
I've worked for some time with it, and make some records. And some of them have value NULL at [Url]. Suddenly I decide to change table:
[Id] INT IDENTITY (1, 1) NOT NULL,
[Path] NVARCHAR (MAX) NOT NULL,
[Name] NVARCHAR (50) NULL,
[Url] NVARCHAR (MAX) NOT NULL, //!!! HERE A PROBLEM, I ALREADY HAVE NULL RECORDS IN DATA
[SecureUrl] NVARCHAR (MAX) NULL
Problem is that my data has been made with the old model and it has records with the NULL at [Url], but in new model NULL value can't be at [Url]. So as with new model, old data can't be correct. Thus we have the error when update.
Related
I am simply trying to run a basic SQL script to recreate a database.
The database was initially created in SQLite, and I exported it using DB Browser for SQLite.
The start of the file looks like this:
BEGIN TRANSACTION;
CREATE TABLE "AspNetUsers"
(
`Id` varchar(128) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint(1) NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint(1) NOT NULL,
`TwoFactorEnabled` tinyint(1) NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint(1) NOT NULL,
`AccessFailedCount` int(11) NOT NULL,
`UserName` varchar(256) NOT NULL,
`IsActivated` tinyint(1) NOT NULL DEFAULT (0),
`Organisation` TEXT NOT NULL,
PRIMARY KEY(`Id`)
);
I created a new db and when running the query in SSMS I get this annoying error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I tried deleting all the whitespace between the first ( and 'Id' but then I just get
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '`'.
I also tried replacing the `s with 's but with the same result....
I'm pretty sure the server I'm trying to execute this on is running SQL Server Express - not sure if that makes a difference
Why must life be so difficult?
The code is rather specific to SQLite in several respects:
The use of backticks is non-standard.
Having a length for integer columns in non-standard.
text and longtext are non-standard.
The equivalent create table statement in SQL Server would be:
CREATE TABLE AspNetUsers (
Id varchar(128) NOT NULL,
Email varchar(256) DEFAULT NULL,
EmailConfirmed tinyint NOT NULL,
PasswordHash varchar(max),
SecurityStamp varchar(max),
PhoneNumber varchar(max),
PhoneNumberConfirmed tinyint NOT NULL,
TwoFactorEnabled tinyint NOT NULL,
LockoutEndDateUtc datetime DEFAULT NULL,
LockoutEnabled tinyint NOT NULL,
AccessFailedCount int NOT NULL,
UserName varchar(256) NOT NULL,
IsActivated tinyint NOT NULL DEFAULT (0),
Organisation varchar(max) NOT NULL,
PRIMARY KEY (Id)
);
Except for the varchar(max), this would be pretty standard for any database.
Some notes:
You probably don't need varchar(max) for any of these fields. Although you can use it, it looks awkward to have a phone number that could occupy megabytes of data.
You could probably replace the tinyints with bits.
DEFAULT NULL is redundant.
I have a strange problem.
I want to insert an item to a table from database. I use Entity Framework.
Although the Id is set, I keep getting the following error:
Cannot insert the value NULL into column 'Id', table 'project_atp.dbo.ShoppingCarts'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}
The table definition:
CREATE TABLE [dbo].[ShoppingCarts] (
[Id] INT NOT NULL,
[Guid] UNIQUEIDENTIFIER NULL,
[Name] NVARCHAR (255) NULL,
[Code] NVARCHAR (255) NULL,
[SupplierNo] NVARCHAR (255) NULL,
[SupplierName] NVARCHAR (255) NULL,
[Price] NVARCHAR (50) NULL,
[Quantity] INT NULL,
CONSTRAINT [PK_ShoppingCarts] PRIMARY KEY CLUSTERED ([Id] ASC)
);
Can you please advise what could be wrong here! Thanks!
By default Entity Framework assumes that an integer primary key is database generated. As the result Entity Framework would not include Primary Key field in the actual INSERT statement.
I would try to either play along and ALTER the table to auto-generate the ID (which judging by your comment you did)
or set StoreGeneratedPattern property of OnlineCarStore.Models.ShoppingCarts Id column to 'None'
or use annotation: [DatabaseGenerated(DatabaseGeneratedOption.None)].
I have a stored procedure which accepts a user-defined table type called dbo.NodeTableType:
ALTER PROCEDURE [dbo].[InsertNonExistingNode]
(#TableVariable dbo.NodeTableType READONLY)
It seems redundant, because the dbo.NodeTableType is identical to an actual table in my database dbo.Nodes.
Do I really have to create this data-type in order to except it as a parameter in my stored procedure?
If the answer to #1 is "Yes", then is there a way to create this data-type by pointing it at the table? Currently, I have to create it this way:
CREATE TYPE NodeTableType AS TABLE
(NodeTypeId SMALLINT NOT NULL,
Location NVARCHAR(50) NULL,
DisplayName NVARCHAR(100) NULL,
AccessLevel SMALLINT NOT NULL,
IsEnabled BIT NOT NULL,
CreatedOn DATETIME2(7) NULL,
CreatedBy NVARCHAR(150) NULL,
ModifiedOn DATETIME2(7) NULL,
ModifiedBy NVARCHAR(150) NULL,
NativeId BIGINT NOT NULL,
SourceId INT NOT NULL,
Name NVARCHAR(100) NOT NULL,
Alias NVARCHAR(100) NULL
)
The only way to define a user-defined table type is through CREATE TYPE statement according to documentation at this URL: User Defined Types. So you cannot use a table for this.
I would recommend to stick to the standard practice of passing a table valued parameter that has been created with CREATE TYPE.
Another approach as outlined below can be used provided you can live without passing a table valued parameter to your procedure, and I am not sure if that is possible in your case.
In your stored procedure, you could populate a table variable of the same type as the original NodeTableType type. Of course, you would need to decide the logic for populating this table variable; I have assumed that the logic is something as simple as NodeId < 10 as an example only; in your case this rule would be different and probably more complex.
DECLARE #myTable dbo.NodeTableType;
INSERT INTO #myTable(
NodeTypeId SMALLINT NOT NULL,
Location NVARCHAR(50) NULL,
DisplayName NVARCHAR(100) NULL,
AccessLevel SMALLINT NOT NULL,
IsEnabled BIT NOT NULL,
CreatedOn DATETIME2(7) NULL,
CreatedBy NVARCHAR(150) NULL,
ModifiedOn DATETIME2(7) NULL,
ModifiedBy NVARCHAR(150) NULL,
NativeId BIGINT NOT NULL,
SourceId INT NOT NULL,
Name NVARCHAR(100) NOT NULL,
Alias NVARCHAR(100) NULL)
SELECT * from dbo.Nodes where NodeId < 10;
--now you can use #myTable rather than #TableVariable
--in your stored procedure
I'm working with ASP.Net web application and whenever I tried to add a FOREIGN KEY this error appears in the Data tools operations :
SQL71508 :: The model already has an element that has the same name
dbo.FK_Sellers_Users. SQL71508 :: The model already has an element
that has the same name dbo.FK_Sellers_Users.
I don't understand what's the problem with FK! I have 2 tables with this error
table Sellers :
CREATE TABLE [dbo].[Sellers] (
[Seller_ID] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (50) NOT NULL,
[SUsername] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[SEmail] NVARCHAR (50) NOT NULL,
[Phone] NVARCHAR (50) NOT NULL,
[City] NVARCHAR (50) NOT NULL,
[LastLoginDate] DATETIME NULL,
[CreatedDate] DATETIME NULL,
PRIMARY KEY CLUSTERED ([Seller_ID] ASC),
CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SEmail]) REFERENCES [Users]([Email]),
CONSTRAINT [FK_Sellers_Users] FOREIGN KEY ([SUsername]) REFERENCES [Users]([Username])
);
and table Users :
CREATE TABLE [dbo].[Users] (
[Id] INT NOT NULL IDENTITY,
[Username] NVARCHAR (50) NOT NULL,
[Password] NVARCHAR (50) NOT NULL,
[Email] NVARCHAR (50) NOT NULL,
[UserType] INT NULL,
PRIMARY KEY CLUSTERED ([Id]),
CONSTRAINT [AK_Users_Username] UNIQUE ([Username]),
CONSTRAINT [AK_Users_Email] UNIQUE ([Email]),
);
Right there in your CREATE TABLE statement for dbo.Sellers, you have two FK constraints named FK_Sellers_Users.
Make those names unique, perhaps by adding the column name on the end.
You have two foreign keys with the same name FK_Sellers_Users. You should better use FK_Sellers_Users_Email and FK_Sellers_Users_Username.
Actually, this has to do with data that was already entered. Some of the PK and FK data that were entered before might have the same values. You need to delete those first and make the changes you need.
If your project uses SSDT (SQL Server Data Tools), you probably have a foreign key defined in a folder for Keys. Look in "DB Name\Schema Objects\Schemas\dbo\Tables\Keys."
Then Look in the table definition. "DB Name\Schema Objects\Schemas\dbo\Tables". The key could be defined in BOTH places.
I was looking at ways to make a newly added column a Unique but not primary column. I had a look at this W3School link. But Instead of following their approach I simply changed my table in the Visual Studio designer as.
CREATE TABLE [dbo].[Userpro] (
[Id] INT NOT NULL IDENTITY,
[Name] NVARCHAR (50) NULL,
[Postcode] NVARCHAR (4) NULL,
[Gender] INT NULL,
[Blog] NVARCHAR (MAX) NULL,
[FeedBack] NVARCHAR (MAX) NULL,
[Username] NVARCHAR (50) NOT NULL Unique,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
Notice that I simply added "Unique" [Username] NVARCHAR (50) NOT NULL Unique. I am unsure if this has the same effect or should I go back and just use the script in the link.
That is perfect.
Adding UNIQUE will have the effect you describe. It is also explained in the link you provide.