Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

SQL Cheat

This document provides a SQL cheatsheet with summaries of common SQL commands: 1. The SELECT statement is used to query data from one or more tables and can include clauses like DISTINCT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. 2. Catalog commands like SHOW, CREATE, ALTER, and DROP are used to view and manage database objects like tables, columns, indexes, and constraints. 3. Writing data uses INSERT to add rows, DELETE to remove rows, and UPDATE to modify rows in tables.

Uploaded by

sreejtih
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

SQL Cheat

This document provides a SQL cheatsheet with summaries of common SQL commands: 1. The SELECT statement is used to query data from one or more tables and can include clauses like DISTINCT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, and LIMIT. 2. Catalog commands like SHOW, CREATE, ALTER, and DROP are used to view and manage database objects like tables, columns, indexes, and constraints. 3. Writing data uses INSERT to add rows, DELETE to remove rows, and UPDATE to modify rows in tables.

Uploaded by

sreejtih
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

SQL Cheatsheet

Brian McGill
April 27,2004

Select
SELECT [DISTINCT|ALL] colexpr1, colexpr2 … FROM table1 … [WHERE bexpr] [GROUP BY ...] [HAVING ][ORDER BY
col1, col2, …] [LIMIT n];
colexpr=expression [AS column_alias]
expression= + - * / % ( ) = != <= >= < > w/ colnames (or database.colname)
abs/mod/floor/ceiling/round/log/log10/pow/sqrt/sin/cos/tan/rand/truncate(expr,#dig)/format(expr,int)
dayofweek/dayname/monthname/week(date) date_add(date,INTERVAL 1 DAY) curdate()/curtime()/now()
if(bexpr,yesval,noval) database() user()
strexpr LIKE ‘letters%’ concat(strexpr,strexpr) length(stexpr) locate(strexpr,strexpr) substring(strexpr,start,length)
ltrim(strexpr) rtrim(strexpr) trim(strexpr) lower(strepxr) upper(strexpr)
space(n) strcmp(strexpr,strexpr) (returns –1,0,1)
COUNT(*) COUNT(DISTINCT expression) AVG/MIN/MAX/SUM (ALL|DISTINCT] expression)
GROUP BY expr1 [ASC|DESC] , expr2 …. [HAVING aggregate_expr] (having clause usually also in select)

Catalog
SHOW DATABASES; USE database; CREATE DATABASE database; (some systems)
SHOW TABLES; SHOW COLUMNS FROM tablename; SHOW INDEX from tablename; SHOW STATUS;
CREATE TABLE name( coldescr1,…[,keystate1,…],[constraint1,…]);
coldescr=name type [DEFAULT expr] [NULL|NOT NULL]
type=int(n) float(n,dec) timesteamp(n) char(n) varchar(n) blob javaobject(javaclass)
keystate=[PRIMARY] KEY (colname[,colname,…])
constraint=[CONSTRAIN][name] [PRIMARY] KEY (colname[,colname…) or UNIQUE(col1[,col2…]) or CHECK(expression)
ALTER TABLE name [ADD INDEX name(col1[,col2,…])] [ADD [COLUMN] coldescr] [DROP [COLUMN] name] [DROP
CONSTRAINT] [DROP PRIMARY KEY]
DROP TABLE [IF EXISTS] table1, …
CREATE VIEW name AS SELECT ….
DROP VIEW name
COMPACT TABLE name

Writing
INSERT INTO table VALUES (v1,v2,v3….)
or INSERT INTO TABLE SET colname=val, colname=val ….
DELETE FROM tablename WHERE bexpr
UPDATE tablename SET column=expr;

You might also like