Getting the error
Cannot insert the value NULL into column 'id', table
'master.dbo.Problems'; column does not allow nulls. INSERT fails.
on my table created with
CREATE TABLE Problems (
id INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
prompt_code VARCHAR(5000) NOT NULL,
created DATETIME DEFAULT CURRENT_TIMESTAMP
);
after I try to run the basic command
INSERT INTO Problems (prompt_code) VALUES ( '<span>Blah blah blah</span>' );
What gives?
Related
I wrote a simple insert into statement:
insert into Worker(WorkingPlace)
select WorkingPlace
from source.Worker;
I got this error:
Cannot insert the value NULL into column 'Worker_ID', table 'Worker'; column does not allow nulls. INSERT fails
(By the way Worker.WorkingPlace is a nullable column)
I'm very confused, because I'm just inserting data into the 'WorkingPlace' column, aren't I?
insert into Worker(WorkingPlace)
select WorkingPlace
from source.Worker;
This will create as many new rows in table Worker as there are rows returned from the select.
So if your query returns 10 rows, then 10 new rows will be inserted into table Worker
This means that any column in that table that is defined as not null must receive a value, either from your query, or get a default value when that is defined.
suppose your table looks like this
create table Worker (id int not null, WorkingPlace varchar(50) null, SomeValue int not null)
then you cannot insert a row like this
insert into Worker (WorkingPlace) values ('something')
This will fail because you have not provided a value for then column id that is defined as not null, and no value for the column SomeValue because that is also defined as not null
if your table looks like
create table Worker (id int primary key not null identity, WorkingPlace varchar(50) null, SomeValue int not null)
then the statement
insert into Worker (WorkingPlace) values ('something')
Will only fail because there is no value for the column SomeValue.
The column id will get its value from the identity
Solution:
insert into Worker (WorkingPlace, SomeValue) values ('something', 123)
Try this way
DECLARE #Worker TABLE (id int,
WorkingPlace varchar(50),
SomeValue varchar(50))
INSERT INTO #Worker(id,SomeValue)
SELECT w.id, w.wPlace, w.something
FROM [source].[Worker] AS w
I tried using SERIAL and GENERATED ALWAYS AS IDENTITY but always got an error whenever attempting to insert into any table that use them.
Example:
CREATE TABLE foo (
id SERIAL NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL
);
CREATE TABLE bar (
id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
name VARCHAR(255) NOT NULL
);
INSERT INTO foo
VALUES ('JOHN');
INSERT INTO bar
VALUES ('JANE');
I would get invalid input syntax for type integer: "John" when inserting into foo
and invalid input syntax for type integer: "Jane" when inserting into bar. As far as I know, it should automatically fill in "id" starting from 1 and only require "name" from the INSERT INTO statement.
Is there something I did wrong?
I have a SQL Server table created with:
IF OBJECT_ID('dbo.Messages', 'U') IS NOT NULL
DROP TABLE dbo.Messages
GO
CREATE TABLE dbo.Messages
(
Id INT NOT NULL PRIMARY KEY, -- primary key column
Username [NVARCHAR](50) NOT NULL,
MessageType [NVARCHAR](50) NOT NULL,
Recepient [NVARCHAR](50) NOT NULL,
RecepientType [NVARCHAR](50) NOT NULL,
Payload [NVARCHAR](255),
Stamp DATETIME
);
GO
When I try to insert data into the table, an error happens.
Insert statement:
INSERT INTO dbo.Messages
VALUES ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
Column name or number of supplied values does not match table
definition.
Is this happening because my datetime needs to formatted for SQL Server, or do I need to describe the primary key differently in to order to have SQL Server autogenerate values for this column?
What would be the correct way to do this?
You are missing ID in your insert statement.
If you want ID to be autogenerated you need to use IDENTITY(1, 1) where you choose seed and increment step in place of 1's.
If you don't want to autogenerate ID, then you need to include it in INSERT statements.
Column definition should be:
Id INT IDENTITY(1, 1) PRIMARY KEY
Consider change stamp column to timestamp and id to identity column like this:
IF OBJECT_ID('dbo.Messages', 'U') IS NOT NULL
DROP TABLE dbo.Messages
GO
CREATE TABLE dbo.Messages
(
Id INT NOT NULL IDENTITY(1, 1) PRIMARY KEY, -- primary key column
Username [NVARCHAR](50) NOT NULL,
MessageType [NVARCHAR](50) NOT NULL,
Recepient [NVARCHAR](50) NOT NULL,
RecepientType [NVARCHAR](50) NOT NULL,
Payload [NVARCHAR](255),
Stamp [timestamp] NOT NULL -- timestamp column
);
GO
Then, simply insert your data:
INSERT INTO dbo.Messages
(Username, MessageType, Recepient, RecepientType, Payload)
VALUES('thin', 'message.broadcast', 'channelID', 'channel', 'test')
Although your error clears that 6 values are passing for 7 columns.
You need to insert specifically column names.
Insert into Messages ( Username , MessageType , Recepient , RecepientType , Payload , Stamp )
values ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
After that also you are facing an issue which is corrected by this.
If Id want to be inserted automatic, then make it idenity like Id int identity(1, 1) not null
You need to add as many columns in your insert statement as there are columns in your table, except for identity fields.
Without your Id as identity, you need this
insert into Messages (Id, Username, MessageType, Recepient, RecepientType, Payload, Stamp)
values (1, 'thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
This means its up to you to determine the next value for your Id every time.
Better is to create the Id field as identity like this
Id INT IDENTITY(1, 1) PRIMARY KEY
Now you can simply do this (the Id value will be automatic incremented now)
insert into Messages (Username, MessageType, Recepient, RecepientType, Payload, Stamp)
values ('thin', 'message.broadcast', 'channelID', 'channel', 'test', '2019-07-23 15:19:08.960697828 +0300 EEST m=+14.232534538')
Using column names in your insert also has the benefit that you can put them in any order you like
CREATE TABLE Type1
(
TypeID TINYINT NOT NULL IDENTITY(1,1),
TypeName VARCHAR(20) NOT NULL,
Speed VARCHAR(10) NOT NULL
CONSTRAINT TypeID_pk PRIMARY KEY (TypeID)
);
CREATE TABLE Splan
(
PlanID TINYINT NOT NULL IDENTITY(1,1),
TypeID TINYINT NOT NULL,
PlanName VARCHAR(20) NOT NULL,
Quota SMALLINT NOT NULL
CONSTRAINT PlanID_pk PRIMARY KEY (PlanID)
CONSTRAINT TypeID_fk FOREIGN KEY (TypeID) REFERENCES Type1(TypeID)
);
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps'),
('Other type', '50Mbps');
^Up until there its fine
and then when I enter the following it returns "Msg 515, Level 16, State 2, Line 8
Cannot insert the value NULL into column 'TypeID' ..... column does not allows. INSERT fails." Statement terminates
INSERT INTO Splan(PlanName, Quota)
VALUES ('Some sample name', '500GB'),
('sample2, '250GB');
I've tried creating the constraints at both column and table level but the second INSERT statement still refused to enter. Double checked via the GUI and 'TypeID' definitely has an IDENTITY property.
I've looked about everywhere and this errors seems to stem from the lack of an IDENTITY property, yet its present in my creation statements and the error still comes up. Tried removing the seed and increment from IDENTITY, still nothing. Also tried inserting the data one row at a time, nothing there either.
P.S If you haven't noticed the actual names have been substituted and other columns rows have been omitted.
Since you created typID as NOT NULL, Sql is complaining that the default value (NULL) is not acceptable.
Try
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Where corresponding records with TypeID = 1 and TypeID = 2 are in your Type1 table.
You are creating 2 tables: Type1 which has a primary key TypeId that is auto generated
and SPlan which has a primary key PlanId that is also auto generated and a foreign key TypeId that must be supplied and cannot be null.
As written you must enter 1 or more records into Type1 first, obtain their TypeIds, then enter those TypeIds into new records in SPlan.
Incidentally, using TINYINT for your primary key data types is perfectly legal but probably a really bad idea if this is anything other than homework.
You need to supply a value for TypeID in your second query because you have a foreign key relationship with the Type1 table and because the TypeID column in the Splan table is also declared NOT NULL.
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (1, 'Some sample name', '500GB'),
(2, 'sample2, '250GB');
Try inserting both records in a transaction using SCOPE_IDENTITY
begin tran
INSERT INTO Type1(TypeName, Speed)
VALUES ('Sample type', '10Mbps')
DECLARE #id INT = SCOPE_IDENTITY();
INSERT INTO Splan(TypeID, PlanName, Quota)
VALUES (#id, 'Some sample name', '500GB')
commit
I've created a insert stored procedure with two tables like in the exapmle:
Table NameAge
CREATE TABLE [dbo].[Assignment3_NameAge]
(
userID int PRIMARY KEY IDENTITY(1,1),
Name varchar(255) NOT NULL,
Age int NOT NULL
)
Table Hobbies
CREATE TABLE [dbo].[Assignment3_Hobbies]
(
hobbiesID int Identity(1,1) Primary Key,
userID int Foreign Key references Assignment3_NameAge(userID),
hobbies varchar(255) NOT NULL,
)
Insert Stored Procedure
CREATE PROCEDURE [dbo].p_Assignment3Join_ins
#Name nvarchar(100),
#Age int,
#Hobbies nvarchar(100)
AS
INSERT INTO [TABLE].[dbo].[Assignment3_NameAge]
([Name]
,[Age])
VALUES (#Name,#Age)
INSERT INTO [TABLE].[dbo].[Assignment3_Hobbies]
([Hobbies])
VALUES (#Hobbies)
The problem is that when i run the stored procedure the table Hobbies has a null value for userid(the foreign key)
What am i doing wrong?
You should provide the key of the Assignment3_NameAge value you want to insert into Assignment3_Hobbies.
If you want the last inserted you can use SCOPE_IDENTITY() from SQL Server(if you're using SQL Server) or equivalent. It will give you the last inserted value from Assignment3_NameAge
I am guessing this is SQL Server based on the IDENTITY column. Correct?
The first insert creates a user, but there is no user ID being set on the insert of the hobby. You need to capture the identity value from the first insert to be used in the second insert. Have you gon over the system functions available?
You're not supplying a value for it, SQL won't automagically fill the value in for you even though you've created a Foreign Key relationship. It's your job to populate the tables.