is there a command for "AS" in postgres? - sqlite

I'm wondering if there is a command like AS for postgres. Does anyone know if postges has this ability? I've tried to google it but it's a very difficult question to google :P I want to make a select statement and store it as a new table name. I want to say something like:
select subj, user as 'new' from table_name;

Yes. It's a reserved SQL key word in PostgreSQL. See Table C-1 at the linked documentation page.
It's typically used with column labels.
The AS keyword is optional, but only if the new column name does not
match any PostgreSQL keyword (see Appendix C).

CREATE TABLE new_table AS SELECT subj, user FROM table_name
So, your new table will be ready.

"I want to make a select statement and store it as a new table name" --
CREATE VIEW view_name AS
SELECT subj, user AS "new" FROM table_name;

If you don't want to leave any changes in the schema, you can do something like this-
select new.* from (
select foo, bar from old
) as new

develop your query: SELECT field AS new_field FROM table WHERE ...;
If it runs ok, copy it
Go to VIEW and do that: CREATE OR REPLACE VIEW new_view AS <QUERY>;
Save the view and use it as table.
enjoy.

In my way, you can do alias for the field and create view to store it. which ll act like table.
select subj, user as new from table_name;
By using the above query can get subj and new as a field.
And create view.
create or replace view_name as
select subj, user as new from table_name;
And just call
select view_name;

Related

Cassandra Select Query using Collection Primary Key in Where Clause

I'm new to Cassandra and I created a table with a frozen collection as the primary key
cqlsh> create table rick_morty (id uuid, name text, adventure text, instigator frozen<set<text>>, PRIMARY KEY((instigator), adventure, name, id));
Now I want to query based on the primary key (instigator) for all of the values held in the collection. I have found that if I just wanted to query on 1 value, I can use CONTAINS 'contained_value', but I want to query on the entire collection.
I've been looking all over to figure out how to do this but I can't find the answer.
Doing something like
const query = 'SELECT name from rick_morty';
retrieves all results but I want to do something like...
const query = 'SELECT name from rick_morty where instigator = ["Rick", "Morty", "Beth"]';
to retrieve all list of names associated with that array of instigators.
Is this possible?? Did I just create my table in an improper way?
Is this possible??
Yes. See #8 here.
"Filter data on a column of a user-defined type. Create an index and then run a conditional query. In Cassandra 2.1.x, you need to list all components of the name column in the WHERE clause."
This should work:
SELECT name from rick_morty where instigator = { 'Rick', 'Morty', 'Beth'};
The following query should work,
SELECT name from rick_morty where instigator contains 'Rick' AND contains 'Morty';
But, This may not be an efficient/proper way to implement as Sets are meant to be used to store/get a set of data for a given primary key.
So, I would recommend you to re-design the data model by denormolise the query into a an additional table in case if this requirement is one of your primary use case.

Insert into view

In Oracle I create a view using a 'union all' as below
create view TESTVIEW as
select column1 from TABLE1
union all
select column1 from TABLE2;
If I want to insert into this view I get
SQL Error: ORA-01732: data manipulation operation not legal on this view
Is there any way around this if I know I want to insert in TABLE1?
Yes, you should be able to create an INSTEAD OF INSERT trigger on the view.
You'll need to write the trigger to insert the row in the base table, and it will run "instead of" the original insert.
Please refer http://docs.oracle.com/cd/E17952_01/refman-5.1-en/view-updatability.html to understand which views are directly updatable. For views which are not directly updatable use "Instead of" trigger as suggested in the above post.

How to update particular column dynamically

I'm using the following table
book_Catalog contains
book name,
book_id,
book_title,
created_by(contains the detail which user created this entry),
updated_by(contains the detail which user updates this entry)
created_by column uses the system date
Please give an idea
what query should be used to fill the update_by column??
Thanks in advance,
Ashmitha
You can use something like
CREATE PROCEDURE UPDATE_BOOK_CATALOG_UPDATED_BY
(pin_BOOK_ID BOOK_CATALOG.UPDATED_BY%TYPE)
IS
BEGIN
UPDATE BOOK_CATALOG
SET UPDATED_BY = UID
WHERE BOOK_ID = pin_BOOK_ID;
END UPDATE_BOOK_CATALOG_UPDATED_BY;
Share and enjoy.
You can use AFTER UPDATE TRIGGER to do this.
CREATE OR REPLACE TRIGGER My_Trigger
AFTER INSERT
ON TABLE
FOR EACH ROW
DECLARE
BEGIN
END;/

Is it possible to select a field from a table, but override it with a new value in the select statement?

I am creating a view like this:
Let's say I originally have this:
select * from mydb.mytable
mydb.mytable has a field called FirstName, but I want to transform its value in the select statement. Conceptually, I want to do this:
select *, upper(firstname) firstname from mydb.mytable
The problem is that * is already returning FirstName, so adding another column of the same name to the select breaks the SQL. To get it to work, I have to list each field like this:
select upper(firstname) firstname, lastname, city, state, zip
This is just one example, but the table I really want to use this with has 30+ columns. I don't like the idea of having to list out each column because adding a new field to the table means I have to modify the SQL (ordinal field position doesn't matter).
Well, that's the way SQL is designed, it's not a specific Teradata problem.
You want something like "select * but firstname" and no DBMS has implemented such a syntax.
Btw, one of (my) basic SQL rules is: never write "SELECT *" :-)
As dnoeth says, that's just how SQL works. Also, I'd reinforce his comment about never using select *, especially in a view.
To address concerns like this, I keep the table and view DDL together in code. Whenever you change the table definition, you change the view definition at the same time. That way, whenever you add or remove columns from your table (your stated concern), your view always remains current.

SQLite Query to Insert a record If not exists

I want to insert a record into a sqlite table if its actually not inserted.
Let's say it has three fields pk, name, address
I want to INSERT new record with name if that name not added preveously.
Can we do with this in a single Query. Seems like its slightly different from SQL Queries sometimes.
Yes, you can do that with a single query.
INSERT ON CONFLICT IGNORE should help you: http://www.sqlite.org/lang_conflict.html
Put a unique key on the name, this will create a conflict when you try inserting a record if the name already exists.
The default is ABORT, so without the IGNORE, the statement will return an error. If you don't want that, use IGNORE.
If you can't make use of a UNIQUE INDEX in combination with INSERT INTO or INSERT OR IGNORE INTO, you could write a query like this;
INSERT INTO table (column)
SELECT value
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE column = value)

Resources