To understand a sentence about the case in SQL/DDL -queries

删除回忆录丶 提交于 2019-12-11 07:46:52

问题


This question is based on this answer.

What does the following sentence mean?

Finally, don't use mixed case identifiers. Do everything lowercase, so you don't have to quote them.

Does it mean that I should change the commands such as CREATE TABLE, USER_ID and NOT NULL to lowercase? - It cannot because it would be against common naming conventions.


回答1:


No, I think the gentleman referred to using all lowercase identifiers, e.g. table, column etc. names. This should have no impact on the commands like CREATE TABLE etc. that you use.

Marc




回答2:


In PostgreSQL, an unquoted identifier is converted into lowercase:

CREATE TABLE "MYTABLE" (id INT NOT NULL);

CREATE TABLE "mytable" (id INT NOT NULL);

INSERT
INTO    "MYTABLE"
VALUES  (1);

INSERT
INTO    "mytable"
VALUES  (2);

SELECT  *
FROM    mytable;

---
  2

SELECT  *
FROM    MYTABLE;

---
  2

Both queries will return 2, since they are issued against "mytable", not "MYTABLE".

To return 1 (i. e. issue a query against "MYTABLE", not "mytable"), you need to quote it:

SELECT  *
FROM    "MYTABLE";

---
  1

CREATE TABLE etc. are not identifiers, they are reserved words.

You can use them in any case you like.




回答3:


I have no idea why he said that. In SQL Server, at least, the case of an identifier doesn't matter. Maybe it does in other DBMS systems?



来源:https://stackoverflow.com/questions/1199868/to-understand-a-sentence-about-the-case-in-sql-ddl-queries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!