I have some temp table:
CREATE TEMP TABLE IF NOT EXISTS temp_test (
col1 INTEGER NOT NULL,
col2 CHARACTER VARYING NOT NULL,
col3 BOOLEAN);
Then I do some inserts into temp_test (that works fine).
Later, without creating a new table test, I try doing the following:
INSERT INTO test(col1,col2,col3) SELECT col1,col2,col3 FROM temp_tes;
And I get the following error:
ERROR: relation "test" does not exist
I thought that if I'm using INSERT INTO, it should create the table for me. does it not?
If it matters, I'm using PostgreSQL 9.6.16.
You are wrong. INSERT inserts into an existing table; it does not create a table.
If you want to create a table, use CREATE TABLE AS:
CREATE TABLE test AS
SELECT col1, ol2, col3
FROM temp_tes;
Related
I created a table in DBBrowser:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId UNINDEXED,
StudentName
);
and insert values to it. After that I add DB with this table to my project.
It is declaration of this table in sqldelight .sq file:
CREATE VIRTUAL TABLE IF NOT EXISTS Students USING FTS5
(
GroupId INTEGER AS Int,
StudentName TEXT,
rank REAL
);
I need to explicit declare rank because I want to apply HAVING MIN(rank) for it when SELECT from table (otherwise it is not compile), but when I trying to insert values in table like that:
insert:
INSERT INTO Students VALUES (?,?);
I receive an error:
Unexpected number of values being inserted. found: 2 expected: 3
If I do like that:
insert:
INSERT INTO Students VALUES (?,?,?);
I receive an exception:
SQLiteException - table Students has 2 columns but 3 values were supplied (code 1): , while compiling: INSERT INTO Students VALUES (?,?,?)
How I can perform insert? Or maybe I can apply HAVING MIN(rank) without explicit declare?
does
insert:
INSERT INTO Students(GroupId, StudentName) VALUES (?,?);
work?
Can I do something like this, column is of type nchar(8), but the string I wanted to store in the table is longer than that.
The reason I am doing this is because I want to convert from one table to another table. Table A is nchar(8) and Table B is nvarchar(100). I want all characters in Table B transfer to Table A without missing any single character.
If the nvarchar(100) contains only latin characters with a length up to 16 chars, then you can squeeze the nvarchar(100) into the nchar(8):
declare #t table
(
col100 nvarchar(100),
col8 nchar(8)
);
insert into #t(col100) values('1234567890123456');
update #t
set col8 = cast(cast(col100 as varchar(100)) as varbinary(100))
select *, cast(cast(cast(col8 as varbinary(100)) as varchar(100)) as nvarchar(100)) as from8to100_16charsmax
from #t;
If you cannot modify A, then you cannot use it to store the data. Create another table for the overflow . . . something like:
create table a_overflow (
a_pk int primary key references a(pk),
column nvarchar(max) -- why stop at 100?
);
Then, you can construct a view to bring in the data from this table when viewing a:
create view vw_a as
select . . . , -- all the other columns
coalesce(ao.column, a.column) as column
from a left join
a_overflow ao
on ao.a_pk = a.pk;
And, if you really want to "hide" the view, you can create an insert trigger on vw_a, which inserts the appropriate values into the two tables.
This is a lot of work. Simply modifying the table is much simpler. That said, this approach is sometimes needed when you need to modify a large table and altering a column would incur too much locking overhead.
I have a simple, as it looks, question:
I am having Sqlite query where I am using temp tables and then joining those table, something like:
drop table if exists SourceA
create temp table SourceA (id int, value text);
insert into SourceA select id, value from TableA
drop table if exists SourceB
create temp table SourceB (id int, value text);
insert into SourceB select id, value from TableB
select SourceA.*, SourceB.* from SourceA join SourceB on SourceA.id = SourceB.id
Now is it possible to create some sort of procedura if or case so for example one of those insert will not be executed, like:
bool merge = false;
drop table if exists SourceA
create temp table SourceA (id int, value text);
insert into SourceA select id, value from TableA
if(merge)
{
drop table if exists SourceB
create temp table SourceB (id int, value text);
insert into SourceB select id, value from TableB
select SourceA.*, SourceB.* from SourceA join SourceB on SourceA.id = SourceB.id
else
{
select * from SourceA
}
Example of usage is simple, its like a condition when you determine seconds two dates, let's say second table counts missing seconds, so now if interval is too big app will crush. So i need to determine outside of SQL if such thing can be done, and pass it to SQL (basically it can be any condition that will exclude second insert from happening)
I've mange to handle my problem by using variables temp table like:
drop table if exists Variables;
create temp table Variables (MinDatetime Datetime, MaxDatetime Datetime);
insert into Variables Values(#MinDatetime, #MaxDatetime);
And then passing parameter from code, an just omitting result using case something like:
case when (Select MinDatetime from Variables) = 'None' or (Select MaxDatetime from Variables) = 'None' then 0
else value
end as value
And if value is 'None' it will return just plain 0
SQLite is designed as an embedded database, to be used together with a 'real' programming language, so it does not have any procedural features.
Put the control logic into your actual program (or, if you don't have one, write a script).
I have a table designed like,
create table tbl (
id number(5),
data blob
);
Its found that the column data have
very small size data, which can be stored in raw(200):
so the new table would be,
create table tbl (
id number(5),
data raw(200)
);
How can I migrate this table to new design without loosing the data in it.
This is a bit lengthy method, but it works if you are sure that your data column values don't go beyond 200 in length.
Create a table to hold the contents of tbl temporarily
create table tbl_temp as select * from tbl;
Rem -- Ensure that tbl_temp contains all the contents
select * from tbl_temp;
Rem -- Double verify by subtracting the contents
select * from tbl minus select * from tbl_temp;
Delete the contents in tbl
delete from tbl;
commit;
Drop column data
alter table tbl drop column data;
Create a column data with raw(200) type
alter table tbl add data raw(200);
Select & insert from the temporary table created
insert into tbl select id, dbms_lob.substr(data,200,1) from tbl_temp;
commit;
We are using substr method of dbms_lob package which returns raw type data. So, the resulted value can be directly inserted.
I'm working with sqlite and trying to create a table from another.
This works:
create table sources_tmp as select "literal" system,name,user from sources;
but it doesn't allow me to specify the type for the "literal".
I've tried this but doesn't work that way apparently:
create table sources_tmp as select "literal" system as $TYPE,name,user from sources;
Use CAST(). "AS" means something else. Example:
CREATE TABLE TEST(ID INT, NAME VARCHAR);
INSERT INTO TEST VALUES(1, '10');
CREATE TABLE TEST2 AS
SELECT CAST(ID AS VARCHAR) A, CAST(NAME AS INT) X FROM TEST;