Monday, October 25, 2010

Summary of SQL Commands




SQL Create Database

CREATE DATABASE MyDatabase;

SQL - Create table

USE mydatabase;

CREATE TABLE inventory
(
   id INT IDENTITY(1,1) PRIMARY KEY,
   product VARCHAR(50) UNIQUE,
   quantity INT,
   price DECIMAL(18,2)
);

SQL Insert

USE mydatabase;

INSERT INTO inventory(id, product, quantity,price)
VALUES('19', 'LCD Screen','25','179.99');
 

SQL Select

USE mydatabase;

SELECT * FROM inventory;
SELECT * FROM inventory where product='LCD Screen';

SQL Select As Code:

USE mydatabase;

SELECT id AS "NewID", Product  As "Computer Screen", 
quantity,price FROM inventory;


SQL - And / Or

USE mydatabase;

SELECT *
FROM orders
WHERE (quantity > 2 AND customer = 'Tizag')
OR (quantity > 0 AND customer = 'Gerald Garner')

SQL Select Between:

USE mydatabase;

SELECT *
FROM orders
WHERE day_of_order BETWEEN '7/20/08' AND '8/05/08';

SQL - Order By

USE mydatabase;

SELECT *
FROM orders
WHERE customer = 'Tizag'
ORDER BY day_of_order DESC
 

SQL - Update

USE mydatabase;

UPDATE orders
SET quantity = '6'
WHERE id = '1'
 

SQL - Alter Table

USE mydatabase;

ALTER TABLE orders
ALTER COLUMN discount DECIMAL(18,2);
 

SQL - Drop Column

USE mydatabase;

ALTER TABLE orders
DROP COLUMN discount;

SQL - Distinct

USE mydatabase;

SELECT DISTINCT customer
FROM orders
WHERE day_of_order BETWEEN '7/31/08' AND '9/1/08';
reference:
http://www.tizag.com/sqlTutorial/sqlsubqueries.php
reference:
http://www.tizag.com/sqlTutorial/sqlcreate.php

No comments:

Post a Comment