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)
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;
I am trying to create view from table as below
CREATE TABLE tb
(
a INTEGER,
b INTEGER
)
CREATE VIEW vtb AS
(SELECT *
FROM tb);
And then when I executed the following command the type of the column is null
help view vtb;
Is there a way to copy the column datatypes while creating the view ?
Example schema:
CREATE VIRTUAL TABLE posts USING FTS5(title, body);
Select table names:
SELECT name FROM sqlite_master WHERE type='table';
Result:
posts
posts_data
posts_idx
posts_content
posts_docsize
posts_config
How to fetch result only for virtual tables, without *_data, *_idx, *_content, *_docsize and *_config?
The FTS modules use shadow tables to store the actual data and its indexes.
But those are 'real' tables, so you can simply use a filter to get only sqlite_master entries for virtual tables:
SELECT name
FROM sqlite_master
WHERE type = 'table'
AND sql LIKE 'CREATE VIRTUAL TABLE%';
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.
I want to create a table in a SQLite database only if doesn't exist already. Is there any way to do this? I don't want to drop the table if it exists, only create it if it doesn't.
From http://www.sqlite.org/lang_createtable.html:
CREATE TABLE IF NOT EXISTS some_table (id INTEGER PRIMARY KEY AUTOINCREMENT, ...);
Am going to try and add value to this very good question and to build on #BrittonKerin's question in one of the comments under #David Wolever's fantastic answer. Wanted to share here because I had the same challenge as #BrittonKerin and I got something working (i.e. just want to run a piece of code only IF the table doesn't exist).
# for completeness lets do the routine thing of connections and cursors
conn = sqlite3.connect(db_file, timeout=1000)
cursor = conn.cursor()
# get the count of tables with the name
tablename = 'KABOOM'
cursor.execute("SELECT count(name) FROM sqlite_master WHERE type='table' AND name=? ", (tablename, ))
print(cursor.fetchone()) # this SHOULD BE in a tuple containing count(name) integer.
# check if the db has existing table named KABOOM
# if the count is 1, then table exists
if cursor.fetchone()[0] ==1 :
print('Table exists. I can do my custom stuff here now.... ')
pass
else:
# then table doesn't exist.
custRET = myCustFunc(foo,bar) # replace this with your custom logic