0% found this document useful (0 votes)
36 views10 pages

Final Module No. 1

The document discusses databases and database systems. It defines a database as a collection of related data that can be retrieved as needed. A database system includes the data, database management system (DBMS), and associated applications. Most databases today use a relational model with data organized into tables with rows and columns. Structured Query Language (SQL) is used to query, manipulate, and define data. SQL allows users to execute queries, retrieve data, insert/update/delete records, and create/alter database objects like tables and stored procedures. The document provides examples of using SQL data definition language (DDL) and data manipulation language (DML) to create tables, add/alter table columns and constraints, and more in a

Uploaded by

Rochelle Manayao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views10 pages

Final Module No. 1

The document discusses databases and database systems. It defines a database as a collection of related data that can be retrieved as needed. A database system includes the data, database management system (DBMS), and associated applications. Most databases today use a relational model with data organized into tables with rows and columns. Structured Query Language (SQL) is used to query, manipulate, and define data. SQL allows users to execute queries, retrieve data, insert/update/delete records, and create/alter database objects like tables and stored procedures. The document provides examples of using SQL data definition language (DDL) and data manipulation language (DML) to create tables, add/alter table columns and constraints, and more in a

Uploaded by

Rochelle Manayao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD

What is a Database and Database System?

 A collection of related data stored in manner that enables information to be retrieved as


needs.
 An organized collection of structured information, or data, typically stored electronically
in a computer system. It is usually controlled by a database management system (DBMS)1
 Together, the data and the DBMS, along with the application that are associated with
them, are referred to as a database system., often shortened to just database2

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

Typically, an electronic database consists of tables—collection of related records

A table composed of:

 Fields(Columns)—Single category of data to be stored in a database


 Records(Rows)—Collection of related fields in a database (example, all the fields in one
customer). This is also known as tuple.

Here is an example of a Table in Relational Database Management System.

What is Structured Query Language (SQL)?

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.

What can SQL do?

 Can execute queries against a database


 Can retrieve data from a database

1
https://www.oracle.com/in/database/what-is-database/
2
Ibid.

1|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD

 Can insert or create records in a database;


 Can update record in a database;
 Can delete records from a database
 Can create new databases
 Can create new tables in a database
 Can create stored procedures in a database
 Can set permission on tables, procedures and views.

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.

Two (2) Languages of SQL Server

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

Method No. 2—the use of the queries or script

1.1.2 Create Table


Before you start implementing your tables in the database, it is necessary to spend
some time design the tables properly using some of the design tools called data
modeling technique which we have previously discussed in the midterm period.

In this module, we will be using the ER-Diagram (Entity Relationship Diagram—ERD)


Database Design technique.

Here is an example:

Table Name: Account Classificaiton


Field Name Type & width Constraint
ID int not null
Code int not null, Primary Key
Classification nvarchar(100) null

Table Name: [Account Classificaiton]


Field Name Type & width Constraint
ID int not null
Code int not null, Foreign Key
SCode int not null, Primary Key
Sub Classification nvarchar(100) null

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

Here, the command on how to create table in SQL SERVER?

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

What is Primary Key?

 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.

What is Foreign Key?

 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.

1.1.3 Alter Table


It is a normal activity for database designer to add a column, delete or drop column
or field, change the data type of the column, and add constraint to existing table
especially if there are quite changes in the transaction.

 How to add PRIMARY KEY especially if there is no primary key was explicitly
defined at table creation.

Figure C

 How to add FOREIGN KEY to existing table particularly if there is no FK defined at


the table creation?

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.

 How to drop or delete column?


Dropping of column from existing table will also delete all values stored on it. No
other remedy to recover the values if the drop command was executed
successfully.

Example: Dropping column or field “[Code]” from table “[SUB CLASSFICATION]”

Figure F

 How to drop FOREIGN KEY CONSTRAINT from existing table.

Example Dropping Foreign Key [Code] from Key “FK_SubClassification”

Figure G

To other constraints such as PRIMARY KEY, UNIQUE, etc.

1.1.4 DROP TABLE


The drop table statement removes table and all its indexes from a table. Be careful
before dropping a table. Deleting a table will result in loss of complete information
stored in the table.

Example: Dropping table “[Sub Classification]”

Figure H

1.1.5 DROP DATABSE


The drop database statement is used to drop an existing SQL database. Be very careful
before dropping a database. Deleting a database will result in loss of complete
information stored in the database. The command is almost the same with dropping
the table. The script below demonstrates how to delete “AIS” database.

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

The said script uses the


keyword “TOP” and the value
100 followed by the special
character “*”. This SELECT
statement demonstrates how to
retrieve the first-100 records
from table “[Account Titles]”.
The asterisk (*) symbol indicates
that all fields of the table
“[Account Titles]” and its values
will be displayed.

Figure 1.1

8|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD

Example 2

The second example is used to


display the First-100 records of
the selected fields or columns of
table “[Account Titles]” from
“[AIS]” database.

Figure 1.2
Example 3

This third example


demonstrates the use of the
“ORDER BY” clause—used to
sort record based on the field
selected. In this example, it
retrieves all records of
“[Account Titles]” table from
“AIS” database and sort or
arrange the record of field
“[SCode]” in Ascending order.

Figure 1.3

Example 4

The fourth example illustrates


the use of the “WHERE=?”
clause—used to search record
based on the given condition.

In this case, the statement will


only display those records
whose “[SCode]” value is
equal to 10. This statement is
known as search and display, Figure 1.4
and this is applied also when
you’ll be “Log-In” in your Facebook, Gmail, YahooMail and other social media where our Account
Name and Password is required.

9|P age
ACCOUNTIN INFORMATION SYSTEM DATABASE--RDBMS FINAL PERIOD

Example 5

This example shows stricter


application of WHERE clause
wherein the “AND” logical operator
was used. In this case it searches
record based on the two conditions
the [SCode] and [TCode] fields or
columns. This is the real picture of Figure 1.5
Log-in of our Gmail and FaceBook
Account. The search method must
satisfy the two conditions

Figure 1.5a

It shows that the two conditions were


not satisfied which mean that there is
no “1000” value stored in field Figure 1,5a
“TCode” which resulted to blank record display.

Our next module will be the application of JOIN and other DML commands.

2.1.3

10 | P a g e

You might also like