SQ Lite insert just a single identifier - sql

I have the following table in my database
" CREATE TABLE Q_GROUP ( Id INTEGER PRIMARY KEY ); "
This is only needed to ensure different Items are in the same group. Each time, when adding new items, I need to create a unique group. The items are then connected to this group. The usual syntax for adding items and auto-incrementing the identifier is to specify the items but not the identifier. In this case, sq lite gives a syntax error when attempting this.
Should I add a foo value to the table, or is there a better way to do this in SQ Lite?
-- edit --
The following queries give a syntax error:
INSERT INTO Q_GROUP VALUES;
INSERT INTO Q_GROUP VALUES ();
INSERT INTO Q_GROUP () VALUES ();
INSERT INTO Q_GROUP ;

try like this,
insert into Q_GROUP values(1);

Use null as placeholder
insert into Q_GROUP (Id)
values (null);
SQLFiddle demo

To insert the default values in all columns, use:
INSERT INTO Q_GROUP DEFAULT VALUES;

Related

INSERT INTO with default values for a single column

I have a problem to insert the data into 1 table with 1 column
Name: user_id
Column: id
I am trying to add 1 line in this column with this query:
INSERT INTO user_id (id) VALUES ()
The problem is the above is invalid, I want the id take the last value id +1
This is not a syntax problem because this query works:
INSERT INTO user_id (id) VALUES (4)
So, I do not really know how to solve this problem.
Assuming the id column is defined as serial or identity you can specify a column list and set the column value to default:
insert into user_id (id) values (default);
This also works if you have more columns, e.g:
insert into users (id, firstname, lastname)
values (default, 'Arthur', 'Dent');
Or you can leave out the column list completely and request the default value(s) for all columns:
insert into user_id default values;
SQL supports the default values statement.
So this will work:
create table t (id serial primary key);
insert into t
default values;
The syntax is described in the documentation.

Insert Into Oracle Table with single, autoincrement Column

Imagine the following (fictional) situation:
You have a table with only one column id that is the primary key, autoincremented by using a typical sequence + trigger combination.
How would you create a new row there as you have to specify the values keyword for the insert query?
INSERT INTO table () VALUES () is not valid as far as I understood.
ATTENTION:
This is not a discussion about the sense of such a table! It is out of pure technical interest.
In any current Oracle version (12.1, 12.2, 18) I would not use a trigger but an identity column - then use the default keyword during insert:
create table x (id integer generated by default as identity);
insert into x (id) values (default);
How about
INSERT INTO theTable (id) VALUES (null);
and your before insert trigger would be like:
if :NEW.id is NULL Then
SELECT id_sequence.NEXTVAL INTO :NEW.id FROM dual;
end if;

SQL error for creating table and inserting values in it. Error -12233 and 12101

So I'm trying to create a table in SQL and then insert values into it. However, I seem to be getting this error:
[Error Code: -12101, SQL State: 42000] Syntax error, 'CHECK' assumed
missing
and
[Error Code: -12233, SQL State: 42000] The number of insert values is
not the same as the number of object columns
Here is my SQL code:
CREATE TABLE Server(
Nummer INTEGER NOT NULL
PRIMARY KEY(Nummer)
);
INSERT INTO Server(Nummer)
VALUES (1,2,3,4,5);
So I want to create a table named Server which has a primary key named nummer. Nummer then has the values 1,2,3,4,5
UPDATE--------------------------------------------------------------------
So my new code is:
CREATE TABLE Server(
Nummer INTEGER NOT NULL,
PRIMARY KEY(Nummer),
);
INSERT INTO Server(Nummer)
VALUES (1);
INSERT INTO Server(Nummer)
VALUES (2);
INSERT INTO Server(Nummer)
VALUES (3);
INSERT INTO Server(Nummer)
VALUES (4);
INSERT INTO Server(Nummer)
VALUES(5);
I solved the check problem by simply putting a comma after every statement in the create section.
But I got a new problem which is this error code:
[Error Code: -12101, SQL State: 42000] Syntax error, IDENTIFIER
IDENTIFIER assumed missing
You have a table with a single colums so you can use multiple insert as
INSERT INTO Server(Nummer)
VALUES (1);
INSERT INTO Server(Nummer)
VALUES (2);
.....
OR If you want batch insert you should use this way
INSERT INTO Server(Nummer)
VALUES (1),(2),(3),(4),(5);
You can't insert a list of values like that. You have to create 5 insert clauses. If you're using Sql server, you can create one insert clause, with five values.

In PostgreSQL how do you insert into a table with only one identity column?

For instance:
{create table Participant ( id serial, primary key(id) );}
How do you insert into table in this case?
If you create the table like above,
You can use default in following way to insert:
INSERT INTO Participant values(default);
Check out SQLFIDDLE.
Another way to insert is:
INSERT INTO Participant values(NEXTVAL('Participant_id_seq'));
CREATE TABLE will create implicit sequence "Participant_id_seq" for serial column "Participant.id".
You can get the sequence for the table using pg_get_serial_sequence function in following way:
pg_get_serial_sequence('Participant', 'id')
It will take new value from sequence using NEXTVAL().
Check out SQLFIDDLE
insert into Participant values (default);

Inserting a new row into a single column table in apache derby with generated id

I have the following table:
create table indices (
id int primary key generated by default as identity
);
how do I insert a new row?
I have already tried several things I have found, like:
insert into indices values (null);
insert into indices default values;
however that didn't work with derby.
Try "insert into indices values (default)"
You need a column to insert into. You have id which is an identity, which means it will already have a value when a new row is inserted. Add a new column and then fill that column in your insert statement.