0% found this document useful (0 votes)
2 views1 page

Module 2-4

This document is a SQL cheat sheet that provides syntax and examples for the CREATE TABLE, ALTER TABLE, TRUNCATE TABLE, and DROP TABLE commands in MySQL and DB2. It details how to create tables, modify columns, drop columns, rename columns, truncate tables, and delete tables. The authors of the document are Himanshu Birla and Niveditha Pandith TS.

Uploaded by

dowanan742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views1 page

Module 2-4

This document is a SQL cheat sheet that provides syntax and examples for the CREATE TABLE, ALTER TABLE, TRUNCATE TABLE, and DROP TABLE commands in MySQL and DB2. It details how to create tables, modify columns, drop columns, rename columns, truncate tables, and delete tables. The authors of the document are Himanshu Birla and Niveditha Pandith TS.

Uploaded by

dowanan742
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

4/10/25, 5:14 PM about:blank

SQL Cheat Sheet: CREATE TABLE, ALTER, DROP, TRUNCATE

Command Syntax Description Example

MySQL/DB2: CREATE TABLE table_name (col1 CREATE TABLE statement is to create the table.
CREATE datatype optional keyword, col2 datatype Each column in the table is specified with its MySQL/DB2: CREATE TABLE employee (
optional keyword,col3 datatype optional employee_id char(2) PRIMARY KEY, first_name
TABLE keyword,..., coln datatype optional
name, data type and an optional keyword which varchar(30) NOT NULL, mobile int);
keyword) could be PRIMARY KEY, NOT NULL, etc.,

MySQL/DB2:
MySQL/DB2
Option 1. ALTER TABLE table_name ADD
ALTER column_name_1 datatype....ADD COLUMN Option 1. ALTER TABLE employee ADD income
TABLE - column_name_n datatype; ALTER TABLE statement is used to add the bigint;
ADD columns to a table.
COLUMN
Option 2. ALTER TABLE table_name ADD COLUMN Option 2. ALTER TABLE employee ADD COLUMN
column_name_1 datatype....ADD COLUMN income bigint;
column_name_n datatype;

MySQL: ALTER TABLE MODIFY MODIFY clause


ALTER MySQL: ALTER TABLE table_name MODIFY is used with the ALTER TABLE statement to MySQL: ALTER TABLE employee MODIFY mobile
column_name_1 new_data_type; CHAR(20);
TABLE - modify the data type of columns.
ALTER
DB2: ALTER TABLE table_name ALTER COLUMN DB2: ALTER TABLE employee ALTER COLUMN
COLUMN column_name_1 SET DATA TYPE datatype;
Db2: ALTER TABLE ALTER COLUMN statement is mobile SET DATA TYPE CHAR(20);
used to modify the data type of columns.

ALTER
TABLE - MySQL/DB2: ALTER TABLE table_name DROP ALTER TABLE DROP COLUMN statement is used to MySQL/DB2:
DROP COLUMN column_name_1 ; remove columns from a table. ALTER TABLE employee DROP COLUMN mobile ;
COLUMN

MySQL:ALTER TABLE table_name CHANGE MySQL: ALTER TABLE CHANGE COLUMN


ALTER CHANGE COLUMN clause is used to rename MySQL: ALTER TABLE employee CHANGE COLUMN
COLUMN current_column_name new_column_name
first_name name VARCHAR(255);
TABLE - datatype [optional keywords]; the columns in a table.
RENAME DB2: ALTER TABLE employee RENAME COLUMN
COLUMN DB2: ALTER TABLE table_name RENAME COLUMN DB2: ALTER TABLE RENAME COLUMN statement is first_name TO name;
current_column_name TO new_column_name; used to rename the columns in a table.

MySQL: TRUNCATE TABLE statement is used to


delete all of the rows in a table.
TRUNCATE MySQL: TRUNCATE TABLE table_name; MySQL: TRUNCATE TABLE employee;
TABLE Db2: The IMMEDIATE specifies to process the
DB2: TRUNCATE TABLE table_name IMMEDIATE; DB2: TRUNCATE TABLE employee IMMEDIATE ;
statement immediately and that it cannot be
undone.

Use the DROP TABLE statement to delete a table


DROP from a database. If you delete a table that MySQL/DB2:
MySQL/DB2DROP TABLE table_name ;
TABLE contains data, by default the data will be deleted DROP TABLE employee ;
alongside the table.

Author(s)
Himanshu Birla
Niveditha Pandith TS

about:blank 1/1

You might also like