When query a Derby database, I find out that for some tables I have to double quote the column name and use table name to qualify the column name, but for some other tables I don’t need to. What happens to these tables and how can I make all tables the same and can query them without the double quote and the table name qualifier? I am using NetBeans IDE’s Sql Command tool. Below are those different queries.
Set schema app;
Select * from table1 where table1.”state” = ‘CA’;
Select * from table2 where state = ‘CA’;
Putting a tablename or column name in quotes, sometimes referred to by the jargon-y term "delimited identifiers" does two things:
Allows you to use words that are otherwise reserved keywords (e.g., naming a column "WHERE" or "SELECT")
Instructs the database system to process the name using case sensitive rules, rather than case-insensitive rules
So if you originally created "table3" with a CREATE TABLE statement that specified "table3" in double quotes like this, then you will forever after have to refer to it with the name in double quotes.
select * from table3
will be automatically processed by the database as if it was
select * from TABLE3
while
select * from "table3"
will successfully match the table you created as create table "table3"
See: http://db.apache.org/derby/docs/10.9/ref/crefsqlj34834.html
Related
Result is fetching zero records.
enter image description here
Are you connected to (logged into) the database in which that table exists?
SQL such as this would be a starting point
select * from _v_relation_column
where UPPER(name) = 'MY_TABLE'
and UPPER(schema) = 'MY_SCHEMA'
order by attnum;
The restriction on schema may not be needed (if you aren't using multiple schemas).
(A) Sometimes we need the column names as headers
select * from <table> limit 0
(B) If you want definition of a table ie. showing all columns along with their data types etc (assuming you have nzsql cli)
nzsql <db> -c "\d <table>"
In Dataverse if I create a table e.g 'foo' MS Dynamics automatically pre-fixes my tables with a few characters to denote that they are my custom tables e.g. abcd_foo.
What's more, if connect to the database with SSMS I can query the table as follows:
Select * from abcd_foo;
When you create a Choice column in foo, the table contains an integer value that references the lookup value - not the lookup value itself.
The picture below shows the choice values returned from a query:
Select
abcd_accesslevel -- my choice column
from abcd_foo;
What is the name of the SQL Table in which MS Dynamics stores these 'choice values' which it then internally joins up to my tables foo choice column, acceslevel?
e.g. if 'abcd_accesslevel' is a Choice column, how would I complete this query:
Select f.abcd_accesslevel,
c.label, -- The choice label
c.value, -- The choice value
f.*
from abcd_foo f
left join some_internal_choice_table c on c.someid = f.abcd_accesslevel
-- There may be a more complex WHERE clause to separate out the label for my table from the other choice values if all choices are stored in a common table.
In Dataverse there are 2 optionsets, local and global.
In your case I believe your abcd_accesslevel is local optionset.
local optionset is stored in metadata Entitydefinitions.
I never retrieved this via sql but I user API to fetch such data.
example API call
https://XYZ.crm.dynamics.com/api/data/v9.2/EntityDefinitions(LogicalName=’incident’)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?&$expand=OptionSet
I tried this sql query and worked for me. (I tried on my account entity)
SELECT *
FROM stringmap
WHERE objecttypecode = 'abcd_foo'
AND attributename = 'abcd_accesslevel ';
If I do SELECT name, ISNULL(license, 0) FROM table; I get a resulting table with the license column having no name ("Column1" on my xml), how do I give it a name in the query so it makes my life easier when manipulating it on my application? I tried finding it but could only find how to rename the column on the table itself, instead of renaming the column on the table generated by the SELECT.
To simplify: ISNULL removes the name of the column in the generated by my SELECT and I want to bring it back.
Just use an alias:
SELECT name, ISNULL(license, 0) AS license FROM table;
I am using Oracle tool (Oracle SQL Developer - Version 19.2.1.247).
When i create new table in Oracle Db then it change all column name in uppercase i.e.(CUSTOMERID), but i want to keep column name i.e.(CustomerId).I am looking for solutions how to resolve this.
I did some try to change formatting of editor as well code setting in Tools -> Preference but not found any proper things.
Thanks in advance.
You should avoid doing that. Every object ( table, column, index, sequence, trigger... ) is stored in uppercase in the Oracle dictionary.
However, if you want to store the name in lowercase, you must use double quotation
SQL> create table test ( c1 number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
C1
SQL> create table test ( "c1" number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
c1
Keep in mind that if you store the value in lowercase, any search or program that uses the dictionary will have to take this in consideration. That is why I believe it is not a good practice.
Oracle has a default functionality where it will convert all unquoted table/column identifiers to upper case, therefore add double quotes around the names should resolve your issue.
I have a code here in the screenshot. At the end of the code you see a "a"
When i try to remove the "a" and run the code, it fails but it works with the "a"
what is the significance of this ?
Edit: Question was originally tagged MySQL. However, the explanation below should still apply for all the major RDBMS.
It is an Aliasname for the Derived Table. A Derived table is basically a sub-select query. In MySQL, every Derived Table should have its own Alias, so that outer Select queries can refer to the columns/expressions from the Derived Table. Without a table name/alias, MySQL cannot determine the origin of a column value unambiguously.
From Docs:
The [AS] tbl_name clause is mandatory because every table in a FROM
clause must have a name. Any columns in the derived table must have
unique names.