SQL Examples

From Null-pointer

Jump to: navigation, search

Contents

CREATE

CREATE USER 'portal'@'localhost' IDENTIFIED BY '[password]';
GRANT ALL PRIVILEGES ON *.* TO 'portal'@'localhost' WITH GRANT OPTION;
CREATE DATABASE Portal;

All domains [1]

CREATE USER 'portal'@'%' IDENTIFIED BY '[password]';

table collate

CREATE  TABLE IF NOT EXISTS `identifier`
(
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `user_id` INT UNSIGNED NOT NULL ,
  `identifier` VARCHAR(450) NOT NULL ,
  `identifier_type` VARCHAR(45) NULL ,
  `name` VARCHAR(100) NULL ,
  `email` VARCHAR(200) NULL ,
  `created_application_id` INT NULL ,
  `created_at` DATETIME NULL ,
  `modified_application_id` INT NULL ,
  `modified_at` DATETIME NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_identifier_user1` (`user_id` ASC) ,
  UNIQUE INDEX `identifier_UNIQUE` (`identifier` ASC) ,
  CONSTRAINT `fk_identifier_user1`
    FOREIGN KEY (`user_id` )
    REFERENCES `user` (`id` )
    )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
COLLATE = latin1_swedish_ci;


limit offset

MySQL

SELECT *
 FROM table_name
 LIMIT 5
 OFFSET 5

limit to five results, start from the fifth result.

MS-SQL

SELECT TOP 5 *
 FROM table_name

no offset

DB2

SELECT *
 FROM table_name
 FETCH FIRST 10 ROWS ONLY
[2]
SELECT * FROM (
   SELECT row_number() OVER (ORDER BY id_col ) as row_number, id_col, other_col FROM table_name 
 ) as t
 WHERE row_number BETWEEN 100 AND 120
[3]

Use MYSQL compatibility mode

group order

SELECT team
 FROM results 
 GROUP BY team
 ORDER BY SUM( CASE WHEN points IS NULL THEN 0 ELSE points END ) DESC

dates

MySQL

INSERT INTO table_name ( DateField ) VALUES ( CURDATE() )

MS-SQL

INSERT INTO table_name ( DateField ) VALUES ( getDate() )

Truncate

MySQL

truncate table_name

DB2

alter table schema.table_name activate not logged initially with empty table
[4]

Export to CSV

The FILE privilege[5]

GRANT FILE ON 'database'.* to 'user'@'host'

This command does not seem to work.

SELECT INTO [6]
SELECT id INTO OUTFILE '/temp/uid.csv' 
FIELDS TERMINATED BY ','
FROM user
order by rand()
limit 10;

Last id

MS-SQL

SELECT @@IDENTITIY

MySQL

LAST_INSERT_ID[7]

SELECT LAST_INSERT_ID();

User-Defined Variables

SET @lastId=LAST_INSERT_ID();
 INSERT INTO myTable ( 'ref' ) VALUES ( @lastId )

Order by random

DB2

SELECT column, RAND() as IDX 
FROM table 
ORDER BY IDX
[8]

References

  1. "Adding User Accounts" MySQL 5.1 Manual. Retrieved 12 Jan 2011.
  2. Craig S. Mullins. (May 2002). "The 'Top Ten' Problem" Retrieved 12 Jan 2011.
  3. Marus_A. (21 Apr 2008). "Forum:Equivalent of LIMIT and OFFSET in DB2" Retrieved 8 Aug 2011.
  4. David Yahalom. (7 Jan 2007). "Truncate in DB2, DB2 CLP tricks, simple DB2 tracing" Life as a DBA. Retrieved 5 Apr 2011.
  5. http://dev.mysql.com/doc/refman/5.1/en/privileges-provided.html#priv_file
  6. http://forums.mysql.com/read.php?79,11324,13062#msg-13062M
  7. http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id
  8. Pete Freitag. (September 14, 2005). "SQL to Select a random row from a database table" Pete Freitag's ColdFusion, Java and Web Development Blog. Retrieved 6 May 2011.
Personal tools