KingKong
2012-11-07 21:01:43
Database tutorial - Part 1
Date: 08/NOV/2012
Index:
Basics
Constraints
SQL DDL
SQL DML
Queries and Aggregate functions
Date: 08/NOV/2012
Index:
Basics
Constraints
SQL DDL
SQL DML
Queries and Aggregate functions
2012-11-07 21:01:43
2012-11-07 21:03:02
type
TEmployee= record
ID: Integer;
Name: String;
Age: Integer; 2012-11-07 21:04:06
2012-11-07 21:05:12
Create Table <table-name> (
<Column-name1> <data-type> [(max length)] [constraint],
<Column-name2> <data-type> [(max length)] [constraint],
--…add as many columns as you need
[Constraint <constraint-name> constraint type,]
[Foreign Key (<column-name>) References (<table-name>)] );
NULL. The value is a NULL value.
INTEGER. The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.
REAL. The value is a floating point value, stored as an 8-byte IEEE floating point number.
TEXT. The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).
BLOB. The value is a blob of data, stored exactly as it was input.
Create table Salary(
ID Text Primary Key,
Frequency Text ,
Amount Integer Constraint colamt_tabSalary Check(Amount >0));Create table Employee(
ID Integer Primary Key,
Name Text not null,
Age Integer ,
Salarytype Text,
Foreign Key (Salarytype) References Salary);Drop table <table-name>;ALTER TABLE <tablename>
[ ADD <column name> <type> [<length>] [null/not null] ]
[ MODIFY <column name> <type> [<length>] [null/not null] ]
[ DROP <column name> ]
[ ADD <constraint> ]
[ DROP <constraint> ];Alter table employee
add (ph_num Integer) not null;
2012-11-07 21:06:31
INSERT INTO <table-name> [(<column-name1> , <column-name2> , ... )]
VALUES ( <value1> , <value2> , ... );Insert into Salary(ID, Frequency, Amount) Values('TS1', 'Daily', 30);
Insert into Salary(ID, Frequency, Amount) Values('PG2', 'Weekly', 30);
Insert into Salary(ID, Frequency, Amount) Values('AW1', 'Fortnightly', 41);Insert Into Employee(ID, name, Age, SalaryType) Values(319, 'Fred Smith', 24, 'TS1');
Insert Into Employee(ID, name, Age, SalaryType) Values(476, 'Fred Smith', 24, 'TS1');
Insert Into Employee(ID, name, Age, SalaryType) Values(503, 'Jane Doe', 34, 'PG2');
Insert Into Employee(ID, name, Age, SalaryType) Values(543, 'John Pitt', 42, 'AW1');
Insert Into Employee(ID, name, Age, SalaryType) Values(677, 'Carol Song', 50, null);Select <item>
From <table-name>
[inner/left outer/right outer join <table-name2> on <condition>]
[Where <condition>]
[Order by <column> [asc/desc]];Select *
From Employee;Select ID, name
from employee
order by ID; --default is ascending orderSelect E.ID, E.name, S.Amount --this means get the column ID from table E
From Employee E --we can now refer to table Employee as E, so we have to type less :D
Inner join Salary S On E.SalaryType = S.ID
--what this does, is it joins the two tables based on the primary key-foreign key relationship and only returns rows whose foreign key has a corresponding value in the primary key table.
Order by 1; --order by the first column :DIf you actually execute that statement, you will realise that it only returns 4 rows. So what happened to the other row? Why didn't we get that too? Its because of the PK-FK relationship. Employee Carol Song had a null salary type, and therefore it did not have any corresponding value in the salary table. So how do we get Carol's information as well? Well for that, we need to use outer joins:Select E.ID, E.name, S.Amount
From Employee E
Left outer join Salary S On E.SalaryType = S.ID
--what this does, is it joins the two tables based on the primary key-foreign key relationship and it will also return every single row from the left table(Employee) even if it cannot find the primary key.
Order by 1; Select E.ID, E.name, S.Amount
From Salary S
Right outer join Employee E On E.SalaryType = S.ID
--in this case, the right outer join works on the right side of the table, which is Employee table.
Order by 1;
Delete from Employee
where name = 'Carol Song';
Insert Into Employee(ID, name, Age, SalaryType) Values(677, 'Carol Song', 50, TS1);
DELETE FROM <tablename>
[WHERE <condition>];
UPDATE <tablename>
SET <columnname1>=<value1>,
[<columnname2>=<value2>,]
--add as many columns as you want
[WHERE <condition>];
Update Employee
set Salarytype = 'TS1'
where name = 'Carol Song';
2012-11-07 21:07:53
Select Count(*) From Employees;
Select count(*) - count(salarytype) From employees;
Select *, 'young' from Employee where age = (select min(age) from employee)
union
Select *, 'old' from Employee where age = (select max(age) from employee);
select avg(age) from employee;
2012-11-07 21:10:29
2012-11-07 21:13:25
2012-11-07 21:18:25
From: Kasi
looking forward to reading this, but why didnt you do part one first and then make the reserve posts :(
2012-11-07 21:31:46
2012-11-08 20:27:32
2012-11-08 20:43:00
2012-11-08 22:18:41
From: Kasi
Repped :D, you sir have gained a spot on my bookmarks.
2012-11-08 22:39:53
From: KingKong
Thanks! And make sure to keep some free space in your bookmarks as i'm going to make an advanced database tutorial soon :)
2012-11-09 01:17:02
From: KingKong
Thanks! And make sure to keep some free space in your bookmarks as i'm going to make an advanced database tutorial soon :)