0% found this document useful (0 votes)
9 views10 pages

SQL Simple Commands

The document outlines essential SQL commands for database management, including SELECT, INSERT, UPDATE, DELETE, and various table operations like CREATE, ALTER, and DROP. It provides syntax examples for each command and explains their functions, such as selecting data, inserting new records, and modifying existing ones. Additionally, it covers the creation and management of indexes to enhance data retrieval performance.

Uploaded by

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

SQL Simple Commands

The document outlines essential SQL commands for database management, including SELECT, INSERT, UPDATE, DELETE, and various table operations like CREATE, ALTER, and DROP. It provides syntax examples for each command and explains their functions, such as selecting data, inserting new records, and modifying existing ones. Additionally, it covers the creation and management of indexes to enhance data retrieval performance.

Uploaded by

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

 SELECT - extracts data from a database

 UPDATE - updates data in a database


 DELETE - deletes data from a database
 INSERT INTO - inserts new data into a database
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
 DROP TABLE - deletes a table
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

1. The SELECT statement is used to select data from a database.

The data returned is stored in a result table, called the result-set.

SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Here, column1, column2, ... are the field names of the table you want
to select data from. If you want to select all the fields available in the
table, use the following syntax:

SELECT * FROM table_name;

Demo Database
Below is a selection from the "Customers" table in the Northwind
sample database:

Customer CustomerNa ContactNa Address City PostalCo Countr


ID me me de y

1 Alfreds Maria Obere Str. Berlin 12209 Germa


Futterkiste Anders 57 ny

2 Ana Trujillo Ana Trujillo Avda. de la Méxic 05021 Mexico


Emparedados Constitució o D.F.
y helados n 2222

3 Antonio Antonio Mataderos Méxic 05023 Mexico


Moreno Moreno 2312 o D.F.
Taquería

4 Around the Thomas 120 Londo WA1 1DP UK


Horn Hardy Hanover n
Sq.

5 Berglunds Christina Berguvsväg Luleå S-958 22 Sweden


snabbköp Berglund en 8

SELECT Column Example


The following SQL statement selects the "CustomerName" and "City"
columns from the "Customers" table:

Example
SELECT CustomerName, City FROM Customers;
The SQL SELECT DISTINCT
Statement
The SELECT DISTINCT statement is used to return only distinct (different)
values.

Inside a table, a column often contains many duplicate values; and


sometimes you only want to list the different (distinct) values.

SELECT DISTINCT Syntax


SELECT DISTINCT column1, column2, ...
FROM table_name;

The SQL INSERT INTO Statement


The INSERT INTO statement is used to insert new records in a table.

INSERT INTO Syntax


It is possible to write the INSERT INTO statement in two ways:

1. Specify both the column names and the values to be inserted:

INSERT INTO table_name (column1, column2, column3, ...)


VALUES (value1, value2, value3, ...);

2. If you are adding values for all the columns of the table, you do not
need to specify the column names in the SQL query. However, make
sure the order of the values is in the same order as the columns in the
table. Here, the INSERT INTO syntax would be as follows:

INSERT INTO table_name


VALUES (value1, value2, value3, ...);

Insert Data Only in Specified


Columns
It is also possible to only insert data in specific columns.
The following SQL statement will insert a new record, but only insert
data in the "CustomerName", "City", and "Country" columns
(CustomerID will be updated automatically):

Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

The SQL UPDATE Statement


The UPDATE statement is used to modify the existing records in a table.

UPDATE Syntax
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Table
The following SQL statement updates the first customer (CustomerID =
1) with a new contact person and a new city.

Example
UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

The SQL DELETE Statement


The DELETE statement is used to delete existing records in a table.

DELETE Syntax
DELETE FROM table_name WHERE condition;
SQL ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in
an existing table.

The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.

ALTER TABLE - ADD Column


To add a column in a table, use the following syntax:

ALTER TABLE table_name


ADD column_name datatype;

The following SQL adds an "Email" column to the "Customers" table:

Example
ALTER TABLE Customers
ADD Email varchar(255);

ALTER TABLE - DROP COLUMN


To delete a column in a table, use the following syntax (notice that
some database systems don't allow deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;

The following SQL deletes the "Email" column from the "Customers"
table:

Example
ALTER TABLE Customers
DROP COLUMN Email;
ALTER TABLE - RENAME COLUMN
To rename a column in a table, use the following syntax:

ALTER TABLE table_name


RENAME COLUMN old_name to new_name;

ALTER TABLE - ALTER/MODIFY


DATATYPE
To change the data type of a column in a table, use the following
syntax:

SQL Server / MS Access:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

My SQL

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

The SQL CREATE DATABASE


Statement
The CREATE DATABASE statement is used to create a new SQL database.

Syntax
CREATE DATABASE databasename;

CREATE DATABASE Example


The following SQL statement creates a database called "testDB":
Example
CREATE DATABASE testDB;

Tip: you can check it in the list of databases with the following SQL
command: SHOW DATABASES;

Alter Database:

Syntax:

ALTER DATABASE <Databse_name>


MODIFY NAME = <New Name>

Example:
ALTER DATABASE Edu_TSQL
MODIFY NAME = Edu_TSQL_Alter;

CREATE TABLE
The CREATE TABLE command creates a new table in the database.

The following SQL creates a table called "Persons" that contains five
columns: PersonID, LastName, FirstName, Address, and City:

Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

CREATE TABLE Using Another


Table
A copy of an existing table can also be created using CREATE TABLE.

The following SQL creates a new table called "TestTables" (which is a


copy of the "Customers" table):
Example
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

ALTER TABLE
The ALTER TABLE command adds, deletes, or modifies columns in a
table.

The ALTER TABLE command also adds and deletes various constraints in
a table.

The following SQL adds an "Email" column to the "Customers" table:

Example
ALTER TABLE Customers
ADD Email varchar(255);

The following SQL deletes the "Email" column from the "Customers"
table:

Example
ALTER TABLE Customers
DROP COLUMN Email;

DROP TABLE
The DROP TABLE command deletes a table in the database.

The following SQL deletes the table "Shippers":

Example
DROP TABLE Shippers;

Note: Be careful before deleting a table. Deleting a table results in loss


of all information stored in the table!
TRUNCATE TABLE
The TRUNCATE TABLE command deletes the data inside a table, but not
the table itself.

The following SQL truncates the table "Categories":

Example
TRUNCATE TABLE Categories;

CREATE TABLE
The CREATE TABLE command creates a new table in the database.

The following SQL creates a table called "Persons" that contains five
columns: PersonID, LastName, FirstName, Address, and City:

Example
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

CREATE TABLE Using Another


Table
A copy of an existing table can also be created using CREATE TABLE.

The following SQL creates a new table called "TestTables" (which is a


copy of the "Customers" table):

Example
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;
CREATE and DROP INDEX Statement in SQL

1. CREATE INDEX Statement :

The CREATE INDEX statement will create indexes in


tables. Indexes are used for data procurement from the databases
faster. The users cannot see the indexes, they are running in the
background of queries, used to speed up searches/queries.

Syntax :Create an index on a table :

CREATE INDEX indexname


ON tablename (columnname1, columnname2, ...);

Create a unique index on a table –


CREATE UNIQUE INDEX indexname
ON tablename (columnname1, columnname2, ...);

Example –
Below SQL statement would create an index named “idx_name” on the
“LastName” column in the “Student” table –

You might also like