Relational SQL Databases

by George Ionescu.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on databases  

You are here: Categories » Computers and technology » Databases

Think of a database that related the players in a game to one another (for example, to determine who was a friend of each player and who was an enemy of each player).

First let's create a table to store the data for each of the players. Notice the addition of a primary key, which allows you to rely on the fact that all rows have a unique field that can be used as a reference. Note also that every row of data in the primary key must be unique to one another. Here is the statement required to create our database (called gamedata) and our playerdata table.

mysql> CREATE DATABASE gamedata;       
mysql> USE gamedata;       
mysql> CREATE TABLE playerdata (      
-> username CHAR(255) UNIQUE NOT NULL,      
-> password CHAR(255),      
-> age INT,      
-> datecreated TIMESTAMP,      
-> PRIMARY KEY(username));

Notice here how we set the username column to UNIQUE and also NOT NULL. In simple terms, this means that it must contain a value, and that value must not be the same as any other username in any other record in the table. Note also that we have set the primary key of the table to be the username field, as we will be mainly searching on this field, which you will see in a moment.

In addition to this information, we also need some way to store friends and enemies. This is done by means of a link table. A link table is really just a normal database table, but its main purpose is to relate data in some way or another to conserve space and optimize the way the database accesses the information.

Let's create two link tables, one for relating friends and one for relating enemies to each other. Following are the statements that are required to accomplish this.

Note

If link tables are used, the optimized way is to store INT values there and have an ID field with auto_increment in the playerdata table as a primary key. It is not as readable when you perform a SELECT, but it is faster from within your applications. When updating a player's name, it does not break the integrity of the database. Also, link tables are often structures to be used only when there are n amount of relations from one row to other rows. If there is always only one relation (one friend or enemy), a direct link should be used.

mysql> CREATE TABLE relatefriends (     
-> player CHAR(255),      
-> friend CHAR(255));

Also:

mysql> CREATE TABLE relateenemies (    
 -> player CHAR(255),    
 -> enemy CHAR(255));

If we now show the tables in the database with the following command:

mysql> SHOW TABLES;

...we can see our database now contains three different tables—our playerdata table and the two link tables.

Let's now add a sample of data to the player data table, so we can experiment with the link tables and understand the logic of how to use them effectively. Here is the statement required to add our sample data to the playerdata table:

mysql> INSERT INTO playerdata VALUES      
-> ('Andrew', 'qwerty', 20, NULL),      
-> ('Henry', 'letmein', 34, NULL),     
-> ('Sandra', 'dra33', 19, NULL),      
-> ('John', 'j12d', 23, NULL),      
-> ('Jenny', 'jen123', 34, NULL);

If we select all the information from the playerdata table now using the following command:

mysql> SELECT * FROM playerdata

Now that we have some sample data, let's try to create some relations between the players in the database. First add to the relatefriends link table the fact that Henry is friends with Sandra. Here is the statement required to add this to the link table:

mysql> INSERT INTO relatefriends VALUES      
-> ('Henry', 'Sandra');

Let's now add some more sample data into both the relatefriends and relateenemies link tables and see how we can manipulate the data. The two statements required to add in the sample data are below:

Note

To prevent being a friend and enemy at the same time, one relation table could be used. Just add a field "enemy" flag, and if it is set, it means that they are enemies; otherwise they are friends.

mysql> INSERT INTO relatefriends VALUES      
-> ('Andrew', 'Henry'),     
-> ('Andrew', 'John'),      
-> ('Andrew', 'Jenny'),     
-> ('Sandra', 'Jenny');

And also:

mysql> INSERT INTO relateenemies VALUES      
-> ('Andrew', 'Sandra'),      
-> ('Henry', 'Jenny'),      
-> ('Henry', 'John');  

Now that we have all of our sample data, let's see if we can find out who Andrew is friends with by using the following statement:

mysql> SELECT friend FROM relatefriends WHERE player = 'Andrew';

When we execute this statement, the console displays a list of all the players that Andrew is friends with.

Again, we can do exactly the same with the relateenemies link table. For example, we could find out all of Henry's enemies with the following statement:

mysql> SELECT enemy FROM relateenemies WHERE player = 'Henry'

With this data, if we then wanted to find out more information about Henry's enemy that has the username of Jenny, we would use the following statement:

mysql> SELECT * FROM playerdata WHERE username = 'Jenny';
Leave a comment or ask a question
Total comments: 0

Databases Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
Data Entry and the Various Industries that use this Service - Group of people flooding Excel sheets with alpha and numeric characters... spontaneous sound of pressing keyboards... these are what you will find in data entry service. From giant financial instit (more...)
Different Forms and Variations of Data Entry - Data entry is the procedure of feeding data into spreadsheets and database. It can either be done automatically by a machine that keeps on adding data in the database, or manually by a person who k (more...)
How Data Entry Services Benefit Industries And Why It Is Essential - It is irrelevant whether you have a small business or a global empire - information is an asset in any kind of business. And when it is business, everything comes down to profitability. Today's wor (more...)
What is meant by dirty data - Suppose you have leads or contacts in your CRM system that are missing key information, have bounced email addresses, are just plain incorrect, or if you have dupl (more...)
Managing Monitoring and Troubleshooting SQL Server 2000 - SQL Server Agent Jobs SQL Server Agent is a job scheduling agent that aids in the management of a SQL server. SQL Server Agent can be started when the operating system starts (more...)
Managing and Monitoring SQL Server 2000 Security - Authentication There are two methods of authentication offered SQL Server 2000, Windows authentication and SQL Server authentication. Windows authentication allows the SQL server (more...)
Basic Terms of Structured Query Language (SQL) - The first questions to ask are what is SQL and how do you use it with databases? SQL has three main roles: 1. Creating a database and defining its structure 2. Querying the d (more...)
Extracting and Transforming Data in SQL Server 2000 - SQL Server 2000 has several components that support the import and export of data. Data Transformation Services (DTS) is used to import and export data between like OLE DB (more...)
Introduction to SQL - SQL is an acronym for Structured Query Language and is the standard language for interaction with databases. SQL is both an ISO (International Organization for Standardization) and ANSI (Americ (more...)
What Is a Database - A database is simply an organized collection of information. It allows many different types of data to be stored and retrieved in a highly efficient manner. Information within a database (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.