© 2013 1keydata.com All Rights Reserved SQL Tutorial Table Management Add, Remove, and Truncate Tables
Database Basics Database Tables Columns Rows © 2013 1keydata.com All Rights Reserved In a relational database, data is stored in tables. A table consists of columns and rows.
CREATE TABLE © 2013 1keydata.com All Rights Reserved The first step to store data in a database is to add a table. To do this, we use the CREATE TABLE command in SQL. CREATE TABLE "table_name" ("column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2" ... ) The syntax for CREATE TABLE is:
CREATE TABLE © 2013 1keydata.com All Rights Reserved For example, the command below, CREATE TABLE ALL_USERS (LAST_NAME char(20), FIRST_NAME char(20)); results in the creation of a table ALL_USERS with two columns: LAST_NAME and FIRST_NAME, both 20 characters long.
DROP TABLE © 2013 1keydata.com All Rights Reserved One also needs to be able to remove tables from a database, otherwise database management becomes very difficult. SQL uses the DROP TABLE command to accomplish this. The syntax for removing a table is: DROP TABLE "table_name“; To drop the ALL_USERS table we previously created, we use the following SQL: DROP TABLE ALL_USERS;
TRUNCATE TABLE © 2013 1keydata.com All Rights Reserved To remove all data but keep the table structure, SQL uses the TRUNCATE TABLE command. The syntax for TRUNCATE TABLE is: TRUNCATE TABLE "table_name“; To truncate the ALL_USERS table we previously created, we use the following SQL: TRUNCATE TABLE ALL_USERS;
1Keydata SQL Tutorial http://www.1keydata.com/sql/sql.html © 2013 1keydata.com All Rights Reserved

SQL Tutorial - How To Create, Drop, and Truncate Table

  • 1.
    © 2013 1keydata.comAll Rights Reserved SQL Tutorial Table Management Add, Remove, and Truncate Tables
  • 2.
    Database Basics Database Tables Columns Rows © 20131keydata.com All Rights Reserved In a relational database, data is stored in tables. A table consists of columns and rows.
  • 3.
    CREATE TABLE © 20131keydata.com All Rights Reserved The first step to store data in a database is to add a table. To do this, we use the CREATE TABLE command in SQL. CREATE TABLE "table_name" ("column 1" "data_type_for_column_1", "column 2" "data_type_for_column_2" ... ) The syntax for CREATE TABLE is:
  • 4.
    CREATE TABLE © 20131keydata.com All Rights Reserved For example, the command below, CREATE TABLE ALL_USERS (LAST_NAME char(20), FIRST_NAME char(20)); results in the creation of a table ALL_USERS with two columns: LAST_NAME and FIRST_NAME, both 20 characters long.
  • 5.
    DROP TABLE © 20131keydata.com All Rights Reserved One also needs to be able to remove tables from a database, otherwise database management becomes very difficult. SQL uses the DROP TABLE command to accomplish this. The syntax for removing a table is: DROP TABLE "table_name“; To drop the ALL_USERS table we previously created, we use the following SQL: DROP TABLE ALL_USERS;
  • 6.
    TRUNCATE TABLE © 20131keydata.com All Rights Reserved To remove all data but keep the table structure, SQL uses the TRUNCATE TABLE command. The syntax for TRUNCATE TABLE is: TRUNCATE TABLE "table_name“; To truncate the ALL_USERS table we previously created, we use the following SQL: TRUNCATE TABLE ALL_USERS;
  • 7.