tamu computer science people pagesfaculty.cse.tamu.edu/slupoli/notes/mysql/database basics... ·...

Post on 15-Sep-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Database BasicsGetting the Big Picture First in DBs

sometimes a picture helps describe the rest!!

The example DB

Technical items about Databases Has a name (universityData in our Example)

o Should be descriptive, but VERY short Databases should be considered “directories”, where tables may be filed Can have many tables inside a database Can have many databases on one DB server the name of the DB (in MySQL) can be 64 letters long

o 64 is safe for all names (fields, tables, etc…)

1

Tables named studentInfo, financialRecords, finalGrades in our example are made up of fields

o fields are attributes of a recordo think of it as what details are needed

tables must be placed in a named database (universityRecords)o there can be multiple tables in a single DB (as there are!!)

design the overall gameplan for tables is importanto unless the project is SUPER simple, there should be more than one table

for a DB

Records and Fields in a table A complete entry into the table, giving values to all fields

Records and Fields example

2

Fields

Records

This table lists all students in a college

Fields (Attributes) Characteristic or property of an overall entity Each field contains a name (short again) datatype options String, Int, Double, Char, Text, Etc... Datatypes covered in more detail later name cannot be a number

100; // ERROR name must start with a letter or a “_” (underscore) name could contain a number as long at letters attached

laneNumber2;o make field name meaningful

ex. counter ex. points

o field names ARE VERY CASE SENITIVE!!!o NO SPACES!!!!

3

Very basic info on MySQL worlds most used open source database (RDMS) manager with its own

programming languageo Relational DB Management System

Interfaceo DOS screeno GUI Interface

PHPMyAdmin

MySQL InterfacesDOS-ish GUI (PHPMyAdmin)

we will focus on the languageo but we will need to create the databases and tables that make up the

overall systemo the language is mostly ENGLISH like

Sample MySQL QueriesSELECT * FROM exampleINSERT INTO example (name, age) VALUES('Timmy Mellowman', '23' )SELECT * FROM example WHERE name='Sandy Smith'

4

Unique features for fields Key

o A unique identifier FIELD to all records, like SSNo There should be a key in every tableo a table cannot be created without a key!!

auto incremento starts at 1, each record is assigned the next value upo this feature is used for id numbers

unsignedo a positive only number, may give us a more useful range

unsigned zerofillo Zerofill automatically adds leading zeros to the number. o Keep in mind that all zerofill columns are automatically unsigned.o example:

no zerofill= 21zerofill = 000000021

update on timestampo when record is added, gives TIMESTAMP when entered. o THIS IS NOT A DATETIME!!!

has caused me problems

5

The example DB

Identify the Keys in each of the tables above.What are the fields in finalGrades?Identify the fields in each table that are autoincrement.

6

Querying a DB in General Ask the database (tables) for answers

o after the table has data inside of it!! Queries must be created as strings of text like a command

o MySQL has a specific syntax (way of writing the command) that we will learn quickly

o The English version does not have to be exact typos, capitalization (some DBMS) and misspelling count!!!

o have to keep track of all names!!! (fields, tables, etc…)

Querying a DB examples

Simple query:How much does Art Frankel owe?

MySQL query syntax:select “address” from `studentRecords` where `lname` = ‘Leader’

7

1. How much does Art Frankel owe?2. Where is Walter Hyland from?3. select “fname” from `studentInfo` where `lname` = ‘Kirouac’4. select “recordNum” from `finalGrades` where `id` = 95. Draw a detailed table that will record a student’s computer/email account

a. What would be a “key” in the table you just created?

The example DB

8

Relationships among tables same categories (sometimes field names) among tables

o sometime keys or secondary keyso usually related

can help us link data between tables used to help identify multiple aspects of the same entity

o in other words, breaking things down into smaller tableso why would BIG tables be bad??

Relationships Example

What grade did Nicole get and in what class? How can you tell?

9

Relationships, the big picture

One-to-One relationship o each student has only ONE financial record

One-to-many relationship o Each student has many final gradeso each student has many classes

10

Foreign Keyso requires two tables, one references another building a relationship

between the twoo second table needs to reference a key from the first table

they should share the SAME name between tables

Identifying Foreign Keys

o In MySQL, a FOREIGN KEY keyword is used to link the second table to the first to keep consistency

this you won’t have to worry about but good to know

11

Terms and Acronyms You Need to Know DBMS

o Database Management System A program that provides all kinds of functionality to allow you to

easily save, update, delete, and search for information. lets you create forms and reports quickly and easily, and obtain

answers to questions about the data Examples

MySQL and Oracle, among many others.

Using a DBMS directly

Using a DBMS through another program

SQLo Structured Query Language

