How to insert a row into a table that has only a single autoincrement column? - sql

My table has only a single column calld id. This column is an autoincrementing key (I am using it for sequencing). I want to do something like: insert into sequencer; but this gives me SQL errors because I guess I need to have a values portion. But there are no other columns in the table and I want the id column to be autoincremented. I'd also rather not hack this by just adding another dummy column. Thanks.

INSERT INTO table SET id = NULL;
or
INSERT INTO table VALUES (NULL);
When inserting NULL into an auto-increment column, mysql will generate a new number instead.

Related

How can I Insert and update in one query

Trying to write insert and update in the query. So my scenario is - If a row exists then do upsert and insert a new row. But row didn't exist only do insert.
So here what I am trying -
Select exist(Select 1 from table where condition)
if(exists){
Update table set column =value where condition.
Insert into table (column) values (" ").
}else{
Insert into table (column) values (" ")
}
As I am writing here so many queries, is there any possibility I can cover all these in one query.
Thanks in advance
You can write just insert query and check for CONFLICT CLAUSE.
Postgresql supports conflict clause, which is triggered when there was some conflict regarding particular CONSTRAINT.
Lets say you have table t with primary key column id and name field:
CREATE TABLE t ( id integer primary key, name varchar(255));
You can check primary key constraint, when you insert:
INSERT INTO t (id, name) VALUES (1, 'Name1')
ON CONFLICT ON t_id_constraint
DO UPDATE SET name='Name1';
t_id_constraint is constraint name for id column uniqueness.
This query will insert new values to table when there is no conflict and will update when new id is found on table.
Original answer

SQL server inserting values from one table to another

I am trying to insert a column of data from one table to another. The table I am trying to insert into is called MU.PROVIDERS. The table I am inserting from is called Sheet1$. (Imported from an excel sheet). The columns they have in common is called "NPI", a key that is common for all providers, so I am trying to insert based on this key. Not every NPI value in the Sheet1$ will put a corresponding RELAY_ID value into the 'MU.PROVIDERS' table. (There are more NPI's in the 'MU.PROVIDERS' than in Sheet1$) My query is as follows:
INSERT INTO [MU.PROVIDERS] (RELAY_ID)
SELECT h.RELAY_ID
FROM Sheet1$ as h JOIN
[MU.PROVIDERS] as i ON h.NPI = i.NPI;
I am getting the error:
Cannot insert the value NULL into column 'NPI', table 'MU.PROVIDERS'; column does not allow nulls. INSERT fails.
I do have the NPI column set as the primary key on the 'MU.PROVIDERS' table, but I am not inserting anything into this column so I do not understand the problem.
I think you want an update. Insert adds new rows. Update changes the values of columns:
UPDATE i
SET relay_id = h.relay_id
FROM mu.Providers i JOIN
Sheet1$ h
ON h.NPI = i.NPI;
You may have defined the NPI column as the primary key.
But the primary key needs a unique value to identify a row.
If you don't provide the value with your INSERT statement you should define the NPI column as IDENTITY(1,1) to automagically create a new identity value.

Insert or replace in SQLite

I want to insert or replace in SQLite by 2 fields Varchar and Integer. These 2 fields should be unique. So i have to create special table with 2 fields as unique ?
Maybe some one could help on this.
If you want the combination of two fields to be unique, the easiest way is to create a unique index on these fields:
CREATE UNIQUE INDEX field1_field2_idx ON table_name(field1, field2)
then in case of a conflict the INSERT OR REPLACE statement will replace the existing record with the new one.

How can to set column field with related column field in another table?

I have the following two sql tables:
The GlassesID Column in Glasses table is the primery key defined as auto Increaseble.
The GlassesColor table has GlassesID(not auto Increaseble) colomn that defined as Foreign Key.
When Glasses table get a record (from stored procedure)GlassesID automatecly get value.
The GlassesColor.GlassesID column must be set with value from Glasses.GlassesID Column.
My question is how can i implement this? i,e... How can i set column field with related column field in another table?
Immediately after you insert a record into Glasses table, get the ID from Glasses table
declare #GlassesID as int
select #GlassesID = scope_identity();
Then you can use #GlassesID to insert into GlassesColor table.
insert GlassesColor(GlassesID, .....)
values(#GlassesID, .....);

How to insert duplicate rows in SQLite with a unique ID?

This seems simple enough: I want to duplicate a row in a SQLite table:
INSERT INTO table SELECT * FROM table WHERE rowId=5;
If there were no explicit unique column declarations, the statement would work, but the table's first column is declared rowID INTEGER NOT NULL PRIMARY KEY. Is there any way to create a simple statement like the one above that works without knowing the schema of the table (aside from the first column)?
This can be done using * syntax without having to know the schema of the table (other than the name of the primary key). The trick is to create a temporary table using the "CREATE TABLE AS" syntax.
In this example I assume that there is an existing, populated, table called "src" with an INTEGER PRIMARY KEY called "id", as well as several other columns. To duplicate the rows of "src", use the following SQL in SQLite3:
CREATE TEMPORARY TABLE tmp AS SELECT * FROM src;
UPDATE tmp SET id = NULL;
INSERT INTO src SELECT * FROM tmp;
DROP TABLE tmp;
The above example duplicates all rows of the table "src". To only duplicate a desired row, simply add a WHERE clause to the first line. This example works because the table "tmp" has no primary key constraint, but "src" does. Inserting NULL primary keys into src causes them to be given auto-generated values.
From the sqlite documentation: http://www.sqlite.org/lang_createtable.html
A "CREATE TABLE ... AS SELECT" statement creates and populates a database table based on the results of a SELECT statement. A table created using CREATE TABLE AS has no PRIMARY KEY and no constraints of any kind.
If you want to get really fancy, you can add a trigger that updates a third table which maps old primary keys to newly generated primary keys.
No. You need to know the schema of the table to write the insert statement properly.
You need to be able to write the statement in the form of:
insert into Table (column1, column2, column3)
select column1, column2, column3
from OtherTable
where rowId = 5
Well, since I was unable to do this the way I wanted, I resorted to using the implicit row id, which handily enough has the same name as the rowId column I defined explicitly, so now I can use the query I had in the question, and it will insert all the data with a new rowId. To keep the rest of the program working, I just changed SELECT * FROM table to SELECT rowId,* FROM table and everything's fine.
Absolutely no way to do this. Primary Key declaration implies this field is unique. You can't have a non unique PK. There is no way to create a row with existing PK in the same table.