SQL Simple Commands
SQL Simple Commands
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:
Demo Database
Below is a selection from the "Customers" table in the Northwind
sample database:
Example
SELECT CustomerName, City FROM Customers;
The SQL SELECT DISTINCT
Statement
The SELECT DISTINCT statement is used to return only distinct (different)
values.
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:
Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');
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;
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.
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;
ALTER TABLE - RENAME COLUMN
To rename a column in a table, use the following syntax:
My SQL
Syntax
CREATE DATABASE databasename;
Tip: you can check it in the list of databases with the following SQL
command: SHOW DATABASES;
Alter Database:
Syntax:
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)
);
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.
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.
Example
DROP TABLE Shippers;
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)
);
Example
CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;
CREATE and DROP INDEX Statement in SQL
Example –
Below SQL statement would create an index named “idx_name” on the
“LastName” column in the “Student” table –