the language of relational databases, databases like MySQL and Oracle.

12

Relational databases are o a type of database that has been around for about 20 years.o the most popular type of database out there today and are commonly

used with dynamic web sites. Logging

o is a special table or text file that records all activity logging to system removing, editing, creating data, etc… could be used to recover old data

Object Databaseso Everything is saved as a programmatic object. o Has everything to do with Object Oriented Programming, Object

Oriented Programming is a way/style of programming that organizes the code as conceptual objects.

o very popular today and can be found in languages like Java, JavaScript, and C#.

Formso Screen objects used to maintain, view, and print data from a databaseo DBMS creates forms that Premiere Products needs

Example of a Form

13

Reportso DBMS creates reports for Premiere Products based on user’s answers

about the desired content and appearance of each reporto example: Crystal Reports

Entity-relationship (E-R) diagramo Visual way to represent a databaseo Rectangles represent entitieso Lines represent relationships between connected entities

Entity-relationship example diagram

14

Data types(Attributes) and Fields in a Table This is a list of the most commonly used datatyp es datatypes can also help reduce the chance for corruption and errors

o inappropriate input is not entered if done correctly!

Unsigned Int Example

Type {storage} Data Type RangeNumeric{1 byte}

TINYINT -128 TO 127[0 to 255 if UNSIGNED]

Numeric{2 bytes}

SMALLINT -32,768 to 32,767[0 to 65,535]

Numeric{3 bytes}

MEDIUMINT -8,388,608 to 8,388,607[0 to 16,777,215]

Numeric{4 bytes}

INT -2,147,483,648 to 2,147,483,647[0 to 4,294,967,295]

Numeric{4 or 8}

FLOAT(p) p=0-24 --> "FLOAT"p=25-53 --> "DOUBLE"

Numeric{4 bytes}

FLOAT Min=+/-1.175E-38Max=+/-3.403E+38

Numeric{8 bytes}

DOUBLE Min=+/-2.225E-308Max=+/-1.800E+308

String{M char's}

CHAR M=0-255 Characters, FIXED. Right padded with spaces.

String{M char's1}

VARCHAR M=0-65,535 CharactersM=0-255 <v5.0.3

String{#char's1}

TEXT2 0-65,535 Char's

String{#char's1}

MEDIUMTEXT2 0-16,777,215 Char's

String{#char's1}

LONGTEXT2 0-4,294,967,295 Char's

Date & Time{8 bytes}

DATETIME "1000-01-01 00:00:00" -"9999-12-31 23:59:59"

Complete list on class website

15

16

What dataypes would you use for:

Table: Bank Account

Account # Answers:Name (first and last)FeesBalanceAddressType (checking, savings, other??)Routing number (bottom of check)Pin #Bank card #Interest rateName of BankContact #

Difference between the datatypes VarChar & Text varchar

o can have numbers and letterso MUST be specific size (#)o Example

State (2) Zip code (5)

texto can have UNLIMITED number of letters/numbero no specific lengtho examples

address comments names

17

NULL vs. NOT NULL another field attribute that can used for validation NULL

o mean that the data for that field is NOT requiredo example

middle name cell phone

NOT NULLo it is required!! o Like key

if a record that is about to be placed is missing a field that is “not null”, the record will not be entered!!

18

Eliminating redundancy between tables if you have several tables that have the EXACT same set up

o but using to place data into different ones for a reason use only one table, and add a column that would help further breakdown your

results save time, space, redundant code and data

Saving oh so much time and data

19

FYI Section

Entity-relationship example diagram

http://www.visual-paradigm.com/VPGallery/img/datamodeling/EntityRelationshipDiagram/Entity-Relationship-Diagram-Sample.png

20

http://pubs.usgs.gov/of/2001/of01-223/freeman1.gif

Introduction to PHPMyAdmin a NICE AND PRETTY web based MySQL application and access IT IS ONLY BUILT with PHP!!! This is not a biased product!!! Free, from Apache Can install (as a server) on your own computer will use it from this class to test pages and familiarize yourself with MySQL ** start first video at 7th minute (guy is boring, sorry, but worth it)

CCBC: http://webtech.ccbcmd.edu/phpmyadmin/sql.phpFCC: https://cis230.frederick.edu

Username -> first letter of FIRST last, full lastname

21

Find where to change your password.

Answers

Sources:http://www.developerfusion.com/article/3998/mysql-tutorial/7/Example of an Access Formhttp://technet.microsoft.com/en-us/library/cc750890What is mySQLhttp://en.wikipedia.org/wiki/MySQL Foreign Keyshttp://www.w3resource.com/sql/creating-and-maintaining-tables/foreign-key.php

22

top related