How to retrieve table attributes in PostgreSQL? [duplicate] - sql

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

Related

Schema.Table in Postgres [duplicate]

This question already has answers here:
Cannot simply use PostgreSQL table name ("relation does not exist")
(18 answers)
I keep getting the error "relation [TABLE] does not exist"
(1 answer)
PostgreSQL "Column does not exist" but it actually does
(6 answers)
Omitting the double quote to do query on PostgreSQL
(5 answers)
Closed 2 years ago.
In Postgres, when I run any query using only the table name, I receive the error below:
ERROR: relation "transactions" does not exist
LINE 2: SELECT * FROM TRANSACTIONS
^
SQL state: 42P01
Character: 16
To get around that I need to use "schema.table" format - which makes the queries very long and clunky.
SELECT * FROM public."TRANSACTIONS"
I only have 1 schema - public. I have already tried to set the search_path to public but it doesn't help. Any suggestion?
You can set search path:
SET search_path TO public;
If it doesn't work check what is your search path after setting by:
SHOW search_path;
See documentation: https://www.postgresql.org/docs/current/ddl-schemas.html#DDL-SCHEMAS-PATH
Also note that double quoting object names in PostgreSQL matters. Maybe your search_path is correct but table was created as double quoted "TRANSACTIONS". PostgreSQL converts only unquoted names to lowercase (in all statements), so if you type SELECT FROM TRANSACTIONS it will become SELECT FROM transactions which will correctly yield error that transactions relation doesn't exist (only TRANSACTIONS does). You can check your table name as seen by PostgreSQL by running \dt - display tables (that will also prove your search_path is set correctly).
TLDR; you don't want to double quote anything unless you have good reason for that.
See documentation on quoting here: https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Looks like you are having trouble with double quoting identifiers ("), which should be avoided if at all possible. If an identifier is double quoted it MUST ALWAYS be double quoted. Thus the the following statements are not the same: Select * from TRANSACTIONS; and Select * from "TRANSACTIONS";
Since public."TRANSACTIONS" works for you try double quoting without the schema:
select * from "TRANSACTIONS";
If that works then make sure to always double quote. Or better, before too far into it, rename it eliminating double quotes.

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

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;)

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;

Specify name of table types in SQL Server [duplicate]

This question already has an answer here:
SQL Server create User-Defined Table Types with schema not working properly
(1 answer)
Closed 9 years ago.
Create a new database in MS SQL Server 2008 R2 and then create a new table type with the following command
CREATE TYPE typeMyType AS TABLE (ID INT)
Now execute the following query
SELECT OBJECT_NAME (object_id) AS ObjectName, *
FROM sys.indexes
WHERE index_id <= 1
ORDER BY ObjectName
This will show you that an object of type HEAP was created which is fine as the data for typeMyType has to be stored somewhere.
But the object is called TT_typeMyType_01142BA1 in my case.
Question:
Why isn't it called typeMyType and how can I overwrite this obviously server generated name?
The table type is listed in several places that can be looked at via the following system tables (sys.sysobjects, sys.indexes, sys.table_types and sys.types).
I can understand that table_types is a subset of types. Therefore, those two places are where to look for my table type AssociativeArray in my AdventureWorks2012 database.
The question I have is why is it looking like a table in sysobjects and sysindexes?
We have not defined any use for the variable yet. However, it looks like a table. It must be how the engine defines meta data for future use.
One take away, does the information in sysobjects and sysindexes get updated a run-time when we declare a variable of type AssociativeArray?
Also, what happens when two SPIDS create the same variable at the same time with different data being inserted?
That is a in-depth engine question that maybe a someone from Microsoft CAT team might know off the top of their head.
I guess I could do some research to find out.
Enclosed is a link stating that table variables use tempdb. That is what I know is a fact. Again, I wonder if the sys.objects and sys.indexes get updated or just place holders.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html

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