Creating reference with tables - postgresql

I have two tables that already have data in them and i would like to create reference via postgressql OR SQL shell. The reference should be like this:
[IMG]http://i.imgur.com/SMUEo4r.png[/IMG]
Can anyone tell me how to do that?

alter table statusi
add constraint fk_darbinieki_statusi
foreign key (id_darbinieks) references darbinieki (id_darbinieks);
Note that I used unquoted identifiers which are case insensitive.
Given your screenshot it might be that you created quoted identifiers using double quotes which are case sensitive. "ID_statusi" is a different name than ID_statusi. So maybe you need to use double quotes when running the above statement. In general, using double quotes for identifiers is not such a good idea. It creates more trouble than it's worth.
More details in the manual:
defining foreign keys:http://www.postgresql.org/docs/current/static/ddl-constraints.html#DDL-CONSTRAINTS-FK
Rules for identifiers: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
ALTER TABLE: http://www.postgresql.org/docs/current/static/sql-altertable.html

Related

nlog didnt insert database when table name uppercase

This works fine;
<targets>
<target name="database" xsi:type="Database"
dbProvider="Npgsql.NpgsqlConnection, Npgsql"
connectionString="User ID=postgres;Password=xxx;Host=192.xx;Port=5432;Database=xxx;">
<!--Pooling=true;-->
<commandText>
insert into systemlogs(...;
</commandText>
But when changed to table name as
"SystemLogs"
(same done in database as well) it throws exception;
"couldnt find table name "systemlogs"
which make sense there isnt but why nlog dont realize updated table name?
In PostgreSQL all quoted identifiers (e.g. table and column names) are case sensitive:
See: Are PostgreSQL column names case-sensitive?
So NLog can't find them is you use quotes and the wrong casing.
So don't use quotes or use the correct casing
If you specified the table name as "SystemLogs" inside double quotes then you will need to use it that way also:
insert into "SystemLogs" ...
In Postgresql quoted identifiers retain the case they are quoted with and need to be referred to the same way. If you create as unquoted name SystemLogs then it will be folded to lower case. See below for more detail:
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

table naming and organizing of tables in postgresql

I'm trying to organize my PosgtresSql tables according to the application components they correspond to. For example, tables related to 'story' such as 'story_contents', 'story_comments', 'story_layout': would it best to keep a simple '' naming convention as presented? Would there be any drawback to using a '.' instead of ''? ... or is there a best practice that I'm completely overlooking?
Short answer:
Sure, if you place the entire database/table/column name reference in quotes
Long answer:
In Postgres, and most other databases, the dot is used to separate database name from table name, and table name from column name. For example, if you had a database called MyDB, with a table called MyTable and column in that table MyCol, then you could write the following SELECT statement:
SELECT MyDB.MyTable.MyCol
FROM MyDB.MyTable
However, if your database, table, and/or column names themselves had dots in them, then doing a SELECT might not work. In this case, I believe you can escape the fully qualified name (or portion) with quotes. So, if you had a column called MyCol.Col1, you could do the following:
SELECT "MyDB.MyTable.MyCol.Col1"
FROM MyDB.MyTable
The comment by #vector seems to be pointing in the right direction (no pun intended), and you should lean towards using underscores or some other character to separate out your schema names, rather than using a dot.

INSERT with alias name is not working on PostgreSQL

SQL Query on PostgreSQL:
insert into TOKEPOOLAMT (TOKEPOOLAMT.TOKEPOOLAMTID,TOKEPOOLAMT.TOKERULEID)
values (151, 176);
Giving error:
com.edb.util.PSQLException:
ERROR: column "tokepoolamt" of relation "tokepoolamt" does not exist
But:
insert into TOKEPOOLAMT (TOKEPOOLAMTID,TOKERULEID) values (151, 176);
is working fine.
Can anybody explain why alias name with column in insert statement not working?
There are no aliases involved here. Your error is that column names in the column list of an INSERT command cannot be table-qualified. #pozs already provided the fitting quote from the manual in his comment.
I don't think it's an issue of case. I tried with both the cases.
That's missing the point. In Postgres, identifiers are folded to lower case unless double-quoted. If you double-quoted a name at creation time you preserved a case sensitive spelling and need to double-quote for the rest of the object's life - unless it was a legal, lower-case name to begin with, then quoting won't make a difference. Details:
Are PostgreSQL column names case-sensitive?

Cannot create a database table named 'user' in PostgreSQL

It seems PostgreSQL does not allow to create a database table named 'user'. But MySQL will allow to create such a table.
Is that because it is a key word? But Hibernate cannot identify any issue (even if we set the PostgreSQLDialect).
user is a reserved word and it's usually not a good idea use reserved words for identifiers (tables, columns).
If you insist on doing that you have to put the table name in double quotes:
create table "user" (...);
But then you always need to use double quotes when referencing the table. Additionally the table name is then case-sensitive. "user" is a different table name than "User".
If you want to save yourself a lot of trouble use a different name. users, user_account, ...
More details on quoted identifiers can be found in the manual: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
It is possible to specify tablename with JPA with next syntax:
#Table(name="\"user\"")
We had this same issue time ago, and we just changed the table name from user to app_user. Due to the use of Hibernate/JPA. We thought it would be easier this way.
Hope this little fix will help someone else.
You can create a table user in a schema other than public.
The example:
CREATE SCHEMA my_schema;
CREATE TABLE my_schema.user(...);
Trailing underscore
The SQL standard explicitly promises to never use a trailing underscore in any keyword or reserved word.
So, to avoid conflicts with any of the over a thousand keywords and reserved words used by various database engines, I name all my database identifiers with a trailing underscore. (Yes, really, over a thousand keywords reserved — I counted them.)
Change this:
CREATE TABLE user ( … ) ;
… to this:
CREATE TABLE user_ ( … ) ;
I do this as a habit for all database names: schemas, tables, columns, indexes, etc.
As an extra benefit, this practice makes quite clear in documentation, email, and such when referring to a programming language variable named user versus the database column user_. Anything with a trailing underscore is obviously from the database side.

The relation does not exist in postgresql

I am have big problem with Postgresql.
I'm trying to insert record to postgres.
But i had error:
The relation "member_orderinfo2" does not exist. But this table had exist when i'm insert record.
This is my query to insert
insert into "member_orderinfo2"(order_id,slim_code,order_date,customer_order_number,ship_date,deliver_company,deliver_number,deliver_address,product_code,product_name,amount,unit_price,total_cost,order_customer_name,ship_status) values
( '30059078','4170906','20131118','15754727-03直送','0','メーカー直送','','京都府綾部市味方町1京セラコミュニケーションシステム','0996381','RD151-50SN(ジユウリヨウダ','5','5120.00','25600','梶野様','入荷待' )
What is error ???
It looks like case sensitivity issue.
You used a double quotes for table name - "member_orderinfo2" - it means so table have to be named exactly member_orderinfo2 with only lower chars. Double quotes are used for case sensitive identifiers. Usually better don't use it when it is possible - are necessary when used identifier is keyword - so don't use case sensitive identifiers and don't use keywords as identifiers.