Saturday 6 October 2012

CREATE Statement

Not only can SQL retrieve data, it can also define data structures and create objects. The create statement can be used to create tables, schemas and even users.

Data types
INTEGER- whole numbers(no decimals)
NUMERIC- decimal numbers allowed
VARCHAR(n)- text, can have a maximum length of n
CHAR(n)- text, must be of length n
DATE-date value

Properties
NOT NULL- value cannot be null
UNIQUE-value must be unique within attribute
AUTO_INCREMENT- automatically increase value for next row

Format
CREATE TABLE table(
attribute1 data type <properties>,
attribute2 data type <properties>,
attribute3 data type <properties>
PRIMARY KEY(attribute1)
);

Create table with foreign key
CREATE TABLE table(
attribute1 data type <properties>,
attribute2 data type <properties>,
attribute3 data type <properties>
PRIMARY KEY(attribute1)
FOREIGN KEY (attribute2) REFERENCES other table
);

Add composite keys instead
PRIMARY KEY(attribute1, attribute2)

No comments:

Post a Comment