cannot insert the NULL value into the column - sql

I have been trying to insert data into the database, but it's continuously showing me this error, even though I not writing the name of the column(MemID) in the first fields, rather I am trying to enter it separately like this
create table Member (
MemID VARCHAR(20) PRIMARY KEY,
FullName VARCHAR(50),
email VARCHAR(50),
gender VARCHAR(50),
Contact_Number VARCHAR(50),
Registration_Number VARCHAR(50),
User_S# INT
);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Cleo Glynn', 'cglynncg#i2i.jp',
'Male', '81-(694)548-5205', '5443114970343516', 450);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Ivonne Deetlefs',
'ideetlefsch#virginia.edu', 'Female', '86-(257)683-5628',
'3571675846170605', 451);
insert into Member(MemID)
values('Mem0');
insert into Member(MemID)
values('Mem1');
insert into Member(MemID)
values('Mem2');
This code is giving me the error
Cannot insert the value NULL into column 'MemID', table
'master.dbo.Member'; column does not allow nulls. INSERT fails.
But I am inserting MemID column separately.
And also the table which has created above is not showing in the database, is it because of the error or something else?

That's not how inserts work.
When an insert is triggered sql tries to add the row immediately, without waiting for the consequent statements (it's stands true for any dml statements not only insert).
So when your first insert is triggered then sql is trying to put null in MemID as you haven't supplied any MemID in your insert.
Try inserting it in the very first place itself in the insert. Instead of separate statement something like:
insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Mem0','Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
EDIT
With response to your comment , I think Prepared statements is what you're looking for.
Since you're using python you could do prepare your insert statement like:
sql = "insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values (%s,'Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);"
sql = sql.format(self.db_scan_table)
//whenever you've memid ready use.
self.cursor.execute(sql, ('Mem01'))

Define an AUTO_INCREMENT column to generate ID values for you:
CREATE TABLE Member (
MemID INT AUTO_INCREMENT PRIMARY KEY,
-- ...
);
If this isn't practical because you need some kind of particular string definition for these then your only option is to supply it as part of the initial INSERT call. You have a constraint here, the PRIMARY KEY value must not be NULL.

should be able to using the query below. identify the next number
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member)+1 as varchar(100)),... )
if you want to start with 1 then + 1
otherwise remove + 1
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member) as varchar(100)),... )

Related

Storing an object into a SQL database in Oracle

Simply trying to find the correct syntax/method to enter create SQL objects and store them inside an oracle database. (school project so it's got to be possible)
CREATE OR REPLACE TYPE Person AS OBJECT
(id NUMBER,
fname VARCHAR(255),
lname VARCHAR(255)) NOT FINAL
CREATE OR REPLACE TYPE customer UNDER Person (
num_purchases NUMBER,
email VARCHAR(255)
);
CREATE TABLE Customers (
id NUMBER,
cust customer
);
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, "John", "Doe", 44, "doezer#gmail.com"));
Returns the error
ORA-00984: column not allowed here
I can't seem to find any way to declare a specific order for putting the values inside of the customer object other than the order that they were declared in. Thank you!
Seems you are inserting columns not literal values. replace your double quotes to single quotes.
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, 'John', 'Doe', 44, 'doezer#gmail.com'));
SELECT ID, TREAT(cust AS Person).id, TREAT(cust AS Person).fname FROM Customers;
See dbfiddle

Missing Semicolon at the end of sql statement Access

I am trying to execute the following code. However, I continue to recieve the following 'Missing semicolon (;) at the end of SQL statement error in Microsoft Access.
The first query creates the table with the columns defined.
create table test
(
ProcessID int,
Name varchar(10),
Address varchar(10),
RandomData varchar(10)
);
The second query is causing the missing semicolon error.
INSERT into test
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri'),
(456 , 'TestName2', 'TestAdd', 'qwerty'),
(789 , 'TestName', 'TestAdd', 'qwrj3ri'),
(1234, 'Testing123', 'tester', 'asdfghjk');
Code with amendments per above comments to make it Access friendly & remove typos:
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (123 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (456 , 'TestName2', 'TestAdd', 'qwerty');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (789 , 'TestName', 'TestAdd', 'qwrj3ri');
INSERT INTO test ( ProcessID, Name, Address, RandomData)
VALUES (1234, 'Testing123', 'tester', 'asdfghjk');
Useful reference: https://msdn.microsoft.com/en-us/library/bb243852(v=office.12).aspx
Specific comments:
#Damien_The_Unbeliever:
I don't think access supports multiple rows in the values.
Amended to include an insert into per row instead of a value set per row (values (...), (...)).
#Thomas Tschernich:
our missing single quote next to the end of your insert
Changed 'tester', sdfg') to 'tester', 'sdfg');
#JohnLBevan:
superfluous character on end of first set of values
Changed 'qwrj3ri'), T to 'qwrj3ri'),
You can insert multiple rows in one insert statement in SQL server,but in MS ACCESS its not possible as above listed.
More techniques on multiple inserts in access are described
beautifully here

