Case problem of table name and field name in PostgreSQL

The table cannot be dropped if there are views that depend on it

 

When learning hibernate, the database used PostgreSQL, and the first lesson was wrong. User entity mapping cannot export tables without life and death. Always remind this sentence:

         ERROR: syntax error at or near "User"。

 

        Later, I found that if the table name is set to t_user, it is enough. Can't PostgreSQL use uppercase? Try T_User again, no error is reported, go to the database to see, eh? Still t_user! Uppercase is automatically changed to lowercase. PostgreSQL doesn't recognize case? Creating a User table with the pgAdmin graphical interface is perfectly fine. After that, I tested the fields in the table and found that the same problem is also encountered. It always prompts ERROR:  syntax error at or near "…"

        when I suddenly found out when I looked at the SQL window of pgAdmin ( it is really important to observe carefully in many cases ), Double quotation marks were added to the capitalized places
         

        . Suddenly, I got enlightened. It is not that the case is not recognized. The mystery turned out to be in the double quotation marks.

       First use SQL statement to try to create a table whose name is all capital letters, remember to add double quotation marks to the table name, run without error, success!
       There are two solutions in the Hibernate example. One is to use lowercase for the table name, and the other is to add double quotation marks in uppercase. Of course, you will ask at this time, two double quotation marks will not work, use escape symbols.


      In the JPA example:

         



Summary:

  1. PostgreSQL is case-sensitive for table and field names. It can be created normally in the graphical interface. When using SQL statements, you need to add double quotes. If jdbc queries, etc., remember to use escape symbols.
  2. PostgreSQL is case insensitive in SQL statements

select ID from t_user  和 select id from t_user


都会从t_user这个表中查询id这个字段。如果要查询大写字母的字段,同样要加上双引号:select "ID" from t_user

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326117626&siteId=291194637