Database Quiz with Explainations Hans-Petter Halvorsen, M.Sc.
Questions 1. What is a Database? 2. Give Examples of Systems that use a Database 3. What is DBMS? 4. Give Examples of DBMS systems? 5. We have 2 different types of Databases. Give also some Examples in each Category 6. What is an ER diagram? 7. Give examples of Database Design Tools? 8. Give Examples of Database Best practice 9. We have 4 different types of SQL Queries. Give also an example in each Category 10. What is a View, Stored Procedure and a Trigger?
What is a Database?
Old fashion Database (Data-storage) Systems Not too long ago, this was the only data-storage device most companies needed. Those days are over.
Database Systems A database is an organized collection of data. A Database is a structured way to store lots of information. The information is stored in different tables. Everything today is stored in databases!
Give Examples of Systems that use a Database
Database Examples Bank/Account systems Information in Web pages such as Facebook, Wikipedia, YouTube, etc. Fronter, TimeEdit, etc. lots of other examples!
What is DBMS?
Database Management Systems (DBMS) A database management system (DBMS) is system software for creating and managing databases. The DBMS provides users and programmers with a systematic way to create, retrieve, update and manage data. A DBMS makes it possible for end users to create, read, update and delete data in a database. The DBMS essentially serves as an interface between the database and end users or application programs, ensuring that data is consistently organized and remains easily accessible. http://searchsqlserver.techtarget.com/definition/database-management-system
Database Management Systems (DBMS) The DBMS manages three important things: the data, the database engine that allows data to be accessed, locked and modified and the database schema, which defines the database s logical structure.
Give Examples of DBMS systems?
Database Management Systems (DBMS) Microsoft SQL Server Enterprise, Developer versions, etc. (Professional use) Express version is free of charge Oracle MySQL (owned by Oracle, but previously owned by Sun Microsystems) - MySQL can be used free of charge (open source license), Web sites that use MySQL: YouTube, Wikipedia, Facebook Microsoft Access IBM DB2 Sybase MariaDB MongoDB etc.
We have 2 different types of Databases Which? Give also some Examples in each Category
Database Types Relation Database/SQL Databases Microsft SQL Server Oracle MySQL MariaDB etc. NoSQL Databases MongoDB etc.
What is an ER diagram?
Database Design ER Diagram ER Diagram (Entity-Relationship Diagram) Used for Design and Modeling of Databases. Specify Tables and relationship between them (Primary Keys and Foreign Keys) Example: Table Name Table Name Primary Key Primary Key Foreign Key Column Names Relational Database. In a relational database all the tables have one or more relation with each other using Primary Keys (PK) and Foreign Keys (FK). Note! You can only have one PK in a table, but you may have several FK s.
Table Name ER Diagram Example - Visio Primary Key (PK) Foreign Key (FK)
Give examples of Database Design Tools?
Database Design Tools Visio PowerDesigner CA ERwin CA ERwin Data Modeler Community Edition Community Edition is Free, 25 objects limit Support for Oracle, SQL Server, MySQL, ODBC, Sybase Toad Data Modeler A Simple designer is also included with SQL Server (physical model, not logical model)
Give Examples of Database Best practice
Database - Best Practice Tables: Use upper case and singular form in table names not plural, e.g., STUDENT (not students) Columns: Use Pascal notation, e.g., StudentId Primary Key: If the table name is COURSE, name the Primary Key column CourseId, etc. Always use Integer and Identity(1,1) for Primary Keys. Use UNIQUE constraint for other columns that needs to be unique, e.g. RoomNumber Specify Required Columns (NOT NULL) i.e., which columns that need to have data or not Standardize on few/these Data Types: int, float, varchar(x), datetime, bit Use English for table and column names Avoid abbreviations! (Use RoomNumber not RoomNo, RoomNr,...)
We have 4 different types of SQL Queries. Give also an example in each Category
SQL Structured Query language A Database Computer Language designed for Managing Data in Relational Database Management Systems (RDBMS) Query Examples: insert into STUDENT (Name, Number, SchoolId) values ('John Smith', '100005', 1) select SchoolId, Name from SCHOOL select * from SCHOOL where SchoolId > 100 update STUDENT set Name='John Wayne' where StudentId=2 delete from STUDENT where SchoolId=3 We have 4 different Query Types (CRUD): INSERT, SELECT, UPDATE anddelete CRUD Create (Insert), Read (Select), Update and Delete
What is a View, Stored Procedure and a Trigger?
A View is a virtual table that can contain data from multiple tables A Stored Procedure is like Method in C# - it is a piece of code with SQL commands that do a specific task and you reuse it A Trigger is executed when you insert, update or delete data in a Table specified in the Trigger.
Create View: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CourseData' AND type = 'V') DROP VIEW CourseData GO Creating Views using SQL A View is a virtual table that can contain data from multiple tables The Name of the View CREATE VIEW CourseData AS SELECT SCHOOL.SchoolId, SCHOOL.SchoolName, COURSE.CourseId, COURSE.CourseName, COURSE.Description FROM SCHOOL INNER JOIN COURSE ON SCHOOL.SchoolId = COURSE.SchoolId GO Using the View: select * from CourseData Inside the View you join the different tables together using the JOIN operator You can Use the View as an ordinary table in Queries : 27
Create Stored Procedure: IF EXISTS (SELECT name FROM sysobjects WHERE name = 'StudentGrade' AND type = 'P') DROP PROCEDURE StudentGrade OG CREATE PROCEDURE StudentGrade @Student varchar(50), @Course varchar(10), @Grade varchar(1) AS Stored Procedure A Stored Procedure is like Method in C# - it is a piece of code with SQL commands that do a specific task and you reuse it Procedure Name Input Arguments DECLARE @StudentId int, @CourseId int select StudentId from STUDENT where StudentName = @Student Internal/Local Variables Note! Each variable starts with @ select CourseId from COURSE where CourseName = @Course insert into GRADE (StudentId, CourseId, Grade) values (@StudentId, @CourseId, @Grade) GO Using the Stored Procedure: execute StudentGrade 'John Wayne', 'SCE2006', 'B' SQL Code (the body of the Stored Procedure)
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'CalcAvgGrade' AND type = 'TR') DROP TRIGGER CalgAvgGrade GO CREATE TRIGGER CalcAvgGrade ON GRADE FOR UPDATE, INSERT, DELETE AS DECLARE @StudentId int, @AvgGrade float select @StudentId = StudentId from INSERTED Trigger A Trigger is executed when you insert, update or delete data in a Table specified in the Trigger. Create the Trigger: select @AvgGrade = AVG(Grade) from GRADE where StudentId = @StudentId update STUDENT set TotalGrade = @AvgGrade where StudentId = @StudentId GO Name of the Trigger Specify which Table the Trigger shall work on Specify what kind of operations the Trigger shall act on Internal/Local Variables Inside the Trigger you can use ordinary SQL statements, create variables, etc. Note! INSERTED is a temporarily table containing the latest inserted data, and it is very handy to use inside a trigger SQL Code (The body of the Trigger)
References Software Development - A Practical Approach Halvorsen, Hans-Petter, 2015 Essentials of Software Engineering Frank Tsui; Orlando Karam; Barbara Bernal, 3 ed., Jones & Bartlett Learning Software Engineering I. Sommerville, 10 ed.: Pearson, 2015 Software Engineering. Modern Approaches E. J. Braude and M. E.Bernstein, 2 ed.: Wiley, 2011.
Hans-Petter Halvorsen, M.Sc. University College of Southeast Norway www.usn.no E-mail: hans.p.halvorsen@hit.no Blog: http://home.hit.no/~hansha/