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

SQL Examples

The document discusses SQL CREATE TABLE statement syntax and various SQL table constraints like PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, CHECK, DEFAULT and INDEX that can be specified when creating a table. Examples are provided for each constraint.

Uploaded by

Retaj Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Examples

The document discusses SQL CREATE TABLE statement syntax and various SQL table constraints like PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE, CHECK, DEFAULT and INDEX that can be specified when creating a table. Examples are provided for each constraint.

Uploaded by

Retaj Ali
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

SQL examples

The CREATE TABLE statement is used to create a


new table in a database.
• The SQL CREATE TABLE Statement
• Syntax
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
• The column parameters specify the names of the columns of the table.
• The datatype parameter specifies the type of data the column can hold (e.g.
varchar, integer, date, etc.).
Required creates a table called "Person" that
contains five columns: PersonID, LastName,
FirstName, Address, and City
• Answer The PersonID column is of type int
CREATE TABLE Person ( and will hold an integer.
PersonID int, The LastName, FirstName, Address,
LastName varchar(20), and City columns are of type varchar
FirstName varchar(20), and will hold characters, and the
Address varchar(30), maximum length for these fields is
City varchar(15) 255 characters.
); Person
PersonID LastName FirstName
Address City
Required create the CUSTOMER table if there
is no table exists with the same name
• CREATE TABLE IF NOT EXISTS CUSTOMER( Syntax
Following is the basic syntax of
• ID INT NOT NULL, a SQL CREATE TABLE statement
• NAME VARCHAR (20) NOT NULL, CREATE TABLE table_name(
column1 datatype,
• DOB INT NOT NULL, column2 datatype,
• ADDRESS CHAR (25), column3 datatype,
• SALARY DECIMAL (18, 2), .....
columnN datatype,
• PRIMARY KEY (ID) PRIMARY KEY( one or more
• ); columns )
);
SQL Create Constraints
• Constraints can be specified when the table is created with the CREATE
TABLE statement, or after the table is created with the ALTER TABLE
statement.
• Syntax
• CREATE TABLE table_name (
• column1 datatype constraint,
• column2 datatype constraint,
• column3 datatype constraint,
• ....
• );
SQL Create Constraints

• Constraints can be column level or table level. Column level constraints apply to a
column, and table level constraints apply to the whole table.
• The following constraints are commonly used in SQL:
• NOT NULL - Ensures that a column cannot have a NULL value
• UNIQUE - Ensures that all values in a column are different
• PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies
each row in a table
• FOREIGN KEY - Prevents actions that would destroy links between tables
• CHECK - Ensures that the values in a column satisfies a specific condition
• DEFAULT - Sets a default value for a column if no value is specified
• CREATE INDEX - Used to create and retrieve data from the database very quickly
NOT NULL Constraint
• The NOT NULL constraint in a column means that the column cannot store
NULL values. For example,
• Required create table entitled college , making sure that the college id is
not null
• answer
• CREATE TABLE College (
• college_id INT NOT NULL,
• college_code VARCHAR(10) NOT NULL,
• college_name VARCHAR(35)
• );
UNIQUE Constraint
• The UNIQUE constraint in a column means that the column must have
unique value. For example,
• Required create table entitled college , making sure that the college id is
not null and unique
• answer
• CREATE TABLE College (
• college_id INT NOT NULL UNIQUE,
• college_code VARCHAR(20) UNIQUE,
• college_name VARCHAR(50)
• );
PRIMARY KEY Constraint
• The PRIMARY KEY constraint is simply a combination of NOT NULL and
UNIQUE constraints. It means that the column value is used to uniquely
identify the row. For example,
• Required create table entitled college , making sure that the college id is
the primary key
• answer
• CREATE TABLE College (
• college_id INT PRIMARY KEY,
• college_code VARCHAR(20) NOT NULL,
• college_name VARCHAR(50)
• );
FOREIGN KEY Constraint
• The FOREIGN KEY (REFERENCES in some databases) constraint in a
column is used to reference a record that exists in another table. For
example,
• Required create table entitled order , making sure that the order_id
is the primary key and customer id is the foreign key
• answer
• CREATE TABLE Order (
• order_id INT PRIMARY KEY,
• customer_id int REFERENCES Customers(id)
• );
CHECK Constraint
• The CHECK constraint checks the condition before allowing values in a
table. For example,

• CREATE TABLE Order (


• order_id INT PRIMARY KEY,
• amount int CHECK (amount >= 100)
• );
CREATE INDEX Constraint
• If a column has CREATE INDEX constraint, it's faster to retrieve data if we use that
column for data retrieval. For example,
• -- create table
• CREATE TABLE College (
• college_id INT PRIMARY KEY,
• college_code VARCHAR(20) NOT NULL,
• college_name VARCHAR(50)
• );
• -- create index
• CREATE INDEX college_index
• ON Colleges(college_code);
DEFAULT Constraint
• The DEFAULT constraint is used to set the default value if we try to
store NULL in a column. For example,

• CREATE TABLE College (


• college_id INT PRIMARY KEY,
• college_code VARCHAR(20),
• college_country VARCHAR(20) DEFAULT 'US'
• );

You might also like