Friday, December 4, 2015

Database concepts - ER Diagrams (Continued)



So, in the last post I went over a little about how things are represented in an ER diagram, as well as the reasons to why using an ER diagram is helpful to design a good database. In this post, we'll elaborate on specific rules and guidelines that should be followed ad considered when creating an ER Diagram along with more information on relationships. Let's get started!

The basic setup of the ER diagram is formed in the first post, but there are Integrity constraints that need to be established.
Entity Integrity:
The entity integrity constraints states that a primary key of an entity cannot be NULL. NULL means that the value is unknown. The reason behind this is that specific rows of an entity is identified by the primary key(previous post), and if there is no primary key, then there is no way to identify it.

Referential Integrity:
The referential integrity constraint states a specific constraint between two tables. The primary table's records must exist in order for another table to reference that record.

For example, John sells a used car to Adam. The license of this car is 00A154.
In the Car table, there would first exist an record of this car:
    (License, Year, Model, Manufacturer) --> (00A154,  2014, Civic, Honda)

In the Sells table, there would then exist an entry:
    (Salesman, Client, Car) --> (John, Adam, 00A154)

1. If John were to change this record in table Car to (00B154, 2014, Civic, Honda), the Sells table would not be able to locate a record of 00A154.

2. If John were to delete the record in table Car, the Sells table would also not be able to locate a record of 00A154.

3. If John accidentally inserted the Sells record incorrectly say (John, Adam, 00B154), then when the Sells table try to locate Car 00B154 and it's information, the records would not be able to find it.

4. However, John can insert the Sells record as (John, Adam, NULL).

Foreign Integrity:
Foreign integrity constraints help solve the first two problems we have with the Referential Integrity Constraints.

Cascade Update:
Any time the primary table's record is changed, anything that references it must also be changed. That way there would not have mismatching records when we try to locate the reference.

Cascade Delete:
Any time a primary table's record is deleted, anything that references it must also be deleted. That way there would not have unfound records.


Relationship Constraints: There are key constraints, One-to-One, One-to-Many, and Many-to-Many. Then, there are participation constraints.

One-to-One: One to one means that an entity can at most be related one entity. For example, a man can be married to one woman, and vice versa. Sometimes there would also be specific ranges on the line specifying the relationship as [1:1]. This would be denoted by an arrow:

Man --> marries <-- Woman

One-to-Many:  Let's say in a case that a man can marry many women, but a woman can only marry one man. Sometimes there would also be specific ranges on the line specifying the relationship as [1:N]. The relationship would be a One-to-Many, expressed as the following:

Man -- marries <-- Woman

Many-to-Many: Many-to-Many are represented like the diagram above, just a thin line.

Mechanic -- Repairs -- Car

Participation Constraints: If we want to create a database where everyone must be married, then we would have a participation constraint. The arrows are still present to represent the One-to-One relation, and the line is thickened to show that records MUST be married couples.

Man --> marries <-- Woman

Saturday, November 21, 2015

Database Concepts - The Entity-Relationship Diagram


Via texample.net

In this post I will try to start database ideas with modeling. Here, I will explain what an ER diagram is and how it is used as a database model. In future posts I will explain more into detail of database ideas once I have established a basic bridge. Lets get started!


In order to continue understanding clearly upcoming concepts we need to first establish some more basic basic basic terms:

Primary Keys:
Primary Keys are the significant attributes of an entity. The primary keys are used to identify the specific entity, meaning that they are unique e.g., student IDs. You'll see more of an explanation of what an entity is below.

Foreign Keys:
Foreign Keys are specific attributes that are referencing an original attribute. Continuing with the student ID example, there may be a chart of students that are age 20, identified by their student IDs. That chart's student IDs refer to the original list of Students and their student IDs.
+-------------------+     +------------------------+
| Student |   ID#   |       |      aged 20 IDs       |
|   John    |  1001  |       |          1001              |
|   Sally   |  1002  |       +-----------------------+
+-------------------+

*see here that we have a roster of students, and then a list of students that are 20, we're assuming only John is 20. This is omitted but the roster of students should also include the details of their age.

Summarized:
Primary Keys are unique identifiers, Foreign keys refer to another entity's attributes. So, looking back into the previous post, we have a change.

1. Professors(Pid CHAR(50), Pname CHAR(50), Dept CHAR(50), PRIMARY KEY (Pid))
2. Students(Sid CHAR(50), Sname CHAR(50), Age INTEGER, MajorCHAR(50), PRIMARY KEY (Sid))
3. Departments(Did CHAR(50), Dname CHAR(50), PRIMARY KEY (Did))
4. Courses(Cno INTEGER, Cname CHAR(50), PRIMARY KEY (Cno))
5. EnrolledIn(Sid CHAR(50) REFERENCES Students(Sid), Cno INTEGER, PRIMARY KEY (Sid))

