You are on page 1of 31

DATABASE

BY: MEDINA , RAFAEL S.


OBJECTIVES:
At the end of the discussion, you will be able:

1. Familiarized the syntax used for manipulating


the database using SQL .

2. Use the syntax to SQL for actual application.

From the book of: Introduction to Database


by: Pepito P. Copernicus
.
Microsoft SQL Server
- Is aimed to be the database platform for the
next generation of connected, scalable, and reliable
enterprise level application.
- It offers a mix of excellent performance,
reliability, ease of administration, and new
architectural options which open to new possibilities
for designing more scalable and powerful system,
not to mention high availability and more secure
database engine.
Data Types of SQL
Every column in our table has a data type. The data type of a
column specifies what type of data or value we can store in that
particular column, and what kind of operation we can perform on those
data or values stored.

1. Integer Data Type


- Refers to the whole number or a number that has no decimal
point.
a. INT – uses 4bytes storage and can be used to store
numbers from -2^31 to 2^31 -1.
b. SMALLINT – uses 2 bytes of storage and can store
numbers from -2^15 to 2^15 – 1
c. TINYINT – uses 1byte of storage and can store
numbers from 0 to 255.
d. BIGINT - uses 8bytes of storage and can store numbers
from -2^63 to 2^63 - 1.
e. MONEY - uses 8bytes of storage.
f. SMALLMONEY - uses 4bytes of storage.
2. Decimal Data Types
- refers to a number with decimal point.

a. Precision - refers to the total number of digits


stored.
b. Scale - is the maximum number of digits to the
right of the decimal point.

c. Decimal - uses 217 bytes for storage, however,


the storage size varies depending on
the specified precision.
d. Float - uses 8 bytes for storage and has precision
of 15 digits.
e. Real - uses 4bytes for storage and has a precision
of 7 digits.
3. Character Data Types
- refers to any combination of letter, numbers and
symbols.

a. VARCHAR (n) – is a variable length single –


byte character strings that can also be
used to store up to 8000 bytes of data.

b. CHAR (n) - is a fixed length single-byte


character strings that can be used to store up
to 8000 bytes of data.

c. Text - is also a variable length single byte


character strings but may be used to store
more than 8000 bytes of data.
4. Date and Time Data Types
- Is used for storing date and time data.

a. DATETIME – uses 8bytes for storage


b. SMALLDATETIME - uses 4 bytes for storage.

5. Miscellaneous Data Types

a. IMAGE - is used to store pictures or image which is


in binary format.
b. BIT - is an integer data type which can store only 1
or 0 and can consume only a single bit of
storage space.
c. XML - is used to store and handle XML data.
Default System Databases

a. Master default system database.


- Is composed of system tables that keep track of server
installation as a whole and all other databases that we
subsequently created.

b. Model default system database


- Is a template database.

c. MSDB default database system


- Contains the metadata and database objects used by
the SQL server agent that performs scheduled activities.

d. Tempdb default database system


- Is a temporary database or workspace.
Query Editor
- Is the most important component of the SQL Server
Management Studio. All our database queries will be typed
and done in the query editor.

Selecting Database we want, we can also activate our


database through hardcoding it.

use Database1; where the Database1 is the name of


the database we want to activate.

Creating Table – we need to create table because the data


is stored into tables that are created in a database.
Syntax:
CREATE TABLE tablename(column_name type,
column_name type……)
Example: Employee1 (table name)

Fields names Data Types Char Length

EmployeeNumber int
Name varchar 15
Salary smallmoney
Address varchar 20

CREATE TABLE Employee1 (EmployeeNumber int, Name


varchar(15), Salary smallmoney, Address varchar (20))

note: after the creation, click the Execute button to build


successfully.
SQL Data Manipulation Language (DML)

DML - is the SQL construct and commands to manipulate the


data in the table.

Common DML commands:

1. INSERT - append(add) rows of data into a table.


