0% found this document useful (0 votes)
51 views13 pages

Column1 Datatype, Column2 Datatype, Column3 Datatype,: Alter

The document describes various SQL statements used to define and manipulate databases and tables. It covers statements for creating, altering, dropping, and selecting data from databases and tables, including the CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, SELECT, WHERE, ORDER BY, and UPDATE statements. It also describes different SQL constraints that can be used like PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK and DEFAULT.

Uploaded by

Puchki Arpita
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
51 views13 pages

Column1 Datatype, Column2 Datatype, Column3 Datatype,: Alter

The document describes various SQL statements used to define and manipulate databases and tables. It covers statements for creating, altering, dropping, and selecting data from databases and tables, including the CREATE DATABASE, CREATE TABLE, ALTER TABLE, DROP TABLE, SELECT, WHERE, ORDER BY, and UPDATE statements. It also describes different SQL constraints that can be used like PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK and DEFAULT.

Uploaded by

Puchki Arpita
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 13

The SQL CREATE DATABASE Statement

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

CREATE DATABASE databasename;

The SQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

DROP DATABASE databasename;

The CREATE TABLE statement is used to create a new table in a database.

CREATE TABLE table_name (


column1 datatype,
column2 datatype,
column3 datatype,
....
);

The SQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.

DROP TABLE table_name;

ALTER TABLE table_name

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.

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

ALTER TABLE table_name


ADD column_name datatype;

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;

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

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

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

SQL Create Constraints

Constraints can be specified when the table is created with the CREATE TABLE statement, or after the
table is created with the ALTER TABLE statement.

CREATE TABLE table_name (


column1 datatype constraint,
column2 datatype constraint,
column3 datatype constraint,
....
);

SQL Constraints

SQL constraints are used to specify rules for the data in a table.

Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and
reliability of the data in the table. If there is any violation between the constraint and the data action,
the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table
level constraints apply to the whole table.

The following constraints are commonly used in SQL:

 NOT NULL - Ensures that a column cannot have a NULL value

 UNIQUE - Ensures that all values in a column are different

 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a
table

 FOREIGN KEY - Uniquely identifies a row/record in another table

 CHECK - Ensures that all values in a column satisfies a specific condition

 DEFAULT - Sets a default value for a column when no value is specified

 INDEX - Used to create and retrieve data from the database very quickly

SQL NOT NULL Constraint


By default, a column can hold NULL values.

The NOT NULL constraint enforces a column to NOT accept NULL values.

This enforces a field to always contain a value, which means that you cannot
insert a new record, or update a record without adding a value to this field.

The following SQL ensures that the "ID", "LastName", and "FirstName" columns
will NOT accept NULL values:

EXAMPLE :

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int
);

Tip: If the table has already been created, you can add a NOT NULL constraint
to a column with the ALTER TABLEstatement.
SQL UNIQUE Constraint on CREATE TABLE

The following SQL creates a UNIQUE constraint on the "ID" column when the "Persons" table is created:

SQL

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
UNIQUE (ID)
);

SQL PRIMARY KEY on CREATE TABLE

The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
SCHEMA OF THE DATABASE:

CUSTOMER(CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)

CATEGORY(CategoryID, CategoryName, Description)

EMPLOYEE( EmployeeID, LastName, FirstName, BirthDate, Photo, Notes)

ORDERDETAILS(OrderDetailID, OrderID, ProductID, Quantity)

ORDERS(OrderID, CustomerID, EmployeeID, OrderDate, ShipperID)

PRODUCTS(ProductID, ProductName, SupplierID, CategoryID, Unit, Price)

SHIPPERS(ShipperID, ShipperName, Phone)

SUPPLIERS(SupplierID, SupplierName, ContactName, Address, City, PostalCode, Country Phone)

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;

The following SQL statement selects all the columns from the "Customers"
table:

Example
SELECT * 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.

The SELECT DISTINCT statement is used to return only distinct (different)


values.

SELECT DISTINCT Syntax


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

SELECT Example
The following SQL statement selects all (and duplicate) values from the

"Country" column in the "Customers" table:

Example
SELECT Country FROM Customers;

SELECT DISTINCT Examples


The following SQL statement selects only the DISTINCT values from the
"Country" column in the "Customers" table:

