What is the Postgresql SQL query analogous to 'USE database_name' from MySQL? [duplicate] - sql

This question already has answers here:
How to switch databases in psql?
(15 answers)
Closed 3 years ago.
I have to execute a script from Java code which should create a database and then a table in that database. How do I do it in PostgreSQL?
CREATE DATABASE mydb
USE mydb // doesn't work for PostgreSQL
CREATE TABLE mytable (.....)
What SQL do we give for Step 2 in PosgreSQL?
Version : 9.5

Almost this question has been asked before (Difference Between Schema / Database in MySQL)
This has a reference to chapter 5.8 'Schemas': https://www.postgresql.org/docs/current/ddl-schemas.html
If your answer was 'how are schemas implemented under PostgreSQL', than this might be the correct answer. I think you cannot use use .., and you have to use mydb.mytable. (or set a search pattern SET search_path TO mydb,public;)

Related

How to retrieve table attributes in PostgreSQL? [duplicate]

This question already has answers here:
PostgreSQL "DESCRIBE TABLE"
(24 answers)
Closed 2 years ago.
So I'm very new to PostgreSQL (and SQL as a whole) and I'm trying to find a way to view all attributes of a table. For example, say you have a table called "movies", I would like to see what attributes it has (e.g. movie_name, release_year).
I'm sure there is a simple command for it, but I couldn't find it.
Thanks in advance for any help!
Assuming that by attributes you mean columns, a standard approach uses system view information_schema.columns:
select column_name, datatype
from information_schema.columns
where table_name = 'mytable'
From a PSQL command line, you can also use \d (which stands for describe):
# \d mytable

How to run Regex in MSaccess (If supported)? [duplicate]

This question already has answers here:
Regular Expressions in MS Access VBA?
(2 answers)
Closed 4 years ago.
This is my first post, please be nice.
I want to know if it is possible to run similar to an Oracle regexp_match/regexp_replace in MSaccess 2010.
The code I usually use is something like
select * from table
where regexp_match(name, '^foo$')
How do I do this in Access,
Yes I have tried google search, unfortunately I was unsuccessful.
Do I have to use VBA for it, if so how?
Thank you, much appreciated
MS Access does not have built-in regular expression support.
However, this query:
select t.*
from table t
where regexp_match(t.name, '^foo$')
Is better written as:
select t.*
from table t
where t.name = 'foo';
Equality is more efficient in any database.

Copy all columns without data but with dependencies in SQL [duplicate]

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 5 years ago.
So i'm trying to copy all data of table CarOrders into a new table, NewCarOrders. But i only want to copy columns and dependencies and not data. I don't know if it truly matters, but i'm using SQL Server Management Studio.
Here are two things that i tried, but none worked.
SELECT *
INTO NewCarOrders
FROM CarOrders
WHERE 0 = 1
The issue with above is, it is copying all the columns but it is not copying the dependencies/relationships.
I saw some posts on some of the forums regarding this, but the suggested solution was using SSMS Wizard. I want to do it using SQL (or T-SQL).
I'm new to SQL world, so i'm really sorry if it is a stupid or no brainer question. Let me know if i am missing some important information, i'll update it.
Try below query and check if this works for you
SELECT TOP 0 *
INTO NewCarOrders
FROM CarOrders
This will create NewCarOrders table with same structure as CarOrders table and no rows in NewCarOrders.
SELECT * FROM NewCarOrders -- Returns zero rows
Note : This will not copy constraints , only structure is copied.
For constraints do as below -
In SSMS right click on the table CarOrders, Script Table As > Create To > New Query Window.
Change the name in the generated script to NewCarOrders and execute.
Also change the constraints name in the generated script else it will throw error like There is already an object named 'xyz' in the database
You can use the like command:
CREATE TABLE NewCarOrders LIKE CarOrders;

SQLite delete with table alias [duplicate]

This question already has answers here:
Sqlite delete query error
(3 answers)
Closed 9 years ago.
I am trying to alias a table in SQLite, for example by the following command: (it is from the book i am reading"Database Management Systems by Ramakrishnan")
DELETE FROM Students S WHERE S.sid=12546
This code gives a syntax error. Without aliasing, the following code works:
DELETE FROM Students WHERE sid=12546
But, if i want to alias the table, what should i do? Can anyone help?
Thanks
The DELETE statement operates on a single table and does not use a table alias. so you will have to use your query as :
DELETE FROM Students WHERE sid=12546
Update:
SQLite apparently doesn't support joins with the delete statement, as you can see on the Syntax diagrams. In short in SQLite, one DELETE command deletes from only one table. So aliasing is of no use

sql server select from case db [duplicate]

This question already has answers here:
How to use a variable for the database name in T-SQL?
(4 answers)
Closed 8 years ago.
I have 2 db on one server.
I need to write procedure that does select basing on #db variable.
I know 2 possibilities for this:
I declare #SQL nvarchar(max) and generating my query in plain text. Then i do exec #SQL.
Bad variant imho.
I do 2 similar queries and use if #db='' 1st query else 2nd query. Another bad variant because it is code duplicate.
Question is - is there any way to do like this or similar: select * from #db.dbo.table?
Using "exec #SQL" isn't evil. If it gets the job done and you're not exposing yourself to any security risks then it may be the best way to go. Another option would be to consider using a real programming language like c# (or whatever your preference is) since they are better equipped to handle these sort of dynamic requirements.