2. UPDATE – edit(modify or change) data in the table.
3. DELETE - erase rows of data from a table.
4. SELECT - retrieve rows of data from a table.

Inputting Data Into Tables

1. INSERT INTO…VALUES
2. INSERT INTO…SELECT
INSERT INTO…VALUES – options needs the column list and all
the columns in the correct order.
Syntax:
INSERT INTO TableName VALUES
(‘char_attrib_val’,’num_attrib_val’)
Where:
char_attrib_val – short term for character attribute value.
num_attrib_val – is a short term for numeric attribute
value.
Example:
insert into Name1 values (‘Juan Cruz’); //then press !execute icon

Name1 – is the name of the table where we insert Juan Cruz.

To view the data on the table after adding ‘Juan Cruz’

Note: type this command after the inserting data to the table:
select * from Name1 (Name1 is the name of the table)
Example2. Input values into the Emplyee1 table, with the following
data:

EmployeeNumber Name Salary Address

1 Juan Tamad 20500 Imus Cavite


Syntax:

insert into Employee1 values (001, ‘Juan Tamad’, 20500,


‘imus cavite’);
select * from Employee1;

or we use:
insert into Employee1 (Name, Address, EmployeeNumber,Salary)
values (‘Juan Tamad’, ‘imus cavite’, 001, 20500);

select * from Employee1


Displaying Specific Columns From A Table

select Name from Employee1 ;

results:

select Name, Salary from Employee1;

result:
select * from Employee1 ;

result:
SQL UPDATE Command
•Is used to set or change data values in a table. We usually change
or set data in more than one row.
Syntax:
UPDATE tablename SET fieldname1, fieldname2,
fieldname3…..
update Employee1 Set Salary = 0;
select * from Employee1 ;

result:
update Employee1 Set Salary = 25000 where

Name = 'Juan Cruise';


select * from Employee1;

result:
update Employee1 Set Salary = 12500
where EmployeeNmber = 4808;
select * from Employee1;

result:
SQL ALTER TABLE Command
- Command when we add, modify(change), and delete columns in a
table`s definition. This command belong to the SQL`s DLL (Data Definition
Language) group of commands, because it is used in changing the definition table.

Adding a Column in a Table


Syntax: alter table tablename add column name datatype

Example:
alter table Employee1 add Position
varchar(20);
select * from Employee1 ;
result:
Changing a Column`s Data Type
- We are allowed to change the data type of an existing
column, provided that the new column data type can
accommodate the existing data. Meaning, that the new data
type is bigger or larger than the older one.
Syntax: alter table tablename
alter column columnname newdatatype

Example: alter table Employee1


alter column Salary float

From: To:
alter table Employee1
alter column Name varchar(50)

result:
Deleting a Column
Syntax: alter table tablename
drop column columnname
Example:
alter table Employee1
drop column Gender;
From :
or

To:
Example2: Delete all employees whose salary is greater than
20,000 that can be found in the Employee1 table.

From:

Syntax: delete from Employee1


where Salary>20000;
select * from Employee1 ;
To:
Deleting a Table

Syntax: drop table tablename;

Example: drop table Employee2;

From:

To:
Creating Comment
- Adding a comments in SQL statement is simple and
easy.

Two ways to include comments in SQL

1. – dashes
2. /* … */

Example:
Select Command
- A command used to retrieve the data from a database.

4 basic part of select command:


a. select from
b. select where
c. select order by

Example: select * from Employee1 ;where

Employee1 is name of the table.

Result:
Select Application With Columns Specified

Syntax: select Name, Salary from Employee1;

Result:

The Where Command


Example: select * from Employee1
where Salary = 12500;
result:
Example2.:
From:

Syntax: select * from Employee1


where Salary >=23000;
result:
Where Application with Logical OR/AND

Syntax: select * from Employee1


where Salary <=23000 and Name =
'jeewan doctor' ;
result:

Example2:
Syntax: select * from Employee1 where
EmployeeNmber = 1234
or Salary >= 20000;
result:
Thank you!!!

You might also like