Lecture 6
Lecture 6
Example:
The following SQL statement creates a database called
"testDB":
CREATE DATABASE testDB;
Example:
The following SQL statement drops the existing database
called "testDB":
DROP DATABASE testDB;
Note: Be careful before dropping a database. Deleting
a database will result in loss of complete information
stored in the database! > CHANGE, EVERYDAY
Creating a table in a database
The CREATE TABLE statement is used to create a new table in a database.
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example:
The following SQL statement creates a table called "Persons" that contains five
columns: PersonID, LastName, FirstName, Address, and City:
CREATE TABLE Persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
); > CHANGE, EVERYDAY
Dropping a table in a database
The DROP TABLE statement is used to drop an existing table
in a database.
Syntax:
DROP TABLE table_name;
Example:
The following SQL statement drops the existing table
"Shippers":
DROP TABLE Shippers;
In SQL there are three main data types: string, numeric, and
date and time.
varchar(n) Variable width character string 8,000 characters 2 bytes + number of chars
varchar(max) Variable width character string 1,073,741,824 characters 2 bytes + number of chars
text Variable width character string 2GB of text data 4 bytes + number of chars
decimal(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal
point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p.
Default value is 0
numeric(p,s) Fixed precision and scale numbers.Allows numbers from -10^38 +1 to 10^38 –1. 5-17 bytes
The p parameter indicates the maximum total number of digits that can be stored (both to the left and to the right of the decimal
point). p must be a value from 1 to 38. Default is 18.
The s parameter indicates the maximum number of digits stored to the right of the decimal point. s must be a value from 0 to p.
Default value is 0
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308.The n parameter indicates whether the field should hold 4 or 8 4 or 8 bytes
bytes. float(24) holds a 4-byte field and float(53) holds an 8-byte field. Default value of n is 53.
datetime From January 1, 1753 to December 31, 9999 with an accuracy of 3.33 milliseconds 8 bytes
datetime2 From January 1, 0001 to December 31, 9999 with an accuracy of 100 nanoseconds 6-8 bytes
smalldatetime From January 1, 1900 to June 6, 2079 with an accuracy of 1 minute 4 bytes
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
timestamp Stores a unique number that gets updated every time a row gets created or modified. The timestamp value
is based upon an internal clock and does not correspond to real time. Each table may have only one
timestamp variable
sql_variant Stores up to 8,000 bytes of data of various data types, except text, ntext,
and timestamp
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
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:
The subquery (inner query) executes once before the main query (outer
query) executes.
The main query (outer query) use the subquery result.
> CHANGE, EVERYDAY
Select Subquery
SQL Subqueries Example :
We have the following two tables 'student' and 'marks' with
common field 'StudentID'.
student marks
Query results:
Query Results:
Query Results: