Final Module No. 1
Final Module No. 1
Data within the most common types of databases in operation today is typically modeled in
rows and columns in a series of tables to make processing and data querying efficient. The data
can then be easily accessed, managed, modified, updated, controlled, and organized. Most
databases use structured query language (SQL) for writing and querying data
SQL is a programming language used by nearly all relational databases to query, manipulate, and
define data, and to provide access control. SQL was first developed at IBM in the 1970s, which led
to the implementation of the SQL ANSI standard, SQL has spurred many extensions from
companies such as IBM, Oracle, and Microsoft. Although SQL is still widely used today, new
programming languages are beginning to appear.
1
https://www.oracle.com/in/database/what-is-database/
2
Ibid.
1|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
Even if SQL is standard, many of the database systems that exist today implement their own
version of the SQL language. In this module, we are going to use Microsoft SQL Server or SQL
Server as our database tool.
1. Data Definition Language (DDL). Manages table and index structure. The most basic items of
DDL are the create, alter, rename and drop statements or script.
1.1. CREATE—creates an object such as Database and tables in the database server
1.2. DROP—deletes an object in the database, usually irretrievable
1.3. ALTER—modifies the structure of an existing object or table in various ways such as:
adding columns, addling constraints, changing data types, and dropping of constraints.
2. Data Manipulation Language (DML). It is the subset of SQL used to create, retrieve, update,
and delete data or know as (CRUD).
The acronym CRUD refers to all of the major functions that need to be implemented in a
relational database application to consider It complete.
1.1.1 Create New Database
It is quite simple to create a new database in Microsoft SQL Server especially if you’re
going to develop Accounting Information System. There are two (2) methods can be
used.
Method 1
Click Database Mode
Click New Database
Type Database Name
Click Add button
2|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
Here is an example:
Note: In relational database all tables have one or more relation with each other using the
primary key (PK) and foreign key (FK). Under rule and principle, a table must have only one
Primary Key, but you may indicate more than one Foreign Key
3|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
To CREATE TABLE in the database, example the two tables presented above—[Account
Classification] and [Sub Classification] tables, used the syntax below:
The data type specifies what type of data the column or field can hold. The data types
as follows:
int
bigint
decimal(n,n)
numeric(n,n)
varchar(n)
nvarchar(n)
text()
date
datetime
bit
image
Figure A
The above SQL script illustrates the use of “CREATE TABLE” followed by table name “[Sub
Classification]”. Inside the open and close parenthesis are the field name, field types and
constraints. The above script identifies
two special type of fields that serve as
Primary Key (PK) and Foreign Key (FK)
which will be used to relate or join the
said table to another table. The result
of the said script is shown on the figure
to the right side.
Figure B
4|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
A primary key is used to ensure data in the specific column or fields is unique. It is a
column or field cannot have a null value. It is either an existing table column or field that
is specifically generated by the database according to a defined sequence. 3
The Primary Key constraint uniquely identifies each record in a table. A table can have
only one primary key, and in the table, this primary key can consist of single or multiple
columns (fields).4
The illustration of the Primary key is seen in Figure A—which shows how it was used in the
script or command and Figure B—shows the Primary Key in the table structure.
A Foreign Key (FK) is a column or combination of columns (fields) that is used to establish
and enforce a link between the data in two tables to control the data the can be stored
in the foreign key. In a foreign key reference, a link is created between two tables when
the column or columns that hold the primary key value for one table are referenced by
the column or columns in another table. This column becomes a foreign key in the second
table.
A Foreign Key is a field (or collection of fields or columns) in one table that refers to the
PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table with the primary key
is called the referenced or parent table.
The illustration of the Foreign key (FK) is seen in Figure A—which shows how it was used in
the script or command and Figure B—shows the Foreign Key in the table structure.
How to add PRIMARY KEY especially if there is no primary key was explicitly
defined at table creation.
Figure C
Figure D
3
https://www.geeksforgeeks.org/difference-between-primary-key-and-foreign-key/
4
https://www.w3schools.com/sql/sql_primarykey.asp
5|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
How to change the data type of column or field “[Sub Classification]” from
nvarchar to varchar?
Figure E
The above script demonstrates the alteration of nvarchar data type to varchar data
type.
Figure F
Figure G
Figure H
Figure I
6|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
The Data Manipulation language of Microsoft SQL Server composed of four (4) major
commands or scripts known as CRUD—CREATE, RETREIVE, UPDATE and DELETE.
2.1.1 CREATE
The SQL Server INSERT statement is used to insert a single record or multiple
record(s) into a table in SQL Server.
Syntax:
In its simplest form, the syntax for the INSERT statement when inserting
a single record using the VALUES keyword in Server is:
Figure J
Example:
Figure K
The above scripts are the same. It demonstrates how INSERT INTO [TABLENAME](…) and
keyword VALUES(…) written and executed in the SQL Server Query window where the said
statement can be written in multiple lines and single line. Remember that in Figure B, Field [Code]
and [SCode] are both “int” data type so the values 1 and 10 are not enclosed with single quotation
(‘). Only nvarchar, varchar and date data types are required to enclose with single quotation (‘)
just like the example written above.
Another example of CREATE is the combination of INSERT INTO and SELECT() known as
RETREIVE command in SQL Language.
This command is applicable only when you are going to insert records from one table to
another.
7|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
Example:
The script presented below shows how to insert records from table [Sub Classification] to
[SubClassification] Table using both INSERT INTO and SELECT. The 10 records from [Sub
Classification] table will be copied to [SubClassification] Table in just one command or statement.
Figure L
2.1.2 Retrieve
The retrieve command of SQL Server was implemented through SELECT
statement. The select statement can retrieve record from one or more table
using the JOIN statement and WHERE clause.
The SELECT statement is used to retrieve data from a database. The data
returned is stored in a result table, called the result-set.
Example 1
Figure 1.1
8|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
Example 2
Figure 1.2
Example 3
Figure 1.3
Example 4
9|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD
Example 5
Figure 1.5a
Our next module will be the application of JOIN and other DML commands.
2.1.3
10 | P a g e