How to reset Postgres’ primary key sequence when it falls out of sync?

— Login to psql and run the following — What is the result? SELECT MAX(id) FROM your_table; — Then run… — This should be higher than the last result. SELECT nextval(‘your_table_id_seq’); — If it’s not higher… run this set the sequence last to your highest id. — (wise to run a quick pg_dump first…) BEGIN; … Read more

Solutions for INSERT OR UPDATE on SQL Server

don’t forget about transactions. Performance is good, but simple (IF EXISTS..) approach is very dangerous. When multiple threads will try to perform Insert-or-update you can easily get primary key violation. Solutions provided by @Beau Crawford & @Esteban show general idea but error-prone. To avoid deadlocks and PK violations you can use something like this: begin … Read more

When to use single quotes, double quotes, and backticks in MySQL

Backticks are to be used for table and column identifiers, but are only necessary when the identifier is a MySQL reserved keyword, or when the identifier contains whitespace characters or characters beyond a limited set (see below) It is often recommended to avoid using reserved keywords as column or table identifiers when possible, avoiding the … Read more

SQL injection that gets around mysql_real_escape_string()

The short answer is yes, yes there is a way to get around mysql_real_escape_string(). #For Very OBSCURE EDGE CASES!!! The long answer isn’t so easy. It’s based off an attack demonstrated here. The Attack So, let’s start off by showing the attack… mysql_query(‘SET NAMES gbk’); $var = mysql_real_escape_string(“\xbf\x27 OR 1=1 /*”); mysql_query(“SELECT * FROM test … Read more

‘IF’ in ‘SELECT’ statement – choose output value based on column values

SELECT id, IF(type=”P”, amount, amount * -1) as amount FROM report See http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html. Additionally, you could handle when the condition is null. In the case of a null amount: SELECT id, IF(type=”P”, IFNULL(amount,0), IFNULL(amount,0) * -1) as amount FROM report The part IFNULL(amount,0) means when amount is not null return amount else return 0.

How do I see what character set a MySQL database / table / column is?

Here’s how I’d do it – For Schemas (or Databases – they are synonyms): SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = “schemaname”; For Tables: SELECT CCSA.character_set_name FROM information_schema.`TABLES` T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = “schemaname” AND T.table_name = “tablename”; For Columns: SELECT character_set_name FROM information_schema.`COLUMNS` WHERE table_schema = “schemaname” AND table_name … Read more

Rename column SQL Server 2008

Use sp_rename EXEC sp_RENAME ‘TableName.OldColumnName’ , ‘NewColumnName’, ‘COLUMN’ See: SQL SERVER – How to Rename a Column Name or Table Name Documentation: sp_rename (Transact-SQL) For your case it would be: EXEC sp_RENAME ‘table_name.old_name’, ‘new_name’, ‘COLUMN’ Remember to use single quotes to enclose your values.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)