I would like to know how to enforce collation on a Select Distinct statement or whether it is even necessary.
-- Load temp Table
INSERT INTO tblTEST(srtTxt)
SELECT DISTINCT [dbo].[tblbackup].[srtTxt] COLLATE Latin1_General_CS_AS FROM
[D:\APP_DATA\TESTDB.MDF].[dbo].[tblbackup]
If I am trying to load distinct strings into a new table, is COLLATION enforced by default?
I believe you only need to set the collation on the destination column. But watch for collation-conversion errors.
It appears I cannot set collation on the destination column but only on the select column. I get errors no matter what I try
Related
I'm trying to change all the USER-DEFINED columns to TEXT in a specific view using pgsql.
Is it possible to do that in a single alter table query ou do I need to check first what columns contain that dataype and then perform the dataype change one by one?
This is what I'm trying:
ALTER TABLE if exists "schemaName"."Table_A"
ALTER COLUMN (
select column_name
from information_schema.columns inf
where table_name = 'Table_A' and inf.data_type = 'USER-DEFINED')
TYPE TEXT;
I'm getting and error in the subquery start "("
You need to do this one by one. Generally speaking such DDL statements cannot work on several objects in one statement.
For ALTER TABLE, see: https://www.postgresql.org/docs/12/sql-altertable.html.
FOR ALTER VIEW, see: https://www.postgresql.org/docs/current/sql-alterview.html
I am currently migrating from MySQL to PostgreSQL in a Laravel application, and I noticed that when updating, the row goes to the end of the table (bottom).
In the application I know I can use ORDER BY to sort, but I am referring to the internal behavior of the database while performing the UPDATE action.
In Mysql, it remains in the same position it occupied before the update.
Is there any way to apply this function? Would it be a InnoDB feature? Using Navicat Premium 12.1 DBMS.
I think this is just an aesthetic factor, but even so I would like to learn how to carry out this "permanent ordination".
The database is in UTF-8 encoding and pt_BR.UTF8 collation and ctype.
Following is the table:
CREATE TABLE `properties` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`name` varchar(255),
`rental_price` decimal(10, 2),
`sale_price` decimal(10, 2)
);
Thank you all!
Part 1: Generally use ORDER BY
If you do not use the ORDER BY statement, both MySQL and PostgreSQL (and for that matter most relational DBMS systems) do not make any promises about the order of records.
You should refactor your application to use the ORDER BY statement. If you want your data set to be ordered by newest first, you could use something like:
SELECT * FROM yourtable ORDER BY id DESC;
SELECT * FROM yourtable ORDER BY creation_date DESC; -- if your table has such a column
Similarly, you can have oldest objects first by using one of the following:
SELECT * FROM yourtable ORDER BY id ASC;
SELECT * FROM yourtable ORDER BY creation_date ASC; -- if your table has such a column
Part 2: Looking into the mechanics
You added to your question a more detailed inquiry:
[...] I know I can use ORDER BY to sort, but I am referring to the internal behavior of the database while performing the UPDATE action.
There is multiple things that influence the sequence of database records displayed on your screen, when performing a query. In a real life application, it is not (practially) possible to predict this sequence.
I assume this is simply an effect of PostgreSQL creating a new record for the updated record as can be found here in the Updating a Row section. I suggest to not rely on this behvaiour in any of your applications.
I am working with SQL Server 2014. I have dynamically generated SQL which looks like this (simplified for brevity):
with CTE as
(
select field as [field], field as [Field]
from myTable
)
select [field], [Field]
from CTE
The above however results in this error:
The column 'Field' was specified multiple times for 'CTE'.
I would expect/want this to work because the 2 columns are in fact unique, accounting for case. Is there anyway we can ask SQL (via some SET option maybe) to treat them as unique?
My dynamically generated SQL is very complex and it's very difficult to identify such 'duplicates' and 'combine' them.
From a theoretical perspective, you could change the collation of your database to a case-sensitive option. Case-sensitive database/server collations will also consider the case sensitivity of aliases. Table column collations, and collating a column in a select will not.
Changing database/server collation will change a whole lot else though. It would be a very extreme change to fix an alias issue and I doubt it is a viable solution for you.
That said, if your dynamic SQL is able to see that the alias field already exists and use the capitalized alias Field for the next instance of the same column, I would think you could simply adjust that to be field1, field2 etc. You can always re-alias those to whatever you want in your outer/final select, they just need to be unique in the CTE query.
I have a column where I want to change the data type. I currently am using Redshift. I know I can use the alter table statement to change the datatype, but this would change the order of the columns.
Is there a way to change the datatype without changing the order of the column?
I would recommend creating a new table with the schema you want and copying it over from the old table using a insert into new_table (select * from old_table) statement (here you can also do any casting to the new data type), after which you can drop the old table and rename the new one:
drop table old_table;
alter table new_table rename to old_table;
Using ALTER TABLE table_name ALTER COLUMN column_name TYPE new_data_type will not change the order of the columns in your table.
Please note that this clause can only changes the size of a column defined as a VARCHAR data type.
There are also other limitations described in AWS documentation of ALTER TABLE
How to make a table column case insensitive in Firebird server?
I am using this code, but it's not working..
con.opne();
cmd = new FbCommand("ALTER TABLE asco ALTER COLUMN Final_Model VARCHAR(30) COLLATE Latin1_General_CS_AS NULL", con);
cmd.ExecuteNonQuery();
con.close();
Error:
ERROR:Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 43
VARCHAR
There are several problems with your statement. First of all, according to the Interbase 6.0 language reference (available from the documentation section of the Firebird site) section ALTER TABLE (page 28), the correct syntax is:
ALTER TABLE table <operation> [, <operation> ...];
<operation> = {...
| ALTER [COLUMN] column_name <alt_col_clause>
...}
<alt_col_clause> = {...
| TYPE new_col_datatype
...}
Notice that new_col_datatype is not further expanded upon (datatype is however on page 29). In other words the statement should at least be:
ALTER TABLE asco
ALTER COLUMN Final_Model
TYPE VARCHAR(30)
Note the TYPE between the column name and the datatype. The order of the BNF productions in the Language Reference could make you think that you can also alter the collation of a column, unfortunately you can't.
So if you want to change the collation, you will have to create a new column with the right collation, copy the content and then drop the old column (and rename the new column to the name of the old). See also this thread on the Firebird-support list.
And finally I want to point out that 1) Latin1_General_CS_AS is a case sensitive collation and not insensitive as stated in your question and 2) Firebird does not know the collation Latin1_General_CS_AS (the closest to what you need is probably the default collations WIN_PTBR or FR_FR_CI_AI, otherwise you can create one yourself using CREATE COLLATION).