You are on page 1of 14

Create database

CREATE DATABASE databasename;


DROP DATABASE databasename;
USE databasename;

1. create database batch3;


use batch3;

Datatype in sql
varchar(length) - varchar(255) -
char(lenght) – fixed length – place with space
int
double(total number of digit, total number of decimal point)
Boolean – true or false
Date – yyyy/mm/dd

Naming convention
- identifier must start with a letter (eg: Student)
-identifier cannot contain space. (instead use underscore)
-no longer than 128 character
-can contain letter, number, underscore

SQL statement
-Reserved word – UPPER CASE
-User defined word – lower case
-statement terminator (;)

Reserved word
-SELECT, UPDATE, DELETE, INSERT
-SELECT – to query data in the database
-UPDATE – to update data in a table
-DELETE – to delete data from the table
-INSERT – to insert data into a table
User defined word
-made by user
-tablename, columnname

Naming convention
Table name – first letter Uppercase letter
Column name – first letter lowercase – the other word first letter UPPER CASE

Create these three table

PropertyForRent

propertyNo city Type room rent ownerNo staffNo branchNo


14 Aberdan House 6 650 46 9 B007
94 London Flat 4 400 87 41 B005
4 Glasgow Flat 3 350 40 B003
36 Glasgow Flat 3 375 93 37 B003
21 Glasgow House 5 600 87 37 B003
16 Glasgow Flat 4 450 93 14 B003
Creating table
CREATE TABLE tablename(
columnname1 datatype,
columnname2 datatype,
columnname3 datatype
);

Other usage in creating table


DATATYPE
NOT NULL
AUTO INCREMENT // rollno int auto increment not null
UNIQUE // nrc varchar(10) unique
PRIMARY KEY //
FOREIGN KEY
Gender varchar CHECK (gender = “male” or gender = “female”)
Age int check(age >= 16 and age < 30)
School varchar(10) DEFAULT “NiT”

1. CREATE TABLE Branch(


branchNo varchar(255),
street varchar(255),
city varchar(255),
postCode varchar(255)
);

Change sex - gender


2. CREATE TABLE Staff(
staffNo varchar(255),
fName varchar(255),
lName varchar(255),
position varchar(255),
gender varchar(255),
DOB date,
salary int,
branchNo varchar(255)
);

3. Adding Primary key into table


PRIMARY KEY (columname);

4. Adding Foreign key into table


FOREIGN KEY(columnname) REFERENCES parenttablename(columnname);

5. Rename Existing Table


ALTER TABLE
oldtablename RENAME newtablename;

6. Rename Multiple Table At Once


RENAME TABLE
oldtablename1 TO newtablename1,
oldtablename2 TO newtablename2,
oldtablename3 TO newtablename3;

Inserting data into table


INSERT INTO tablename (columnname1, columnname2, columnname3) VALUES (value1, value2,
value3);

1. INSERT INTO Branch (branchNo, street, city, postCode) VALUES ('b005', '22 Deer Rd',
'London', 'SW1 4EH');

