0% found this document useful (0 votes)
58 views

Ulangkaji - DB: 1) Create Database Query Analyzer

The document discusses various SQL commands used to create and manage databases and tables. It covers creating databases and specifying file properties, creating tables and defining column data types, inserting data into tables, selecting data with WHERE clauses and comparisons, joining tables to combine data, and using different join types like inner, left, right and full outer joins.

Uploaded by

ircmatrixx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Ulangkaji - DB: 1) Create Database Query Analyzer

The document discusses various SQL commands used to create and manage databases and tables. It covers creating databases and specifying file properties, creating tables and defining column data types, inserting data into tables, selecting data with WHERE clauses and comparisons, joining tables to combine data, and using different join types like inner, left, right and full outer joins.

Uploaded by

ircmatrixx
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

ULANGKAJI - DB

1) CREATE DATABASE

Query analyzer:

CREATE DATABASE database_name

Contoh:

CREATE DATABASE Payroll

Contoh create database berpandukan PROPERTIES yg dikehendaki:

- the name and location of the database's data file and transaction log.
- the initial size of these files (with the SIZE argument),

- the maximum size it can grow to (with the MAXSIZE argument)

- the growth increment of each file (using the FILEGROWTH) 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

create table "tablename"


("column1" "data type",
"column2" "data type",
"column3" "data type");

Contoh:

create table employee


(first varchar(15),
last varchar(20),
age number(3),
address varchar(30),
city varchar(20),
state varchar(20));

3) INSERT DATA

INSERT INTO Table1


VALUES (value1, value2, value3…)

Contoh :

INSERT INTO Customers


VALUES ('Peter', 'Hunt', 'peter.hunt@tgmail.net', '1/1/1974', '626 888-8888')

INSERT INTO Customers (FirstName, LastName, Email, DOB, Phone)


VALUES ('Peter', 'Hunt', 'peter.hunt@tgmail.net', '1/1/1974', '626 888-8888')

:
4) SELECT and WHERE

Please have a look at the general SQL SELECT syntax:

SELECT Column1, Column2, Column3,


FROM Table1

atau

SELECT *
FROM Table1

Atau Where

SELECT *
FROM Customers
WHERE LastName = 'Smith'

The result of the SQL expression above will be the following:

FirstName LastName Email DOB Phone


John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

Penggunaan comparison and logic:

<> (Not Equal)

SELECT *
FROM Customers
WHERE LastName <> 'Smith'

> (Greater than)


SELECT *
FROM Customers
WHERE DOB > '1/1/1970'

>= (Greater or Equal)

SELECT *
FROM Customers
WHERE DOB >= '1/1/1970'

< (Less than)

SELECT *
FROM Customers
WHERE DOB < '1/1/1970'

<= (Less or Equal)

SELECT *
FROM Customers
WHERE DOB =< '1/1/1970'

LIKE (similar to)

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.

Between (Defines a range)

SELECT *
FROM Customers
WHERE DOB BETWEEN '1/1/1970' AND '1/1/1975'
5) JOIN

Gabungan 2 table berdasarkan column/field tertentu (field yg sama properties n


tajuk):

Contoh:

Customers:

CustomerID FirstName LastName Email DOB Phone


626
John.Smith@yahoo.co
1 John Smith 2/4/1968 222-
m
2222
323
2 Steven Goldfish goldfish@fishhere.net 4/4/1974 455-
4545
416
3 Paula Brown pb@herowndomain.org 5/24/1978 323-
3232
416
4 James Smith jim@supergig.co.uk 20/10/1980 323-
8888

Sales:

CustomerID Date SaleAmount


2 5/6/2004 $100.22
1 5/7/2004 $99.95
3 5/7/2004 $122.95
3 5/13/2004 $100.00
4 5/22/2004 $555.55

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.

Contoh menggunakan JOIN:

SELECT Customers.FirstName, Customers.LastName,


SUM(Sales.SaleAmount) AS SalesPerCustomer
FROM Customers JOIN Sales
ON Customers.CustomerID = Sales.CustomerID
GROUP BY Customers.FirstName, Customers.LastName

Join Types

Depending on your requirements, you can do an "inner" join or an "outer" join.


These are different in a subtle way

 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

Example Inner Join Statement

SELECT * FROM Individual


INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
Note: We could use table aliases instead of the full table name. This will keep
our statement shorter. For example:

SELECT * FROM Individual AS Ind


INNER JOIN Publisher AS Pub
ON Ind.IndividualId = Pub.IndividualId

Example SQL statement


SELECT * FROM Individual
INNER JOIN Publisher
ON Individual.IndividualId = Publisher.IndividualId
WHERE Individual.IndividualId = '2'

Source Tables

Left Table

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike

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

Next lesson covers the SQL OUTER JOIN.

Left Outer Join

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

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike

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

Right Outer Join

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

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike

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

Full Outer Join

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

Id FirstName LastName UserName


1 Fred Flinstone freddo
2 Homer Simpson homey
3 Homer Brown notsofamous
4 Ozzy Ozzbourne sabbath
5 Homer Gain noplacelike

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

You might also like