*here we have added things to specify PRIMARY KEY. Also, EnrolledIn has an addition to show that Sid is referring to the table Student(Sid).  With that said, we also need to establish some integrity constraints to avoid big disastrous problems. I'll talk about these once we get the whole picture.

The Entity-Relationship Diagram:
Also called ER diagrams. ER diagrams are used to plan out how different elements of the database are going to be connected and "mapped." They show the relationships between entities their attributes, as well as entity-entity relationships.

Entity:
The entities in the diagram above are the ones in blue(Employee, Salesman, Mechanic, etc.).
Entities are the "things" that are major in a database. In the diagram, we see that the "Important" objects that are the bigger part of this database are going to be the ones in blue. All of the entities are represented as a rectangle.

Attributes:
Attributes are the little details related to each entity.

And there you have it! If you read this then you have just learned the very basics of database concepts! I'm looking forward to writing more about this topic! See you soon.


Wednesday, November 18, 2015

Data Structures - Stacks and Queues

In this post we'll go over some things about stacks and queues.
I have in an earlier post wrote an implementation of a queue while explaining lists. You can take a look here.



Queues:
Queues are FIFO, meaning first in first out. The first element put into the queue is dequeued in the before the others. There is a head and a tail that keeps track of the first and last element enqueued.

Priority queues:
Priority queues are used in storing processes and their priorities. When a system has to switch the processor between many many processes going on, it can switch based on priorities. Some processes have lower priority and some, higher.

Circular Queues:
Circular queues are the same as queues, except that it wraps around the end to the beginning again. With circular queues, it would be easy to use an array, since there is only a limited amount of space in a circle.

Stacks:
Stacks are FILO, meaning first in last out. Think of the stack like, a stack of plates. If you stack one plate over another, say five plates, you would take each one off one by one from the fifth one you stacked to the first one at the very bottom. Stacks are used for memory storage in systems, can be allocated for a process.

These two data structures, as you can see my examples of them, are used in important systems and programs.

Thursday, November 12, 2015

Database Concepts - What is a Database? Some beginning notes



What is a database?
A database is a glop of data, a collection of data that models real world things; companies, inventory, websites, etc. The collection of data describes a series of relationships between some entities. (e.g., Annie is taking course number A110 or Josie is subscribed to a specific magazine).

What is a DBMS?
DBMS stands for "Database Management System." As the name says, it's a software that is designed to manage and store the database.

How is a file system different?
A file system is split between main memory and secondary storage, since not everything can be stored in one place. A file system also has multiple users, but it also has access control.

How is this important in CS?
The need for database knowledge is growing; websites have big sets of products, search libraries, the Human Genome project, etc. The concepts of databases also connect to Operating Systems, Programming, theory, or Artificial Intelligence.

What is a data model?
A data model is something that describes the relationships between the entities.
The relational model of relational data consists of tables:
1. The Relation: the rows and columns part of the tables are the relations.
2. The Schema: The schema are the entities and it's attributes.

The different levels of abstraction:
1. View Level/External Level:
       This layer describes how users see this data and database
2. Conceptual Level:
        This level defines the logical part of the database, the connections.
3. Physical Level:
        This level are the schemas that describe those physical files.

