410 likes | 420 Views
Learn how to create tables, establish relationships, and insert data in MySQL. Follow the step-by-step guide with screenshots.
E N D
Open a browser and enter the following address: http://csc-srv1.lasalle.edu:8080/ It should lead to a log in screen. If your name is George Washington, then your username will be c240washingtong If your student id is 1234567, then your password is 1234567was (MySQL usernames can only be 16 characters long. If the above prescription was longer than 16 characters, I stopped at 16. If your name is hyphenated, I only used the first part. )
You should have access to 3 items on the left: Northwind, information_schema, and a database that matches your username.
Looking at a site like brainquote.com, we realize that there is a database behind the scenes.
Brainyquote entities • We see items on the webpage like • Quotes • Authors • Topics
Choosing a quote reveals more: some of the attributes of the entities as well as some of the relationships between entities.
Attributes and relationships • Attributes: • An author has an occupation, a nationality, a birthdate and death date. • Relationships: • A quote is attributed to just one author, but an author may have many quotes attributed to him/her. (Said to be a one-to-many relationship) • A quote might pertain to many topics, and a topic has many quotes that pertain to it. (Said to be a many-to-many relationship)
Click on the database with your user name and start to create a table for Author data. Choose 7 columns. Click Go.
Name the fields, choose their type, decode whether of not they can be null. (I also made the id field “auto-increment” A.I.)
Scroll down and find the Save button. (The Go button is to add another column.)
Some decisions • I did not insert an ID because I chose the ID field to be “auto-incremented”. An ID should be unique so that it can serve as the primary key – a field which uniquely identifies each row/record in a table. • That reminds me I forgot to make the ID into the primary key.
Click on the Structure tab, check the ID field, and click on the Primary key icon.
Other decisions • The remaining fields were chosen to have the type of varchar with lengths of 30 or 20.
Could do better • It might be better to have a list of nationalities (in another table) and choose the author’s nationality from the list rather than typing it in and risking more typo’s. • The same goes for the profession field.
Dates can be tricky • You might have seen in the list of field types a “date type”. This choice would seem to be better for the author’s birth date and death date. But I have seen problems arise with more “historical” dates such as these. The dates supported by the date type don’t go back as far as one might like.
Now for the quote table/entity • We will want • An ID to identify the quote • The text of the quote itself • A quoteAuthorID to establish the relationship between the author and the quote This number is probably the quote’s ID number in brianyquote’s database.
Creating the quoteQuote table Create table, name it, add a column if necessary For quoteID choose PRIMARY for Index and AutoIncrement. For quoteAuthorID choose INDEX for Index. (This choice is necessary for establishing the relationship.) Click Go to Add a column Click save to make the table
After making the quoteQuote table, click on Relationship View
Use the drop-down list to say that the quoteQuote’s quoteAuthorID will come from the quoteAuthorID field of the quoteAuthor table. Click save.
With the quoteQuote table selected, click on the Insert tab. Enter the text of the quote. The quoteAuthorID comes from a drop-down which forces it to match one of the authorID’s.
Next let us make a quoteTopic table with two fields an ID (primary key and auto-incremented) and a name (varchar 30)
Next we will make a bridge/junction table to establish the many-to-many relationship between quote and topic.
Creating the bridge table with two fields – the combination of the two will serve as the primary key.
Use the drop-down to establish the connection to the other tables. In this bridge table the fields serve as both primary keys and foreign keys.
Click on the database (as opposed to a table within the database). Then click on the Designer tab.
What to submit for lab • Make a screen capture (by clicking Print Screen or Alt+Print Screen and pasting the result into a Word document) of the structure (the Structure tab) of your tables and the contents of your tables (the Browse tab). • Also make a screen capture of the Designer.