the code i use:
$query = "CREATE TABLE `new_form` as select * from `form`
but new_form can't have default value
like column ID can't auto_increment.
This is not possible with CREATE TABLE … AS SELECT ….
You have to create the new table manually before copying the data.
Related
I am trying to create a new Teradata table by copying another table, but also need to add one new column, based on a condition of another column from the old table while copying, can you help me on the code?
create Table new_table as
(select *
from old_table) with data
ALTER TABLE new_table ADD new_col varchar(20) check(new_col in ('National', 'Local')
-- there is a column in the old_table with value ( 'Y', 'N'), how can i create the new column in the new_table with this condition: if Y new_col=national, if N, new_col=local?
Thank you.
You can't create a check constraint that will immediately be violated. Also note that CREATE TABLE AS (SELECT ...) results in all columns being nullable, and if you don't explicitly state a Primary Index the new table will use system default, e.g. first column alone as PI. A CASE expression can be used to populate the new column.
One possible sequence:
create Table new_table as old_table with no data; -- copy index definitions, NOT NULL attributes
ALTER TABLE new_table ADD new_col varchar(20) check(new_col in ('National', 'Local'));
INSERT new_table SELECT o.*,
CASE WHEN o.old_col = 'Y' THEN 'National' ELSE 'Local' END
FROM old_table o;
CREATE TABLE `projects` (
`stars` integer,
`title` varchar(255)
);
CREATE VIEW projects_view AS SELECT *, title AS name FROM projects;
When I try to insert something:
INSERT INTO `projects_view` (`name`) VALUES ('Name');
I get: ERROR 1471 (HY000): The target table projects_view of the INSERT is not insertable-into
But this works in PostgreSQL.
You cannot insert because by using select * you have two references in the view to a single column.
From the documentation a view is not insertable where
Multiple references to any column of a base table (fails for INSERT, okay for UPDATE, DELETE)
It should work if you name each column:
CREATE VIEW projects_view AS
SELECT stars, title as name FROM projects;
Fiddle example
I have a old table and a new table. what i need is to copy the uuId of the old table to the new Table.
im following some answers from other references but i can`t get the ideal answer.
the closest answer i found is this:
update table1
set table1.uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription)
when i execute this query, it only saves the 1st found uuid of the old table to all the entry in the new Table.
Sample Table2 (old table):
uuid|itemDescription
1|item1
2|item2
3|item3
Sample Table1 (new Table):
uuid|itemDescription
Null|item1
Null|item2
Null|item3
Desired Output:
uuid|itemDescription
1|item1
2|item2
3|item3
what happens:
uuid|itemDescription
1|item1
1|item2
1|item3
In SQLite, you must not use the table name in the SET clause:
update table1
set uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription);
Try like this :
UPDATE table1
SET table1.uuid = table2.uuid
FROM table2 WHERE table1.itemDescription = table2.itemDescription
No need for subquery, otherwise you need to link item description in outer query also
I'm using sqlite3 and my table has a text field that actually has a list of strings.
So a sample select (select * from table where id=1) would return for example
1|foo#bar.com|21-03-2015|["foo", "bar", "foobar"]
I couldn't figure out how the sqlite statement for updating the list is though. I tried
update table set list="["foo", "bar"] where id=1;
update table set list=["foo", "bar"] where id=1;
update table set list="\["foo", "bar"\]" where id=1;
update table set list=(value) where id=1 VALUES (["foo", "bar"])
This is the statement you need:
UPDATE table SET list = '[\"foo\", \"bar\"]' WHERE id = 1
The syntax to create a table is
CREATE [temp] TABLE TableName(...)
so table can be created in the temp database using
CREATE temp TABLE MyTable(...)
or
CREATE TABLE temp.MyTable(...)
and the fully resolved way to refer to that table would be
temp.Mytable
which would be a different table to main.MyTable.
However, the syntax to create an index is just
CREATE INDEX IndexName ON TableName (FieldName )
which does not allow the table name to have the database name prepended
so this is OK
CREATE INDEX MyIndex ON MyTable (MyField)
but this is not
CREATE INDEX MyIndex ON Temp.MyTable (MyField)
How then can I create an index on a temp table (especially if there might be table of the same name in the main database?
The database name must be put on the index name:
CREATE INDEX temp.MyIndex ON MyTable(MyField)