0% found this document useful (0 votes)
8 views

All SQL

The document discusses SQL commands used to create, alter, drop and truncate tables. CREATE is used to create a new table with columns and constraints. ALTER modifies existing table structure by adding, renaming, modifying or dropping columns. DROP deletes an existing table. TRUNCATE deletes all rows from a table.

Uploaded by

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

All SQL

The document discusses SQL commands used to create, alter, drop and truncate tables. CREATE is used to create a new table with columns and constraints. ALTER modifies existing table structure by adding, renaming, modifying or dropping columns. DROP deletes an existing table. TRUNCATE deletes all rows from a table.

Uploaded by

coolscools123
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CREATE It is used to create a new table in the database.

CREATE TABLE <TABLE_NAME>


(COLUMN_NAME DATATYPES,[,....] [ Constaints]);

CREATE TABLE EMPLOYEE


(Name VARCHAR2(20),
Email VARCHAR2(100),
DOB DATE);
CREATE TABLE Students(
id int,
name varchar(50),
address text,
grades varchar(50),
phone varchar(10)
);

The constraints used in SQL are:

Constraint Description
NOT NULL values cannot be null

UNIQUE values cannot match any older value

PRIMARY KEY used to uniquely identify a row

FOREIGN KEY references a row in another table

CHECK validates condition for new value

DEFAULT set default value if not passed

CREATE INDEX used to speedup the read process


NOT NULL Constraint
CREATE TABLE Colleges (
college_id INT NOT NULL,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);

UNIQUE Constraint
CREATE TABLE Colleges (
college_id INT NOT NULL UNIQUE,
college_code VARCHAR(20) UNIQUE,
college_name VARCHAR(50)
);

PRIMARY KEY Constraint


CREATE TABLE Colleges (
college_id INT PRIMARY KEY,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);

FOREIGN KEY Constraint


CREATE TABLE Orders (
order_id INT PRIMARY KEY,
customer_id int REFERENCES Customers(id)
);
CHECK Constraint
CREATE TABLE Orders (
order_id INT PRIMARY KEY,
amount int CHECK (amount >= 100)
);

DEFAULT Constraint
CREATE TABLE College (
college_id INT PRIMARY KEY,
college_code VARCHAR(20),
college_country VARCHAR(20) DEFAULT 'US'
);

CREATE INDEX Constraint

If a column has CREATE INDEX constraint, it's faster to


retrieve data if we use that column for data retrieval.
CREATE TABLE Colleges (
college_id INT PRIMARY KEY,
college_code VARCHAR(20) NOT NULL,
college_name VARCHAR(50)
);

CREATE INDEX college_index


ON Colleges(college_code);
CREATE TABLE IF NOT EXISTS
If we try to create a table that already exists, we
get an error message 'Error: table already
exists' .

CREATE TABLE IF NOT EXISTS Companies (


id int,
name varchar(50),
address text,
email varchar(50),
phone varchar(10)
);

Create Table Using Another Existing Table


CREATE TABLE CustomersBackup
AS
SELECT *
FROM Customers;
DROP TABLE Statement
DROP TABLE is used to delete the tables in our
database.

DROP TABLE table_name;

Example

DROP TABLE student;

DROP TABLE IF EXISTS


DROP TABLE IF EXISTS <tablename>;

ALTER TABLE Statement


ALTER TABLE Operations - We can perform the
following operations on a table using the ALTER
TABLE command:
Add a column
Rename a column
Modify a column
Delete a column
Rename a table
Add Column in a Table
ALTER TABLE Customers
ADD phone varchar(10);

Add Multiple Columns in a Table

ALTER TABLE Customers


ADD phone varchar(10), age int;

Rename Column in a Table


ALTER TABLE Customers
RENAME COLUMN customer_id TO c_id;

Modify the Data Type of a Column

ALTER TABLE Customers


MODIFY age VARCHAR(2);

Modify the Data Type of a Column

ALTER TABLE Customers


MODIFY age VARCHAR(2);
Drop Column in a Table
We can also drop (remove) columns in a table using the ALTER

TABLE command with the DROP clause.


ALTER TABLE Customers
DROP COLUMN country;

Rename a Table
We can change the name of a table using the ALTER TABLE command with
the RENAME clause.
ALTER TABLE Customers
RENAME TO New_customers;

Syntax
RENAME TABLE table-Name TO new-Table-Name

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax

1. DROP TABLE table_name;

Example

1. DROP TABLE EMPLOYEE;

c. ALTER: It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new
attribute.

Syntax:

ADVERTISEMENT

To add a new column in the table

1. ALTER TABLE table_name ADD column_name COLUMN-definition;


To modify existing column in the table:

1. ALTER TABLE table_name MODIFY(column_definitions....);

EXAMPLE

1. ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));


2. ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));

d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.

Syntax:

1. TRUNCATE TABLE table_name;

Example:

1. TRUNCATE TABLE EMPLOYEE;

You might also like