This question already has answers here:
Can you SELECT everything, but 1 or 2 fields, without writer's cramp?
(12 answers)
Closed 8 years ago.
I have a table with many columns (20 or 25) and i want to build an insert statement on it, using select and generating another primary key
The simple soulution will be:
INSERT INTO myTable
SELECT mySeq.nextVal PRIMARY_KEY, COLUMN2, COLUMN3, COLUMN4...
FROM myTable
WHERE PRIMARY_KEY = 1
Since my table have many columns, there is a way to say "i give you primary key, and ALL the other columns are the same" without explain them?
Like:
INSERT INTO myTable
SELECT mySeq.nextVal m.* /* Sure this not work because i get again PRIMARY_KEY column*/
FROM myTable m
WHERE PRIMARY_KEY = 1
There is no way to specify something like SELECT * EXCEPT aColumn, you will have to write them manually as you already did, sorry.
Actually, you could do a subquery that select column from table info then query that subquery but that make non-sense to me.
If your table contains so many column that it is a pain to write them all, then you might want to re-design your database and split your table in multiple tables.
Related
This question already has answers here:
How to select all records from one table that do not exist in another table?
(15 answers)
Closed 1 year ago.
I have one table "Cells" with Primary key "Cell_ID". There are 160 ID's/records. There is one other table, for example "Customers" where I use a field "CellID", which is a table list using row source the field "Cell_ID" from the table "Cells".
I would like to create a query that will return me all the Cell_ID values that are not used on the Customers.CellId field.
My first thought was to use something like the following:
SELECT Cells.Cell_ID
FROM Cells
WHERE Cells.Cell_ID NOT IN (
SELECT Customers.CellID
FROM Customers);
Your method is conceptually okay. But there is a problem if Customers.CellId is ever NULL. If that is the case, then the query will return no rows.
For this reason, I recommend never using NOT IN with a subquery. Instead, use NOT EXISTS:
SELECT c.Cell_ID
FROM Cells as c
WHERE NOT EXISTS (SELECT 1
FROM Customers as cu
WHERE c.Cell_ID = cu.CellID
);
This question already has answers here:
Can you SELECT everything, but 1 or 2 fields, without writer's cramp?
(12 answers)
Closed 5 years ago.
I am using Oracle Database and I need to realize a query which retrieves all the values of a table record (for a specific WHERE condition), except for one which is known.
Imagine to have the following table:
Sample table
Where you do not want to retrieve the "Age" column, but where - in next releases of the software - the table could have more columns respect to the ones actually present.
Is there any command in Oracle which excludes a specific column (always known, as in the example "Age") and allows me to retrieve all the other values?
Thanks in advance!
You can make that particular column Invisible using following query:
alter table TABLE_NAME modify COLUMN_NAME INVISIBLE;
This will exclude that column from select * statement unless and until you specify that particular column in select clause like below:
select COLUMN_NAME from TABLE_NAME;
From Your sample data:
alter table SAMPLE_TABLE modify Age INVISIBLE;
select * FROM SAMPLE_TABLE will produce
select FirstName, LastName, Address, City, Age from SAMPLE_TABLE will produce:
There are several approaches
1)You can set column UNUSED.It won't be retrieved (and it wont be used) with the queries. This would be permanent. You can't get then column back, the only allowed op would be DROP UNUSED COLUMNS.
ALTER TABLE sample_table SET UNUSED(age);
2)You can set column INVISIBLE, this is temporary. It won't be retrieved, unless you explicitly reference it in SELECT query.
ALTER TABLE sample_table MODIFY age INVISIBLE;
// to change it back to VISIBLE
ALTER TABLE sample_table MODIFY age VISIBLE;
3)Create VIEW without age column and then query view instead of querying TABLE.
CREATE VIEW sample_table_view AS
SELECT first_name, last_name, address, city FROM sample_table;
This question already has answers here:
Creating temporary tables in SQL
(2 answers)
Closed 6 years ago.
I have a complicated query I'm working on. It involves several tables.
It would be very helpful for me to create a new table and then simply query from that. However, this is a shared database and I don't want to make a new table, especially when i don't plan on using that table specifically. (I just want it as a stepping stone in my query)
Is it possible to create a table just for 1 query that deletes right when the query is done? (i.e a temporary table)
Sure. Use CREATE TEMPORARY TABLE:
=> CREATE TEMPORARY TABLE secret_table(id BIGSERIAL, name text);
=> INSERT INTO secret_table(name) VALUES ('Good day');
INSERT 0 1
=> INSERT INTO secret_table(name) VALUES ('Good night');
INSERT 0 1
=> SELECT * FROM secret_table;
id | name
----+------------
1 | Good day
2 | Good night
(2 rows)
But upon reconnection:
psql (9.5.4)
Type "help" for help.
=> SELECT * FROM secret_table;
ERROR: relation "secret_table" does not exist
LINE 1: SELECT * FROM secret_table;
You could use temporary tables which drops itself at the end of session in which they were created (not after the query finishes, as you've said). Though, you could always drop it manually at the end of your operation.
If you'd like to create such table as a result from a query then this is the sample to be expanded to your needs:
CREATE TEMP TABLE tmp_table_name AS ( SELECT 1 AS col1 );
But I'm thinking you may be looking for a CTE instead of a table since you're saying that you're planning to use it only once. Consider this:
WITH tmp_table AS ( SELECT 1 AS col1 )
SELECT *
FROM tmp_table
...
You can also do dinamically The result of a query is also a Table
select * from (select col1, col2, col3
from my_complex_table
... ) t1
use keyword temporary, the temporary table is only visible in your current connection and drop after you disconnect your connection.
The other way would create a table and drop the table by yourself when you don't need it
This question already has answers here:
How can I create a copy of an Oracle table without copying the data?
(17 answers)
Closed 9 years ago.
How to create table with existing table structure without iterate row by row like this in Oracle? Thanks in Advance.
CREATE TABLE new_table
AS (SELECT *
FROM old_table WHERE 1=2);
If you are worried about iterating through the table:
CREATE TABLE new_table
AS (SELECT *
FROM (select * old_table where rownum = 1) t
WHERE 1=2
);
I have already read about this.. Hope it gives a Detailed explanation to you..
What we ended up doing in this clients case was to replace the “WHERE 1=2” with a clause that equated the primary key of the table with an impossible value for that key, in this case the ID was being passed in as a GUID (a hexadecimal value) so we use a “WHERE KEY=HEX(00)” and got a low cost unique index lookup instead of a costly full table scan.
http://www.dba-oracle.com/oracle_tips_ault_where_1_equals_2_parallel_.htm
Thanks to Burleson Consulting
I'm not sure on the exact Oracle syntax but in virtually any SQL if you open up the other table using a GUI tool there are options both to generate a create script statement for the table and to backup the table without data.
Either of those will do what you need.
This question already has answers here:
SQLite INSERT - ON DUPLICATE KEY UPDATE (UPSERT)
(5 answers)
SQL: How to update or insert if doesn't exist?
(2 answers)
Closed 9 years ago.
I have an sql table with 3 columns, none of which is UNIQUE ( but the pair name + role is ):
Name | Role | Votes
What I need to do is, write an sqllite query that stick to the following rules :
If a row with the given name and role already exist, votes is
incremented by 1
If not, a new row is created with Votes = 1
I've looked into INSERT OR REPLACE and this great post but it doesn't seem to help me that much, and I'm not even sure INSERT OR REPLACE is a viable option, since something like
INSERT OR REPLACE INTO words (name,role,votes)
VALUES ('foo','bar', coalesce((
select votes from words where name = 'foo' and role='bar'),0)+1)
always insterts and never replace
You simply need to create unique index over your 2 columns for this to work:
CREATE UNIQUE INDEX words_name_role_idx ON words (name,role)
Note that you do not create unique index for any single column, but for combination of 2 as a whole.
After that, your REPLACE INTO statement should start working correctly:
REPLACE INTO words (name,role,votes) VALUES ('foo','bar',
coalesce((
SELECT votes FROM words
WHERE name = 'foo' AND role='bar'),0
)+1
)
(note that I have changed counter to votes above).
This query will update your record with +1.
update todo set text='raj',complete='raj',pk=((SELECT pk FROM todo where text='raj' and complete='raj')+1) where (SELECT pk FROM todo where text='raj' and complete='raj')
EDIT YOUE QUERY
update words set name='foo',role='bar', votes =((SELECT votes FROM words where name='foo' and role='bar')+1) where (SELECT votes FROM words where name='foo' and role='bar')
And make insert query if this condition will not true.