SQL - CREATE TABLE Statement - 1keydata
SQL - CREATE TABLE Statement - 1keydata
SQL Tutorial
Basic SQL Commands
SQL SELECT
SQL DISTINCT
SQL WHERE SQL > Data Definition Language (DDL) > Create Table Statement
SQL AND OR
In a relational database, data is stored in tables. Given that there is no way for the database vendor to know ahead of time
SQL IN
storage needs are, you will for sure need to create tables that fit your needs in the database. Therefore, the CREATE TABLE
SQL BETWEEN
of the most fundamental components of SQL.
SQL Wildcard
SQL LIKE Before we dive into the SQL syntax for CREATE TABLE, it is a good idea to understand what goes into a table. A table is m
SQL ORDER BY and columns. Each row represents one piece of data, and each column can be thought of as representing a component of t
SQL GROUP BY For example, if we have a table for recording customer information, then the columns may include information such as Fir
SQL HAVING Name, Address, City, Country, and Birth Date. As a result, when we specify a table, we include the column headers and the
SQL ALIAS each column. We may also decide to place certain limitations, or constraints, to guarantee that the data stored in the table
SQL AS
The SQL syntax for CREATE TABLE is
SQL SELECT UNIQUE
SQL JOIN
CREATE TABLE "table_name"
SQL INNER JOIN
("column 1" "data type for column 1" [column 1 constraint(s)],
SQL OUTER JOIN "column 2" "data type for column 2" [column 2 constraint(s)],
SQL LEFT OUTER JOIN ...
SQL CROSS JOIN [table constraint(s)] );
SQL INSERT INTO
SQL INSERT INTO SELECT "Column 1" and "column 2" represent the name of each column. After the name, we specify the data type for that column.
SQL UPDATE include integers (such as 1), real numbers (such as 0.55), strings (such as 'sql'), date/time expressions (such as '2000-JAN-2
SQL DELETE FROM binary types. Different relational databases allow for different data types, so please consult with a database-specific refere
Advanced SQL The [ ] symbol means that the phrase inside it may occur zero, one, or more times. When creating a table, specifying colum
SQL UNION constraints is optional, and each column and each table may have more than one constraint.
SQL UNION ALL
SQL Inline View Six types of constraints can be placed when creating a table:
SQL INTERSECT
NOT NULL Constraint: Ensures that a column cannot have NULL value.
SQL MINUS
DEFAULT Constraint: Provides a default value for a column when none is specified.
SQL LIMIT
UNIQUE Constraint: Ensures that all values in a column are different.
SQL TOP
CHECK Constraint: Makes sure that all values in a column satisfy certain criteria.
SQL Subquery
Primary Key Constraint: Used to uniquely identify a row in the table.
SQL EXISTS Foreign Key Constraint: Used to ensure referential integrity of the data.
SQL CASE
SQL DECODE To create a customer table with the fields specified in the second paragraph above, we would type in
SQL AUTO INCREMENT
SQL IDENTITY CREATE TABLE Customer
SEQUENCE & NEXTVAL (First_Name char(50),
Last_Name char(50),
SQL Functions Address char(50),
SQL Average City char(50),
SQL COUNT Country char(25),
Birth_Date datetime);
SQL MAX
SQL MIN
No constraints were specified in the above SQL statement. What if we want to add a constraint that says the default count
SQL SUM
States'? In other words, if no data is entered for the "Country" column, it would be set to 'United States.' In that case, we w
SQL ROUND
following SQL:
String Functions
SQL CAST CREATE TABLE Customer
SQL CONVERT (First_Name char(50),
SQL CONCATENATE Last_Name char(50),
Address char(50),
SQL SUBSTRING
City char(50),
SQL INSTR Country char(25) default 'United States',
SQL TRIM Birth_Date datetime);
http://www.1keydata.com/sql/sqlcreate.html 1/2
3/2/2016 SQL CREATE TABLE Statement | 1Keydata
SQL LENGTH
SQL REPLACE Sometimes the table structure we need is the same as another table in the database. In this case, we can also use CREATE
SQL TO_DATE copy of the table structure so we do not need to type all the column names, data types, and constraints in detail. The synt
Date Functions is:
CREATE TABLE "table_name" AS
[SQL Statement];
To copy both the structure and data of Table1 into Table2, we would issue the following SQL statement:
To copy the structure of Table1 into Table2 without any data, we would issue the following SQL statement:
The WHERE 0 = 1 clause is always false. Therefore, no row of data will be copied from Table1 to Table2. Only the table str
over.
Copyright © 2016 1keydata.com All Rights Reserved Privacy Policy About Contact
http://www.1keydata.com/sql/sqlcreate.html 2/2