Professional Documents
Culture Documents
Example
1. Simple statement without any constraint
c. Create index
Note* :
1. a column which is set as Primary Key is
already indexed by default
2. index should be used on column which he
frequently queried on
3. avoid using index on columns which have
large amount of null values
4. index should be avoided on small tables
Stx :
1) To create an index which will have duplicate
values in the column_name
Example
1. Index on one column
- Alter
a. ALTER TABLE statement is used to add, delete,
or modify columns in an existing table
b. ALTER TABLE is also used to add and drop
various constraints on an existing table
c. Syntax
1. Add column
2. Drop column
d. Example
Db : Persons
ID LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Add column
Now we want to add a column named “DateOfBirth” in
the “Persons” table
Db : Persons
ID LastName FirstName Address City DateOfBirth
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Db : Persons
ID LastName FirstName Address City DateOfBirth
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
Db : Persons
ID LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
- Drop
1. Drop database
a. Drop database will delete the complete
database along with its components
b. You need to have admin privilege to perform
this action
c. To verify if the database is deleted you can
run the command SHOW DATABASE
d. Syntax :
e. Example
2. Drop table
1) Remove the table from the database
a. Drop table is used when we want to
remove the table from the database
b. All the table’s rows, indexes and
privileges will also be removed
c. No DML (Data Manipulation Language)
triggers will be fired
d. The operation cannont be rolled back
e. Syntax
3. Drop index
a. Index can also be dropped using alter table
statement
b. Syntax
c. Example
- Miscellaneous
1. Table constraints
a. To preserve the consistency and correctness
of the stored data, a relational DBMS imposes
one or more data integrity constraints. These
constraints restrict the data values that can be
inserted into the database or created by a
database update. Different types of data
integrity constraints are:
1) NOT NULL – implies that the column
cannot store null values
Adding NOT NULL using create table
statement
Syntax
Example
(checkpoint)2
https://www.w3schools.com/sql/sql_datatypes.asp
No DataType Description
Fixed – length character
1 CHAR(len)
strings
2 CHARACTER(len)
Variable – length
3 VARCHAR(len)
character strings
4 CHAR VARYING(len)
CHARACTER
5
VARYING(len)
Fixed – length national
6 NCHAR(len)
character strings
7 NATIONAL CHAR(len)
NATIONAL
8
CHARACTER(len)
Variable – length
9 NCHAR VARYING(len)
national character strings
NATIONAL CHAR
10
VARYING(len)
NATIONAL
11 CHARACTER
VARYING(len)
12 INTEGER INT Integer numbers
13 SMALL INT Small integer numbers
14 BIT(len) Fixed – length bit strings
Variable – length bit
15 BIT VARYING(len)
strings
NUMERIC(precision,
16 Decimal numbers
scale)
17 DECIMAL(precision, scale)
18 DEC(precision, scale)
19 FLOAT(precision) Floating point numbers
Low – precision floating
20 REAL
point numbers
High – precision floating
21 DOUBLE PRECISION
point numbers
22 DATE Calendar dates
23 TIME(precision) Clock times
24 TIMESTAMP(precision) Dates and times
25 INTERVAL Time intervals
Example
CREATE TABLE Employee(
ID INT,
NAME VARCHAR(20),
AGE INT,
ADDRESS CHAR(25),
SALARY DECIMAL(18, 2),
date_of_birth DATE
);
Output :
Db : Employee
ID NAME AGE ADDRESS SALARY date_of_birth
1 Achmad 25 Taman 27.000.000 1997-04-05
ubud
2 Nafisah 23 Bogor 6.500.000 1998-12-13
3. Views