sql an overview(1)

22
Database Creation Recap and an introduction to SQL Server Management Studio

Upload: helen-ward

Post on 22-Jan-2018

146 views

Category:

Documents


1 download

TRANSCRIPT

Database Creation

Recap and an introduction to

SQL Server Management Studio

What is a database?Storing and retrieving data

• Persistent data: preserves previous data“a persistent data structure is a data structure that always preserves the previous version of itself when it is modified” wikipedia.org

“Persistent data are very rarely modified; this means the information stored within the database, disk or tape is not changed, except for special occasions.” www.wisegeek.com/

Normalisation required to ensure that new data is recorded in new records rather than overwriting

‘Rules’

The Data Protection Act″ used fairly and lawfully• used for limited, specifically stated purposes• used in a way that is adequate, relevant and not

excessive• accurate• kept for no longer than is absolutely necessary• handled according to people’s data protection rights• kept safe and secure• not transferred outside the UK without adequate

protection ”https://www.gov.uk/data-protection/the-data-protection-act

Further legislation (DPA)

“There is stronger legal protection for more sensitive information, such as:

• ethnic background

• political opinions

• religious beliefs

• health

• sexual health

• criminal records ”https://www.gov.uk/data-protection/the-data-protection-act

Flat-file database

• Single table

• Repeated or overwritten data

Discuss this example…

Normalisation

• Relational database (multiple tables/entities)

• Reduces repetition (data redundancy)

• Maintains data integrity

• Allows for referential integrity (parent/child relationships)

• Maintains previous data: keeps persistent data

Notes:

Keeping persistent data

Consideration needs to be given to what will data will need to be kept and what does not e.g.

Contact details

• Is it necessary to record previous contact details?

• What about future? (e.g. new address in 2 weeks)

• Do several people live at the same address?

• How many phone numbers could a person have listed?

• How could you allow for more than one phone number (or address) without duplicating the person details?

• Should contact details be entities in their own right?

Maintaining Data Integrity

• What does the term mean?

• What tools are available to you (as database designer) to help maintain data integrity?

Type your answers/notes here…

Maintaining Data Integrity

• Data Types

• Validation rules/text

• Setting limits

• Combo/drop-down boxes

• Set formats/input masks

• Default values

• Mandatory fields

• …?

SQL Server Management Studio

• Structured Query Language

• SQL Server: ‘back-end’ (storage area for data)

• Management Studio: client ‘front-end’ (user interface)

• Middleware: software that links the front-end and back-end applications together

programming that serves to "glue together" or mediate between two separate and often already existing programs…provide messaging services so that different applications can communicate http://searchsoa.techtarget.com/definition/middleware

Retrieving data: Query (or View)

• Table format (virtual)

• Limits/restricts what is shown from selected table(s) according to set criteria selection

– User sees only what is needed

– Make sure you select appropriate fields to make sense!

• Can allow for simple functions/calculations to be carried out on the selected data

SQL View

″ In SQL, a view is a virtual table based on the result-set of an SQL statement.”

″ A view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database.”

″ A view always shows up-to-date data! The database engine recreates the data, using the view's SQL statement, every time a user queries a view.”

http://www.w3schools.com/sql/sql_view.asp

SQL View

• Restricts what a user sees according to permissions granted

– the user will only see what they require from the database and can query that ‘view’ in the same way as if it were the whole database

– Can be reused so that you don’t have to copy and paste (as you would with queries) if you want to use the same in different places of your code.

Adapted from: http://stackoverflow.com/questions/1108664/difference-between-views-and-select-queries

Retrieving data

• SQL Views and queries

• Always shows up-to-date information at runtime

• Specific data e.g.

– Personal data: search by name, address, DoB…

– Finance: weekly/monthly income or outgoings

– Stock: search for products

– …?

http://www.w3schools.com/sql/sql_select.asp

Retrieving data

Selecting specific data, using one or more criteria

• Customers, ‘over 18 years old’, who live in ‘West Durrington’

• Staff wages for ‘January’

• Best selling products in ‘January’

• ‘Strategy’ AND ‘Role-playing’ games (by genre)

• Attendance at college by individual (or group!)

Try some at:http://www.w3schools.com/sql/

Retrieving data: Reports

• Categorising (groups) and sorting data more effectively

• More formally presented

• More user-friendly

Queries can be exported for further formatting options or to enable more complex calculations to be carried out

SQL

Creating a table:

• Naming conventions

• Data types

• Allow Nulls?

Research the full list of SQL data types and their uses.

Why is it important to use the correct data type?

SQL Server Data Types

http://www.w3schools.com/sql/sql_datatypes.asp

Things to remember

• Table per entity

• Attributes as Atomic Values in fields– Information > data

• Keys:– Primary: single unique identifier

– Composite: multiple field identifier (primary + secondary)

– Foreign: primary key from another table (used to create a link between two tables)

Doctor’s surgery

• Primary key in Patient table

– Patient number

• Composite key in Appointments table

– Patient number and date of appointment

• Foreign key in Appointments table

– Patient number

Relationship examples

• One-to-one – Person to Ethnicity or Gender (where Ethnicity or

Gender are pick-lists from another table)

• One-to-many– Person to Appointment– Person table=‘parent’, Appointment table=‘child’

• Many-to-many– Person to Address (Addresses may be set up e.g.

Gazetteer)– No ‘parent’ as one Person may have many addresses

but Address may have many occupants too!

Assignment Submission Database

• Discuss entities, attributes and keys that you have recorded for your practice database.

• Can you spot any potential duplication of data or other problems?

• Ensure that your design documents reflect data types, validation, appropriate use of keys and relationships.

• Create a comprehensive list of the queries that your client needs from the database.

• Continue to develop your database (in MS Access or SQL Server), identifying issues as they may arise.

• Review your design documents accordingly.