2. INSERT INTO Branch (branchNo, city, street, postCode) VALUES ('b007', 'Aberdeen','16 Argyll
St', ‘AB2 3SU’);

Insert data into table without Columnname


-number of insert value must match number of column count of the table
1. INSERT INTO Branch VALUES ('b005', '22 Deer Rd', 'London', 'SW1 4EH');
Insert multiple row at once
1. INSERT INTO Branch VALUES ('b005', '22 Deer Rd', 'London', 'SW1 4EH'),
(‘B007’, ’16 Argyll St’, ‘Aberdeen’, ‘AB2 3SU’),
(‘B003’, ‘163 Main St’, ‘Glasgow’, ‘G11 9QX’);

// Only 3 column data are added at once insert statement


2. INSERT INTO Branch (branchNo, street, city) VALUES ('B005', 'London', 'Deer Road'),
('B007', 'Aberdeen', 'Argy Street'),
('B003', 'Glasgow', 'Main Street'),
('B004', 'Bristol', 'Manse Road'),
('B002', 'London', 'Clover Dear');

Chang Column Datatype


1. ALTER TABLE tablename
MODIFY COLUMN columnname datatype;

Change Column Order


1. ALTER TABLE tablename
MODIFY columnname datatype
AFTER columnname;

Eg: alter table test


modify studentname varchar(255)
after gender;

Add New Column into Existing Table


1. ALTER TABLE tablename
ADD columnname datatype;

2. ALTER TABLE tablename


ADD columnname datatype CHECK(columnname = value);
(exception – existing column cannot be added CHECK constraint)
(if you want to add – drop the existing column first and then add column with CHECK constraint)
(default, not null, check, auto increment, unique)
Add new COLUMN + PK into Existing Table
ALTER TABLE tablename
ADD columnname datatype PRIMARY KEY;

Add Primary Key into Existing Column


1. ALTER TABLE tablename
ADD PRIMARY KEY (keycolumnname);

Eg: alter table test


add primary key(id);

Drop Primary Key from Existing Table


1. ALTER TABLE tablename
DROP PRIMARY KEY;

Eg: alter table test


drop primary key;

Add new COLUMN + (all constraint) into Existing Table


1. ALTER TABLE tablename
ADD columnname datatype PRIMARY KEY NOT NULL AUTO_INCREMENT;

Add New COLUMN into Existing Table with Default value


1. ALTER TABLE tablename
ADD columnname datatype DEFAULT value;

Eg: alter table test


add room int default 4;

SET DEFAULT into Existing Column


1. ALTER TABLE tablename
ALTER COLUMN columnname SET DEFAULT value;
Eg: alter table test
alter column gender set default 'M';

DROP DEFAULT from Existing Table


1. ALTER TABLE tablename
ALTER COLUMN gender DROP DEFAULT;

Eg: alter table test


alter column gender drop default;

Rename COLUMN
1. ALTER TABLE tablename
RENAME COLUMN oldcolumnname TO newcolumnname;

Eg: alter table test


rename column name to studentname;

Drop table
DROP TABLE tablename;

Delete a Column from Existing Table


1. ALTER TABLE tablename
DROP COLUMN columnname;

Add Foreign Key into Existing Table (simple)


1. ALTER TABLE tablename
ADD FOREIGN KEY(columnname) REFERENCES parenttablename(keycolumnname);

Eg: alter table test2


add foreign key(testid) references test(id);
Add Foreign Key into Existing Table (With Constraint Name)
ALTER TABLE tablename
ADD CONSTRAINT constraintname
FOREIGN KEY(columnname) REFERENCES parenttablename(keycolumnname);

Eg: alter table test2


add constraint FK_test2
foreign key(testid) references test(id);

Add Foreign Key into Existing Table (with Constraint Name + Referential Constraint)
ALTER TABLE tablename
ADD CONSTRAINT constraintname
FOREIGN KEY(columnname) REFERENCES parenttablename(keycolumnname) ON DELETE
SET NULL;

Referential Action
-ON DELETE SET NULL
-ON DELETE CASCADE
-ON DELETE NO ACTION (language default setting)
-ON DELETE SET DEFAULT

-ON UPDATE SET NULL


-ON UPDATE CASCADE
-ON UPDATE NO ACTION (language default setting)
-ON UPDATE SET DEFAULT

Eg: alter table test2


add constraint FK_test2
foreign key(testid) references test(id) on delete set null;
Drop foreign key from existing table (need constraint name)
ALTER TABLE tablename
DROP CONSTRAINT constraintname;

Eg: alter table test2


drop constraint FK_test2;

SQL Query ORDER


-to retrieve data from one or more table
SELECT - specify which column to appear in output
FROM – specify the table or tables
WHERE – condition (filter the row which match the specify condition)
GROUP BY - group the rows which match the column value
HAVING -
ORDER BY – order the output

GroupBy (from CH5 textbook print)-the last page note


1. select city, count(*) As No_Of_City
From branch;

2. select city
From branch
Group by city;

3. select city, count(*) As No_Of_City


from branch
group by city;

4. select city, count(*) As No_Of_City


from branch
group by city
having count(*) > 1;
5. select branchNo
From staff;

6. select branchNo
From staff
Group by branchNo;

7. select branchNo, count(*)


From staff
Group by branchNo;

8. select branchNo, count(*)


From staff
Group by branchNo
Having count(*) > 1;

9. select position
From staff;

10. select position


From staff
Group by position;

11. select position, count(*)


From staff
Group by position;

12. select position, count(*)


From staff
Group by position
Having count(*) > 1 ;

13. select gender


From staff;

14. select gender


From staff
Group by gender;

15. select gender, count(*)


From staff
Group by gender;

16. select gender, count(*)


From staff
Group by gender
Having count(*) > 1;

1. Show number of staff for each position


2. Show number of staff for each gender
3. Show number of staff for each branch
4. Show number of branch for each city

Union, Intersect, Except


PurchaseTableA
customerCode customerName repCode
1311 Aye Mon Thi C01
2051 Thiri Soe A12
4293 Thurein B30
1806 Min Myat A11
7745 Lin Lin D04

PurchaseTableB
customerCode customerName repCode
2051 Thiri Soe A12
4293 Thurein B30
5018 Myo Myo A11
Check Constraint
No_of_Room no more than 15
Salary 10000 – 50000
branchNo B001 – B999
gender M or F

create table student(


id int,
name varchar(255),
gender varchar(255) check (gender = 'M' or 'F')
);

With default + check


create table student(
id int,
name varchar(255),
gender varchar(255) default 'M' check (gender = 'M' or 'F')
);
create table student(
id int,
name varchar(255),
gender varchar(255) default 'M' check(gender = 'M' or gender = 'F')
);
gender char default ‘M’
gender char check (gender = ‘M’ or gender = ‘F’)
room int check (room >= 1 and room <= 15)

drop column
alter table tablename
drop column columnname;

In
Between
Operator (logical and comparison)
Distinct eg and defn
Null (eg)
Aggregate function (select, having) eg
Temporary column name, tablename
Check (in, between)

Check constraint in Q1
Grant – in Q1
Revoke
Create Domain
Create View

Update
Delete
Join (inner, left, right)
Union
Intersect
Except

Selection and Projection


Concurrency Control
Dead lock
Exclusive control
Shared lock
Datamining
Data warehouses

You might also like