Using SQL Standards? Database SQL comparition


Published on

Interesting to learn about different databases. Do they actually follow SQL standards ?
- Oracle
- PostgreSQL

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Using SQL Standards? Database SQL comparition

  1. 1. Using SQL Standards SQL COMPARISON Denish Patel Database Administrator OmniTi Computer Consulting Inc Friday, April 2, 2010
  2. 2. SQL STANDARDS SQL92 SQL99 SQL 2003 SQL 2008 Friday, April 2, 2010
  3. 3. DATABASES Oracle 10g Express Edition PostgreSQL 8.3.3 MySQL 5.0.18 Friday, April 2, 2010
  4. 4. VIEWS SQL92:Updatable views can not be derived from more than one base table SQL99: As long as it “make sense” SQL:2008: As long as the update-operation translates into unambiguous change Friday, April 2, 2010
  5. 5. VIEWS Oracle Conforms at least SQL92 Break rules by not allowing PostgreSQL updatable views but offer non- standard ‘rule’ system MySQL Conforms at least SQL92 Friday, April 2, 2010
  6. 6. JOIN TYPES Oracle PostgreSQL MySQL Natural Join Yes Yes Yes Using-clause Yes Yes Yes Full Join Yes Yes No Explicit(Cross Yes Yes Yes Join) Friday, April 2, 2010
  7. 7. ORDER BY SQL Standard : Relations are unordered but result sets may be ordered when returned to the user using cursor DECLARE cursorname CURSOR FOR SELECT ...FROM ....WHERE... ORDER BY column_name1,column_name2,.... Doesn’t specify NULLs order NULLs are equally ordered and can be first or last ...ORDER BY...NULLS FIRST ...ORDER BY ...NULLS LAST Friday, April 2, 2010
  8. 8. ORDER BY - Cursor allows ORDER BY Oracle - By Default NULLs considered higher - ORDER BY ... NULLS LAST - Cursor allows ORDER BY PostgreSQL - By Default NULLs considered higher - ORDER BY ... NULLS LAST Cursor allows ORDER BY - By Default NULLs considered lower MySQL - minus(-) character used to change from ASC to DESC or Vice-Versa Friday, April 2, 2010
  9. 9. SIMPLE LIMIT FETCH FIRST (Since SQL:2008) SELECT ...FROM ..WHERE...ORDER BY...FETCH FIRST n ROW(s) ONLY Window Function(Since SQL:2003) SELECT * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY key ASC) as rownumber, columns FROM tablename ) as foo WHERE rownumber <=n Cursor DECLARE , OPEN , FETCH , CLOSE Friday, April 2, 2010
  10. 10. SIMPLE LIMIT -Supports ROW_NUMBER but not FEATCH Oracle FIRST - Doesn’t support ROW_NUMBER() or FETCH FIRST PostgreSQL - Alternative LIMIT (*SELECT ..FOR UPDATE) - Doesn’t support ROW_NUMBER() or FETCH FIRST MySQL - Alternative LIMIT Friday, April 2, 2010
  11. 11. MULTI- INSERT Not Supported Oracle insert into tablename values (0,‘foo’); insert into tablename values (1,‘bar’); Supported PostgreSQL insert into tablename values (0,‘foo’), (1,‘bar’); Supported MySQL insert into tablename values (0,‘foo’), (1,‘bar’); Friday, April 2, 2010
  12. 12. TYPES- BOOLEAN BOOLEAN type is Optional!! TRUE, FALSE, UNKNOWN or NULL UNKNOWN can be NULL! TRUE > FALSE Friday, April 2, 2010
  13. 13. TYPES- BOOLEAN Not Supported Oracle Alternative NUMBER(1) Do not forget to add constraint Follow Standards PostgreSQL Does not accept UNKNOWN Nonconforming alternative TINYINT(1) MySQL Accept values greater than 1 Friday, April 2, 2010
  14. 14. TYPES- CHAR CHAR-length Return error if the inserted sting is too long except spaces Pad with spaces if string is shorter Casting or Compare (VARCHAR) - Pad with trailing spaces Friday, April 2, 2010
  15. 15. TYPES- CHAR Generally Follows Standard Oracle Doesn’t remove trailing spaces when exceeds CHAR length Generally Follows Standard PostgreSQL Truncate trailing spaces i.e CHARACTER_LENGTH Breaks the rules MySQL Silently inserting truncated string Truncates all trailing spaces Friday, April 2, 2010
  16. 16. TYPES- TIMESTAMP second - 6 fractional digits TIMESTAMP ‘2009-03-05 16:10:18.299411’ TIMESTAMP WITH TIME ZONE TIMESTAMP ‘2009-03-05 16:10:18.299411+05:00’ Friday, April 2, 2010
  17. 17. TYPES- TIMESTAMP Oracle Follows standard Follows Standards with exception PostgreSQL TIMESTAMP & TIMESTAMPTZ Illogical Standard is Standard! Not Supported Magical TIMESTAMP datatype MySQL No Sanity checks on the date ‘2009-03-32 00:00:00’ Friday, April 2, 2010
  18. 18. UNIQUE CONSTRAINT Unique Values or NULL NULL <> NULL Nulls allowed feature create table test(a number,b number, unique (a,b)); insert into test values (1,NULL); insert into test values(1,NULL); Friday, April 2, 2010
  19. 19. UNIQUE CONSTRAINT Oracle Follows standard without Nulls allowed PostgreSQL Follows standard with Nulls Allowed MySQL Follows standard with Nulls Allowed Friday, April 2, 2010
  20. 20. AUTO KEY GENERATED .. [ALWAYS /BY DEFAULT] AS IDENTITY ALWAYS - user can not specify value BY DEFAULT - user can manually specify value Friday, April 2, 2010
  21. 21. AUTO KEY Does not support IDENTIFY attribute Oracle Sequence & Trigger Exception to implement ALWAYS Does not support IDENTIFY attribute PostgreSQL In-line SERIAL attribute Trigger to implement ALWAYS Does not support IDENTIFY attribute AUTO INCREMENT MySQL Never fails (!) Friday, April 2, 2010
  22. 22. TRUNCATE TABLE Delete all rows without causing triggered action Need more clarification on TRUNCATE TABLE should be allowed in transaction Immediate Commit Friday, April 2, 2010
  23. 23. TRUNCATE TABLE Follows the standard Oracle Implicit commit Follows the standard PostgreSQL Allowed with other transaction Does not implicit commit Doesn’t ALWAYS follow standard Slow and Trigger Invoking MySQL For Innodb, Allowed with other transaction & does implicit commit Friday, April 2, 2010
  24. 24. MISC CL- interface DB Name List Schema Default port Version Name List Tables sqlplus select name from v$database; select username from all_users; Oracle 1521 select * from v$version; select * from tab; dn or select schemaname from psql l or psql --list information_schema.schemata; PostgreSQL 5432 select version(); dt or select tablename from pg_tables; mysql show databases; Does not support schema MySQL 3306 select version(); show tables; Friday, April 2, 2010
  25. 25. RESOURCES SQL Standards Documentations/Books: SQL99: SQL2008: (File 5CD2-02-Foundation-2006-01.pdf) Oracle Documentation: b14200/toc.htm PostgreSQL Documentation: commands.html MySQL documentaion: Friday, April 2, 2010
  26. 26. THANK YOU!! Friday, April 2, 2010