Examples of Levels:
Physical Level:
        All the files of each table, not in some order. So just think of a big floating world of tables, I suppose.
    Conceptual Level:
    1. Professors(Pid CHAR(50), Pname CHAR(50), Dept CHAR(50))
    2. Students(Sid CHAR(50), Sname CHAR(50), Age INTEGER, Major CHAR(50))
    3. Departments(Did CHAR(50), Dname CHAR(50))
    4. Courses(Cno INTEGER, Cname CHAR(50))
    5. EnrolledIn(Sid CHAR(50), Cno INTEGER)

    So here we have covered some basic knowledge of databases. Please let me know in the comments what I should talk more about, or if I made any mistakes or typos in this post! I hope you enjoy the start of this learning experience with me! I will soon add to this collection of database posts and put the next link here.

    Tuesday, November 3, 2015

    Sorting Algorithms - Insertion Sort

    Pic from Geeksquiz
    Insertion Sort is utilized to sort a group of elements from smallest to largest. Here's how it works:


    Lets say we start with an array A = {15, 16, 12, 7, 9}. This array is not sorted(least to greatest).


    Pseudocode:
    Insertion-Sort(A)
    1.    for j = 2 to A.length do
    2.        key = A[j]
    3.        i = j - 1
    4.        while i > 0 and A[i] > key do
    5.            A[i+1] = A[i]
    6.            i = i - 1
    7.        A[i + 1] = key

    Walk-through:
    Starting with the unsorted array {15, 16, 12, 7, 9}...
    line #1: j is 2. array is unchanged {15, 16, 12, 7, 9}
    line #2: key is 16
    line #3: i is 1
    line#4: loops while i is not going out of bounds and A[i] is bigger than the key(current j that we are comparing)
    line#7: we skipped lines 5 and 6 because A[i](15) was not bigger than key(16). We basically copy the key back into i + 1 in which this case it is also j.

    line#1: j = 3 array is {15, 16, 12, 7, 9}
    line#2: key is 12
    line#3: i is 2
    line#4: loops while i is not going out of bounds and A[i] is bigger than the key(current j that we are   comparing)
    line#5: 16 > 12, so A[i + 1] = A[i] OR {15, 16, 16, 7, 9}
    line#6: i is 1
    line#7: A[i + 1] = key OR {15, 12, 16, 7, 9}
    line#5: 15 > 12, so A[i + 1] = A[i] OR  {15, 15, 16, 7, 9}
    line#6: i is 0
    line#7: A[i+1]  = key OR {12, 15, 16, 7, 9}

    See here that first three elements are now already sorted, if we keep going with the pseudocode we will eventually come to the sorted {7, 9, 12, 15, 16}. Try doing the rest!

    Analysis: 
    Worst Case: The worse array that can go through this is some array that is sorted in reverse order e.g, {19, 15, 13, 7, 3, 1} 

    In this case, the algorithm will have to go through and compare each number to more and more numbers.
    For example, the worst case array presented here will first compare 15 once, then compare 13 twice, then 7 three times, 3 four times, and 1 five times:
                                                         1 + 2 + 3 + 4 + 5........

    In terms of the length of the array n, it will compare a total of:
                                                                n(n + 1)/ 2  OR  (n^2 + n)/2 times

    As you accumulate more knowledge or already have, you will see that O(n^2) is not very nice a time compared to some others.

    Thursday, October 29, 2015

    Data Structures - Linked Lists Part 2 (and Queues)


    This is a second part to my first post about Arrays and Linked lists. Here I have (finally) implemented and tested two separate queues. Notice that you probably wouldn't need a queue structure if you are utilizing only one queue. The queue structure is so that I can use several different queues. Lots and lots of comments to guide you through:

    #include <stdio .h>
    #include <stdlib .h>
    
    typedef struct mynode{ //this is the node structure, which includes a data int
      int data;            //and a pointer to the next node
     struct mynode* next;
    }node;
    
    typedef struct{ //this is the queue structure, which includes a head and tail pointer.
      node* head;   //a head and a tail pointer points to the first and last nodes of a queue
      node* tail;
    }queue;
    
    void enqueue(queue* que, int new_data); //here a list of the function prototypes
    void dequeue(queue* que);
    int take_queue_data(queue* que);
    void print(queue* que);
    
    int main()  //main function to test out
    {
        //test cases hereeee
        queue* testq1 = (queue*)malloc(sizeof(queue));
        queue* testq2 = (queue*)malloc(sizeof(queue));
        enqueue(testq1, 1); print(testq1);
        enqueue(testq2, 2); print(testq2);
        enqueue(testq1, 3); print(testq1);
        enqueue(testq2, 4); print(testq2);
        enqueue(testq1, 5); print(testq1);
        enqueue(testq2, 6); print(testq2);
        dequeue(testq1); print(testq1);
        dequeue(testq2); print(testq2);
        dequeue(testq1); print(testq1);
        dequeue(testq2); print(testq2);
        dequeue(testq1); print(testq1);
        dequeue(testq2); print(testq2);
    }
    
    void enqueue(queue* que, int new_data){
      node* tmp = (node*)malloc(sizeof(node)); //make a temporary node
      tmp->data = new_data; //set the node->data to what we want to insert   
      tmp->next = NULL; //the next of that node would just be the nothing.
    
      if (que->head == NULL && que->tail == NULL){// if queue is empty
        que->head = tmp;// here we set the head and tail to this one node
        que->tail = tmp;// because this is the first node ever
      }
      else{ //if queue isnt empty, then just set it as the new tail.
        (que->tail)->next = tmp; 
        que->tail = tmp;// and then remember to link this node to the last one's "next" pointer
      }
      return;
    }//end of enqueue
    
    void dequeue(queue* que){
        node* tmp = que->head; //temporary for head of queue
      if (que->head == NULL){ //in case queue is empty
      printf("Queue is empty! \n");
        return;
      }
      else if(que->head == que->tail){
        que->head = NULL; //delete that last element
        que->tail = NULL; //and everything back to NULL
      }
      else{//there is more than one element in queue
          que->head = (que->head)->next;
      }
      free(tmp); //we cant hold on to this mem space forever, or cannnnn we. :(
    }// end of dequeue
    
    int take_queue_data(queue* que){//this will give us the first data in queue
        if(que->head == NULL){
         printf("Queue is empty, nothing to look at.");
         return 0;
        }
        else{
         return (que->head)->data;   
        }
    }
    
    void print(queue* que){//this is to look at whole queue
        node* tmp = que->head; //set tmp to the front of the queue to walk
        while(tmp != NULL){
            printf("%d ", tmp->data);
            tmp = tmp->next;
        }    
        printf("\n");
    }

    I hope this helps you understand a linked list better(or how to implement it). Remember there is a doubly linked list too, and it's simple to implement with a alittle tweaking of this code. :D
    If you see any problems, have any suggestions, or have any questions feel free to ask in the comments below!

    Monday, October 12, 2015

    Data Structures - Arrays and Linked Lists

    An array is a random access data structure, while a linked list is a sequential access data structure. So, what's the difference between random and sequential?

    Well, random means you can access something directly, and sequential means in order to access something, you have to go one by one. See this:

    Arrays:

    Linked lists:

    or

    The differences between the two different linked lists, we'll talk about later. But the difference between arrays and linked lists, we will talk about :D

    Okay, the difference: Arrays, can be access directly, like some array A and some index -> A[0].
    In the above example of an array, A[0] would = 10, A[1] = 6,  A[2] = 7, and so on. The way to access these indices is very direct, like opening a book to a specific page, assuming that there's bookmarks to every page...

    Linked lists on the other hand, are linked someway, the first one in only one direction. For the first linked list, it is analogous to......to.......... walking into a huge cave and every step you get a boulder behind you, you can't go back, sadly.

    The second one is linked bidirectionally, which means you can go back and forth between elements, like a string of rooms each with a door. 

    Yup, that's all.

    I'll add more to this if I find that I have left anything out, and I hope this explains to you arrays and linked lists!

    Here's to part 2: Implementation



    Wednesday, October 7, 2015

    Java 101 - 02 Comments, Objects, and Classes


    Summary of last post(link here): We looked over the basic introduction of Java and some start on the syntax and specifics how Java should work. Here's the code that we ended up with to refresh your mind:
    //This is the Myclass class naming
    class MyClass{
    //This is the main method we always need
    public static void main(String []args){

    }
    //This is some random method we made, notice the capitalization rules.
    public void thisMethodNotAllLowercase(){

    }
    }


    In this post will go over commenting(which you've seen some from last post), Objects, and how classes are related to those Objects.

    This is going to be a pretty simple a straight forward post about commenting in the Java programming language. Commenting in any language is useful if a few ways:

    1. If you are working on a crazy long project, comments will help you skim through what your lines are doing and refresh your mind quicker. That is both a time saver as well as a big life saver.
    2. If you are working with other team members and they need to know what you are doing, they can skim through your lines without sitting there forever trying to firgure out. That will save you and your team time, as well as get some work doneeeee.
    3. This sort of piggy backs with the first two, but if you decide to come back to it after a break then you won't have to worry about forgetting, it's all there!
    Alright then, to the hows:
    • To comment just on one line, simply type "//" without the quotes.
    • To comment several rows simply start with "/*" and end with "*/" , also without the quotes.

    //This is one line comment

    /*
    This
    is
    several
    line
    comment!
    */

    /*
    * This
    * Just
    * looks
    * Prettier
    */

    So, this basically ends it. Pretty simple eh? Now you can comment you can try creating your own code without having to remember everything by mind. Hope you learn something from this post and I'll try to post more soon.

    *If you find a typo or error let me know! I'm human and I make silly mistakes too.

    Monday, June 22, 2015

    Starting on OOP(Object Oriented Programming) through Java




    Hi readers,

    I've been wondering if I should throw something out there about getting started with java, since many self taught programmers learn java some time in their life, or not. I teach an introductory class in college, well I guess I instruct. In that class students are introduced to the Java programming language and they learn Object Oriented Programming concepts.

    So, in this post, I'd like to write something about getting some tools ready and set to learn the language and concepts that it teaches.


    1. Set Up an IDE or Such
    Setting up an IDE is important if you want to check or compile your code. There are different IDEs available for Java just like there are several for other programming languages. To see different types of them see here.

    2. Use Online Resources
    I give my students TutorialsPoint references very often. They give information by variable types; Strings, arrays, etc. If sometimes you don't see the information you want, using Google to search the site and the topic usually will bring up a TutorialsPoint link for it.


    Java Oracle Documentation: Many of my students don't like using this site since it's not always the simplest explained. Nevertheless, some still find this page useful for finding what they need.








    Stackoverflow: Yes yes, it doesn't give documentation like oracle or tutorialspoint, but it does give you insight to similar problems and similar solutions once you get started. Stackoverflow is also useful for other languages as well.

    3. Tutorials
    Oracle also gives tutorials here, although the effectiveness of the tutorials are up to you. There are many Youtube videos that give great amounts of info about java, going through things step by step and talking slower so that you can process concepts more properly. Derek Banas give really fast tutorials but if you slow it down or you pause to think through it, it will work out well. His tutorials are more like reviews sometimes so you possibly want to use it as a review when you think you've got the jist of Java.

    Through this mini guide I hope that you find some useful resources on where to look to learn Java! :D
    I also hope to put up a few more posts/tutorials for Java and possibly videos to go with it, I look forward to helping anyone with the language through my tutorials.

    Tuesday, June 16, 2015

    Java 101 - 01 What is Java and What is it Made of?

     Whether you're a computer Science major or just someone thinking of picking up a pastime skill, this post will explain what Java is and also give some beginning introduction to it. :)


    An Introduction to Java's birth and life:
    Java was born on June 1991. Created by James Gosling, Mike Sheridan, and Patrick Naughto. Apparently I learned through some research that Java was created for the purpose of television improvement but it was too advanced to be used during that time. It was originally named Oak after some oak tree. Later, the name changed to green during their green project and then changed to Java after the coffee. Something I know about Java is that things are being added and have been added since the creation of it and everything accumulated from all these years is still in the language. Reason of this that there were five established goals at the time they created it.

    1. It must be "simple, object-oriented and familiar"
    2. It must be "robust and secure".
    3. It must be "architecture-neutral and portable".
    4. It must execute with "high performance".
    5. It must be "interpreted, threaded, and dynamic".

    With these rules the now commonly used language was birthed. If you like to know more about the creator or the language, visit the wiki or webpage I have linked to each of the creators.

    How this little tutorial is going to work:
    In this tutorial (or any future tuts) I will try to go through the talking step by step, where each tutorial will go along the development of a small program. Each step will include little snippets of code, where at the end I will make a code combination. The final code combination allows you to see what you have done after the whole tutorial, as well as see what you have learned. These tutorials, hopefully, can be easily followed and the code snippets can be copied into your own compiler for you to learn.

    Suggestion: Use a text editor or a simple compiler program to help you with the code. In our University Java class we use Dr. Java. Usually the jar file is easier to use. It's also portable so you can stick in on a flash drive.

    Straight to the basics:
    Now to the fun. We will get started with the basic syntax since all background information is provided in the section above.

    Syntax:
    Java code is case sensitive, so beware your capitalization or your keyboard's auto-capitalization features. Many times my students go nuts over their code error forever to only find that it was one letter. ONE LETTER!!!!

    Naming Conventions:
    Naming conventions are just the rules of how to keep organized names of different things. Class names should have the first letter capitalized,with each word's first letter also capitalized.
    class MyClass{
    }

    Method Names:
    Method names should have the first letter lowercase,with other words' first letter capitalized.
     public void thisMethodNotAllLowercase(){

    }

    The Main Method:
    The main method is a mandatory part of a java program; it is used to tell the Java program where to go and what to do.
    public static void main(String []args){

    }

    Program File Naming:
    The name of your program file should always be named the same as your class name. Going with the class name example we put above, our program file would be named MyClass.java


    Keywords:
    Keywords are special words that are used to "command" the program. Words like class, public, or static(like those used in example above). So, don't use these keywords as names unless you want your compiler to yell at you. And you don't want your compiler to yell at you.

    Summary:
    We learned about how to name things in our Java program in this tutorial; don't worry I will explain what class method and all that jazz is in a later tutorial! But, here is the end code if you want to try it anyways.
    //this is a comment. We'll talk about it later don't worry.
    //This is the Myclass class naming
    class MyClass{
    //This is the main method we always need
    public static void main(String []args){

    }
    //This is some random method we made, notice the capitalization rules.
    public void thisMethodNotAllLowercase(){

    }
    }


    If I made any errors please point out to me with comments! I'm human, despite what my nephew says; Unfortunately I make mistakes too.

    Wednesday, March 11, 2015