INT
REAL
DOUBLE
DECIMAL(m, n): m total digits, with n digits after the decimal point
CHAR(n): n characters maximum; n bytes will always be used; 1 <= n <= 8000
VARCHAR(n): n characters maximum; number of bytes used is the actual length of each string; 1 <= n <= 8000
CREATE TABLE TableName (Field1 type, Field2 type, ... FieldN type)DROP TABLE TableNameINSERT INTO TableName VALUES (value1, value2, ... valueN)DELETE TableName WHERE...SELECT Field1, Field2, ... FieldN FROM TableNameSELECT * FROM TableNameSELECT ... WHERE... ORDER BY...UPDATE TableName SET Field1 = value1, Field2 = value2, ... FieldN = valueNUPDATE ... SET ... WHERE...SELECT * FROM NameTable WHERE FirstName = 'John'SELECT * FROM NameTable WHERE FirstName <> 'John'SELECT * FROM NameTable WHERE StudentID > 123SELECT * FROM CourseTable WHERE Course LIKE 'CSCI%'SELECT * FROM CourseTable WHERE Course LIKE 'MATH%' AND StudentID = 123SELECT COUNT(*) FROM CourseTable WHERE StudentID = 123SELECT MIN(StudentID) FROM NameTableMIN, MAX, AVG, COUNTSELECT * FROM NameTable ORDER BY LastName ascSELECT * FROM NameTable ORDER BY LastName descSELECT * FROM NameTable ORDER BY LastName desc, FirstName descA join is a query involving multiple tables simultaneously. In SQL, some additional syntax is needed to give the database the necessary cues to perform an efficient query.
Here is an example of a two-step query without a join:
SELECT StudentID FROM NameTable WHERE FirstName = 'John' AND LastName = 'Doe'In the example tables, this returns 123. Once we know this, we issue a followup query as follows:
SELECT Course FROM CourseTable WHERE StudentID = 123
Now we can see how to issue a single query to obtain this information by using a join:
SELECT CourseTable.Course
FROM CourseTable, NameTable
WHERE NameTable.FirstName = 'John'
AND NameTable.LastName = 'Doe'
AND NameTable.StudentID = CourseTable.StudentID
Without an index, the database will perform an O(n) search through a column when performing queries. Indexing can accelerate search substantially, but indexes can only be created manually.
Indices are typically created along with the table. The syntax is as follows:
CREATE TABLE TableName (Field1 type, Field2 type, ... FieldN type, INDEX indexName (FieldX), ...)The index name is optional. All data types can be indexed. CHAR indices can be restricted to index on a prefix by putting a length in parentheses. For example, the following will create an index that looks at the first five characters of the last name:
CREATE TABLE NameTable (StudentID INT, LastName CHAR(20), FirstName CHAR(15), INDEX ByLast(LastName(5)))