SQLite Insert with the newly generated id

I have a table with an auto increment field and another field VARCHAR filed. I would like to fill the string field with the newly generated id directly in the insert query. For example with table:
Person
(
id auto_increment,
name varchar
)
I would like to perform a query like this:
INSERT INTO Person(name) VALUE ('Person #' + ROWID())
Anyway to do this?
This will use the last inserted row id with the increment of 1 to generate the next person id. Keep in mind that for last_insert_rowid() you should not be doing concurrent inserts to the database.
INSERT INTO Person (name) SELECT 'Person #' || (last_insert_rowid()+1);
Alternatively this will read the biggest ID you currently have and increment +1 to it and then concatenate it with the text. If it returns null it uses 1.
INSERT INTO Person (name) SELECT 'Person #' || (IFNULL((MAX(id)+1),1)) FROM Person LIMIT 1;
This one will get the index of autoincrement:
INSERT INTO Person (name) SELECT 'Person #' || (seq+1) FROM sqlite_sequence WHERE name="person"
You can also do something like this:
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
INSERT INTO Person (name) VALUES (NULL);
UPDATE Person SET name = 'Person #' || id WHERE name IS NULL;
Or:
INSERT INTO Person (name) VALUES ('Person');
UPDATE Person SET name = name || ' #' || id WHERE id = last_insert_rowid();
This one will update the names that does not have a # with its row id:
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
INSERT INTO Person (name) VALUES ('Person');
UPDATE Person SET name = name || ' #' || id WHERE (LENGTH(name)-LENGTH(REPLACE(name, '#', ''))) = 0;
Live DEMO
You shouldn't store the same info twice. Just generate the string you want dynamically when you query:
SELECT name || '#' || id FROM Person
You have to do two sentences: INSERT + UPDATE, you cannot access the last insert id in the current insert sentence
The system will not reliably know that ID until the row is inserted. I would think the best solution would be a view (since SQLite does not support computed columns which would be the easiest route). Just insert the VARCHAR and use a view to build the concatenated value at request time using the inserted VARCHAR and the allocated ID.
Good Luck!
This will not be guranteed but this might work ...
INSERT INTO tab1(val) VALUES ('tab1 #' + cast((SELECT top 1 IDENT_CURRENT('tab1') + IDENT_INCR('tab1') FROM tab1) as varchar(20)))

Inserting "bad" data into a SQL database?

I'm writing a query that inserts customer data into a MSSQL database. Very basic.
Unfortunately, I ran into a problem when trying to do the following:
INSERT INTO USERS(newid(),'BOB''S SELECT MARKETING')
I made sure to escape my quotes, but the server is still seeing SELECT as a reserved keyword. I don't want to have to wrap a bunch of reserved words in brackets. Is there a cleaner way of getting my data in the database intact and not mangled by brackets?
I appreciate your help.
Thank you!
You are missing the Key word VALUES:
INSERT INTO USERS VALUES (NEWID(),'BOB''S SELECT MARKETING');
You have several choices of syntax here. Using the one in your code sample, you forgot the VALUES keyword. For example:
declare #users table
(
id uniqueidentifier,
name varchar(50)
)
insert into #users values (newid(), 'BOB''S SELECT MARKETING')
You can also use the insert into / select statement like below if you are inserting a value into each one of the table's columns:
declare #users table
(
id uniqueidentifier,
name varchar(50)
)
insert into #users
select newid(), 'BOB''S SELECT MARKETING'
Or you can use the insert into / select statement and specify the columns you are inserting:
declare #users table
(
id uniqueidentifier,
name varchar(50)
)
insert into #users (id, name)
select newid(), 'BOB''S SELECT MARKETING'

can't insert data into table

I have created table using this command successfully
create table Person(
first_name varchar(25) not null,
last_name varchar(25) not null,
persoin_id number not null,
birth_date date,
country varchar (25),
salary number);
and now I want to insert data into that table
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia');
first row is inserted,but problem is with second line
1 rows inserted.
Error starting at line 10 in command:
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
Error report:
Unknown Command
Please help me to determine what is problem?thanks
If you are on a RDBMS that supports multi-rows inserts in one INSERT:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia') ,
--- comma here ---^
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia') ;
^--- no "values" here
If not (like Oracle), you'll have to issue two insert statements:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia') ;
--- as it was here ---^
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia') ;
or use this approach:
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
select
(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia')
from dual
union all select
(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
from dual
;
You will need to use 2 insert statement instead of one for 2 different sets of data...
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
insert into Person(persoin_id,first_name,last_name,salary,birth_date,country)
values(101,'irakli','oqruashvili',350,to_date('01/03/10','DD/MM/YY'),'georgia')
You have a ; at the end of:
values(100,'dato','datuashvili',350,to_date('01/01/10','DD/MM/YY'),'georgia');
^
change it to , and also loose the values from the next line. You need just one values per insert.