Ulangkaji - DB: 1) Create Database Query Analyzer
Ulangkaji - DB: 1) Create Database Query Analyzer
1) CREATE DATABASE
Query analyzer:
Contoh:
- the name and location of the database's data file and transaction log.
- the initial size of these files (with the SIZE argument),
USE master
GO
CREATE DATABASE Payroll
ON
( NAME = Payroll_dat,
FILENAME = 'c:\program files\microsoft sql server\mssql\data\payrolldat.mdf',
SIZE = 20MB,
MAXSIZE = 70MB,
FILEGROWTH = 5MB )
LOG ON
( NAME = 'Payroll_log',
FILENAME = 'c:\program files\microsoft sql server\mssql\data\payroll.ldf',
SIZE = 10MB,
MAXSIZE = 40MB,
FILEGROWTH = 5MB )
GO
2) CREATE TABLE
Query analyzer
Contoh:
3) INSERT DATA
Contoh :
:
4) SELECT and WHERE
atau
SELECT *
FROM Table1
Atau Where
SELECT *
FROM Customers
WHERE LastName = 'Smith'
SELECT *
FROM Customers
WHERE LastName <> 'Smith'
SELECT *
FROM Customers
WHERE DOB >= '1/1/1970'
SELECT *
FROM Customers
WHERE DOB < '1/1/1970'
SELECT *
FROM Customers
WHERE DOB =< '1/1/1970'
SELECT *
FROM Customers
WHERE Phone LIKE '626%'
Note the LIKE syntax is different with the different RDBMS (SQL Server syntax
used above). Check the SQL LIKE article for more details.
SELECT *
FROM Customers
WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975'
5) JOIN
Contoh:
Customers:
Sales:
As you can see those 2 tables have common field called CustomerID and
thanks to that we can extract information from both tables by matching their
CustomerID columns.
Join Types
INNER JOIN: This will only return rows when there is at least one row in
both tables that match the join condition.
LEFT OUTER JOIN (or LEFT JOIN): This will return rows that have data in
the left table (left of the JOIN keyword), even if there's no matching rows in
the right table.
RIGHT OUTER JOIN (or RIGHT JOIN): This will return rows that have
data in the right table (right of the JOIN keyword), even if there's no
matching rows in the left table.
FULL OUTER JOIN (or FULL JOIN): This will return all rows, as long as
there's matching data in one of the tables.
Join Syntax
Inner Join:
SELECT * FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name = table_name2.column_name
Left Join:
SELECT * FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name = table_name2.column_name
Right Join:
SELECT * FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name = table_name2.column_name
Full Join:
SELECT * FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name = table_name2.column_name
Source Tables
Left Table
Right Table
IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator
Result
Individual FirstNa LastNa UserNa Individual AccessLev
Id me me me Id el
2 Homer Simpson homey 2 Contributor
Use this when you only want to return rows that have matching data in the left
table, even if there's no matching rows in the right table.
Example SQL statement
SELECT * FROM Individual AS Ind
LEFT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Source Tables
Left Table
Right Table
IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator
Result
Individual FirstNa LastNa UserNam Individual AccessLe
Id me me e Id vel
Administrat
1 Fred Flinstone freddo 1
or
2 Homer Simpson homey 2 Contributor
notsofamo
3 Homer Brown 3 Contributor
us
Osbourn
4 Ozzy sabbath 4 Contributor
e
noplacelik
5 Homer Gain NULL NULL
e
Use this when you only want to return rows that have matching data in the right
table, even if there's no matching rows in the left table.
Example SQL statement
SELECT * FROM Individual AS Ind
RIGHT JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Source Tables
Left Table
Right Table
IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator
Result
Individual FirstNa LastNa UserNam Individual AccessLe
Id me me e Id vel
Administrat
1 Fred Flinstone freddo 1
or
2 Homer Simpson homey 2 Contributor
notsofamo
3 Homer Brown 3 Contributor
us
Osbourn
4 Ozzy sabbath 4 Contributor
e
Administrat
NULL NULL NULL NULL 10
or
Use this when you want to all rows, even if there's no matching rows in the right
table.
Example SQL statement
SELECT * FROM Individual AS Ind
FULL JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId
Source Tables
Left Table
Right Table
IndividualId AccessLevel
1 Administrator
2 Contributor
3 Contributor
4 Contributor
10 Administrator
Result
Individual FirstNa LastNa UserNam Individual AccessLe
Id me me e Id vel
Administrat
1 Fred Flinstone freddo 1
or
2 Homer Simpson homey 2 Contributor
notsofamo
3 Homer Brown 3 Contributor
us
Osbourn
4 Ozzy sabbath 4 Contributor
e
noplacelik
5 Homer Gain NULL NULL
e
Administrat
NULL NULL NULL NULL 10
or