Example
SELECT DISTINCT Country FROM Customers;

The following SQL statement lists the number of different (distinct) customer
countries:

Example
SELECT COUNT(DISTINCT Country) FROM Customers;
The SQL WHERE Clause
The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified
condition.

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

WHERE Clause Example


The following SQL statement selects all the customers from the country
"Mexico", in the "Customers" table:

Example

SELECT * FROM Customers


WHERE Country='Mexico';

Text Fields vs. Numeric Fields


SQL requires single quotes around text values (most database systems will also
allow double quotes).

However, numeric fields should not be enclosed in quotes:

Example
SELECT * FROM Customers
WHERE CustomerID=1;

The SQL AND, OR and NOT Operators


The WHERE clause can be combined with AND, OR, and NOT operators.

The AND and OR operators are used to filter records based on more than one
condition:

 The AND operator displays a record if all the conditions separated by AND
is TRUE.
 The OR operator displays a record if any of the conditions separated by
OR is TRUE.

The NOT operator displays a record if the condition(s) is NOT TRUE.

AND Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

OR Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

NOT Syntax
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

AND Example
The following SQL statement selects all fields from "Customers" where country
is "Germany" AND city is "Berlin":

Example

SELECT * FROM Customers


WHERE Country='Germany' AND City='Berlin';
OR Example
The following SQL statement selects all fields from "Customers" where city is
"Berlin" OR "München":

Example
SELECT * FROM Customers
WHERE City='Berlin' OR City='München';

NOT Example
The following SQL statement selects all fields from "Customers" where country
is NOT "Germany":

Example

SELECT * FROM Customers


WHERE NOT Country='Germany';

Combining AND, OR and NOT


You can also combine the AND, OR and NOT operators.

The following SQL statement selects all fields from "Customers" where country
is "Germany" AND city must be "Berlin" OR "München" (use parenthesis to form
complex expressions):

Example
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');

The following SQL statement selects all fields from "Customers" where country
is NOT "Germany" and NOT "USA":
Example
SELECT * FROM Customers
WHERE NOT Country='Germany' AND NOT Country='USA';

The SQL ORDER BY Keyword


The ORDER BY keyword is used to sort the result-set in ascending or
descending order.

The ORDER BY keyword sorts the records in ascending order by default. To sort
the records in descending order, use the DESC keyword.

ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

ORDER BY Example
The following SQL statement selects all customers from the "Customers" table,

sorted by the "Country" column:

Example
SELECT * FROM Customers
ORDER BY Country;

ORDER BY DESC Example


The following SQL statement selects all customers from the "Customers" table,
sorted DESCENDING by the "Country" column:
Example

SELECT * FROM Customers


ORDER BY Country DESC;

ORDER BY Several Columns Example


The following SQL statement selects all customers from the "Customers" table,
sorted by the "Country" and the "CustomerName" column:

Example
SELECT * FROM Customers
ORDER BY Country, CustomerName;

ORDER BY Several Columns Example 2


The following SQL statement selects all customers from the "Customers" table,
sorted ascending by the "Country" and descending by the "CustomerName"
column:

Example
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

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;

Note: Be careful when updating records in a table! Notice the WHERE clause in
the UPDATE statement. The WHERE clause specifies which record(s) that should
be updated. If you omit the WHERE clause, all records in the table will be
updated!

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;

UPDATE Multiple Records


It is the WHERE clause that determines how many records that will be updated.

The following SQL statement will update the contactname to "Juan" for all
records where country is "Mexico":

Example
UPDATE Customers
SET ContactName='Juan'
WHERE Country='Mexico';

****Be careful when updating records. If you omit the WHERE clause, ALL
records will be updated!
Example
UPDATE Customers
SET ContactName='Juan';

SQL DELETE Example


The following SQL statement deletes the customer "Alfreds Futterkiste" from the
"Customers" table:

Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste';

Delete All Records


It is possible to delete all rows in a table without deleting the table. This means
that the table structure, attributes, and indexes will be intact:

DELETE FROM table_name;

or:

DELETE * FROM table_name;

grant connect to Arpita

grant connect,resource,DBA to Arpita

grant create session,

grant any privilege to Arpita

grant unlimited tablespace to Arpita

You might also like