Base de datos

Páginas: 18 (4432 palabras) Publicado: 9 de febrero de 2012
SQL Syntax
Select
SELECT "column_name" FROM "table_name"
Distinct
SELECT DISTINCT "column_name"
FROM "table_name"
Where
SELECT "column_name"
FROM "table_name"
WHERE "condition"
And/Or
SELECT "column_name"
FROM "table_name"
WHERE "simple condition"
{[AND|OR] "simple condition"}+
In
SELECT "column_name"
FROM "table_name"
WHERE "column_name" IN ('value1', 'value2', ...)
BetweenSELECT "column_name"
FROM "table_name"
WHERE "column_name" BETWEEN 'value1' AND 'value2'
Like
SELECT "column_name"
FROM "table_name"
WHERE "column_name" LIKE {PATTERN}
Order By
SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC]
Count
SELECT COUNT("column_name")
FROM "table_name"
Group By
SELECT "column_name1", SUM("column_name2")
FROM"table_name"
GROUP BY "column_name1"
Having
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
HAVING (arithematic function condition)
Create Table
CREATE TABLE "table_name"
("column 1" "data_type_for_column_1",
"column 2" "data_type_for_column_2",
... )
Drop Table
DROP TABLE "table_name"
Truncate Table
TRUNCATE TABLE "table_name"
Insert Into
INSERTINTO "table_name" ("column1", "column2", ...)
VALUES ("value1", "value2", ...)
Update
UPDATE "table_name"
SET "column_1" = [new value]
WHERE {condition}
Delete From
DELETE FROM "table_name"
WHERE {condition}

SQL SELECT

SELECT "column_name" FROM "table_name"
To illustrate the above example, assume that we have the following table:
Table Store_Information
|store_name |Sales|Date |
|Los Angeles |$1500 |Jan-05-1999 |
|San Diego |$250 |Jan-07-1999 |
|Los Angeles |$300 |Jan-08-1999 |
|Boston |$700 |Jan-08-1999 |

To select all the stores in this table, we key in,
SELECT store_name FROM Store_Information
Result:

|store_name |
|Los Angeles |
|San Diego ||Los Angeles |
|Boston |


SQL DISTINCT

The SELECT keyword allows us to grab all information from a column (or columns) on a table. This, of course, necessarily mean that there will be redundencies. What if we only want to select each DISTINCT element? This is easy to accomplish in SQL. All we need to do is to add DISTINCT after SELECT. The syntax is as follows:
SELECT DISTINCT"column_name"
FROM "table_name"
For example, to select all distinct stores in Table Store_Information,
Table Store_Information
|store_name |Sales |Date |
|Los Angeles |$1500 |Jan-05-1999 |
|San Diego |$250 |Jan-07-1999 |
|Los Angeles |$300 |Jan-08-1999 |
|Boston |$700 |Jan-08-1999 |

we key in,SELECT DISTINCT store_name FROM Store_Information
Result:
|store_name |
|Los Angeles |
|San Diego |
|Boston |


SQL WHERE

Next, we might want to conditionally select the data from a table. For example, we may want to only retrieve stores with sales above $1,000. To do this, we use the WHERE keyword. The syntax is as follows:
SELECT "column_name"
FROM"table_name"
WHERE "condition"
For example, to select all stores with sales above $1,000 in Table Store_Information,
Table Store_Information
|store_name |Sales |Date |
|Los Angeles |$1500 |Jan-05-1999 |
|San Diego |$250 |Jan-07-1999 |
|Los Angeles |$300 |Jan-08-1999 |
|Boston |$700 |Jan-08-1999 |

wekey in,
SELECT store_name
FROM Store_Information
WHERE Sales > 1000

Result:
|store_name |
|Los Angeles |


SQL AND OR

In the previous section, we have seen that the WHERE keyword can be used to conditionally select data from a table. This condition can be a simple condition (like the one presented in the previous section), or it can be a compound condition. Compound...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Que es una base de datos y tipos de base de datos
  • Bases de datos y usuarios de bases de datos
  • Base De Datos
  • Base De Datos
  • Base de datos
  • Base De Datos
  • Base de datos
  • Bases de datos

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS