Thursday, April 26, 2012

RDBMS Keys

Key

A key is a single or combination of multiple fields. Its purpose is to access or retrieve data rows from table according to the requirement. The keys are defined in tables to access or sequence the stored data quickly and smoothly. They are also used to create links between different tables.


Types of Keys

The following tables or relations will be used to define different types of keys.


Primary Key

The attribute or combination of attributes that uniquely identifies a row or record in a relation is known as primary key.


Foreign Key

A foreign key is an attribute or combination of attribute in a relation whose value match a primary key in another relation. The table in which foreign key is created is called as dependent table. The table to which foreign key is refers is known as parent table.





Secondary key

A field or combination of fields that is basis for retrieval is known as secondary key. Secondary key is a non-unique field. One secondary key value may refer to many records.


Candidate Key or Alternate key

A relation can have only one primary key. It may contain many fields or combination of fields that can be used as primary key. One field or combination of fields is used as primary key. The fields or combination of fields that are not used as primary key are known as candidate key or alternate key.



Composite key or concatenate key

A primary key that consists of two or more attributes is known as composite key.


SQL Fundamentals - DML

 DML (Data Manipulation Language)


The Data Manipulation Language (DML) is used to retrieve, insert and modify database information. These commands will be used by all database users during the routine operation of the database. Let's take a brief look at the basic DML commands:

INSERT

The INSERT command in SQL is used to add records to an existing table. Returning to the personal_info example from the previous section, let's imagine that our HR department needs to add a new employee to their database. They could use a command similar to the one shown below:

INSERT INTO personal_info
values('bart','simpson',12345,$45000)

Note that there are four values specified for the record. These correspond to the table attributes in the order they were defined: first_name, last_name, employee_id, and salary.

SELECT

The SELECT command is the most commonly used command in SQL. It allows database users to retrieve the specific information they desire from an operational database. Let's take a look at a few examples, again using the personal_info table from our employees database.

The command shown below retrieves all of the information contained within the personal_info table. Note that the asterisk is used as a wildcard in SQL. This literally means "Select everything from the personal_info table."

SELECT *
FROM personal_info

Alternatively, users may want to limit the attributes that are retrieved from the database. For example, the Human Resources department may require a list of the last names of all employees in the company. The following SQL command would retrieve only that information:

SELECT last_name
FROM personal_info

Finally, the WHERE clause can be used to limit the records that are retrieved to those that meet specified criteria. The CEO might be interested in reviewing the personnel records of all highly paid employees. The following command retrieves all of the data contained within personal_info for records that have a salary value greater than $50,000:

SELECT *
FROM personal_info
WHERE salary > $50000

UPDATE

The UPDATE command can be used to modify information contained within a table, either in bulk or individually. Each year, our company gives all employees a 3% cost-of-living increase in their salary. The following SQL command could be used to quickly apply this to all of the employees stored in the database:

UPDATE personal_info
SET salary = salary * 1.03

On the other hand, our new employee Bart Simpson has demonstrated performance above and beyond the call of duty. Management wishes to recognize his stellar accomplishments with a $5,000 raise. The WHERE clause could be used to single out Bart for this raise:

UPDATE personal_info
SET salary = salary + $5000
WHERE employee_id = 12345

DELETE

Finally, let's take a look at the DELETE command. You'll find that the syntax of this command is similar to that of the other DML commands. Unfortunately, our latest corporate earnings report didn't quite meet expectations and poor Bart has been laid off. The DELETE command with a WHERE clause can be used to remove his record from the personal_info table:

DELETE FROM personal_info
WHERE employee_id = 12345

SQL Fundamentals - DDL

DDL(Data Defination Language)


SQL commands can be divided into two main sublanguages.

 The Data Definition Language (DDL) is used to create and destroy databases and database objects. These commands will primarily be used by database administrators during the setup and removal phases of a database project. Let's take a look at the structure and usage of four basic DDL commands: 

CREATE

Installing a database management system (DBMS) on a computer allows you to create and manage many independent databases. For example, you may want to maintain a database of customer contacts for your sales department and a personnel database for your HR department. The CREATE command can be used to establish each of these databases on your platform. For example, the command:

CREATE DATABASE employees

creates an empty database named "employees" on your DBMS. After creating the database, your next step is to create tables that will contain data. (If this doesn't make sense, you might want to read the article Microsoft Access Fundamentals for an overview of tables and databases.) Another variant of the CREATE command can be used for this purpose. The command:

CREATE TABLE personal_info (first_name char(20) not null, last_name char(20) not null, employee_id int not null)

establishes a table titled "personal_info" in the current database. In our example, the table contains three attributes: first_name, last_name and employee_id. Don't worry about the other information included in the command -- we'll cover that in a future article.

USE

The USE command allows you to specify the database you wish to work with within your DBMS. For example, if we're currently working in the sales database and want to issue some commands that will affect the employees database, we would preface them with the following SQL command:

USE employees

It's important to always be conscious of the database you are working in before issuing SQL commands that manipulate data.  


LTER

Once you've created a table within a database, you may wish to modify the definition of it. The ALTER command allows you to make changes to the structure of a table without deleting and recreating it. Take a look at the following command:

ALTER TABLE personal_info
ADD salary money null

This example adds a new attribute to the personal_info table -- an employee's salary. The "money" argument specifies that an employee's salary will be stored using a dollars and cents format. Finally, the "null" keyword tells the database that it's OK for this field to contain no value for any given employee.

DROP

The final command of the Data Definition Language, DROP, allows us to remove entire database objects from our DBMS. For example, if we want to permanently remove the personal_info table that we created, we'd use the following command:

DROP TABLE personal_info

Similarly, the command below would be used to remove the entire employees database:

DROP DATABASE employees

Use this command with care! Remember that the DROP command removes entire data structures from your database. If you want to remove individual records, use the DELETE command of the Data Manipulation Language.

Structured Query Language (SQL)

Structured Query Language (SQL)


  Most large-scale databases use the Structured Query Language (SQL) to facilitate user and administrator interactions. This language offers a flexible interface for databases of all shapes and sizes. 

The first important point to make is that [em]all[/em] database transactions are made in SQL, whether you realize it or not. Nowadays, there are a large number of graphical user interfaces (GUIs) that simplify database administration tasks. If you're a SQL Server user, you may be familiar with tools like Enterprise Manager.

MySQL users may use any of a number of front ends. There are also quite a few third-party applications that interact with different databases (in fact, many of these can work with multiple database platforms simultaneously.) Did you ever wonder how these applications work? That’s right! They use SQL! The front-end translates your mouse clicks and text entries into SQL and then “speaks” to the database in the universal language of SQL.  


Flavors of SQL

SQL comes in many flavors. Oracle databases utilize their proprietary PL/SQL. Microsoft SQL Server makes use of Transact-SQL. However, all of these variations are based upon the industry standard ANSI SQL.


DDL and DML

SQL commands can be divided into two main sublanguages. The Data Definition Language (DDL) contains the commands used to create and destroy databases and database objects. After the database structure is defined with DDL, database administrators and users can use the Data Manipulation Language to insert, retrieve and modify the data contained within it.

DDL Definition: The Data Definition Language (DDL) is one of two major components of the Structured Query Language (SQL). It is used to alter the structure of tables within a relational databases. Some of the major commands comprising DML are CREATE TABLE, DROP TABLE and CREATE INDEX.

DML Definition: The Data Manipulation Language (DML) is one of two major components of the Structured Query Language (SQL). It is used to insert, retrieve and modify data stored within a relational databases. The major commands comprising DML are SELECT, INSERT, DELETE and UPDATE.

Top Five Things Beginners Need to Know About Databases

 

1. SQL Forms the Core of Relational Databases

 You can't avoid it. The Structured Query Language forms the core of all relational databases. It provides a uniform interface to Oracle, SQL Server, Access and other relational databases and is a "must learn" for all aspiring database users. In fact, I encourage you to take an introductory SQL course before you even attempt to learn any specific database software. The investment of time will help you build a proper foundation and get started in the world of databases on the correct foot. For a quick start, read SQL Basics or, for a more comprehensive introduction, take our free Learning SQL e-course.

 

2. Selecting Primary Keys is an Extremely Important Decision

The selection of a primary key is one of the most critical decisions you’ll make in the design of a new database. The most important constraint is that you must ensure that the selected key is unique. If it’s possible that two records (past, present, or future) may share the same value for an attribute, it’s a poor choice for a primary key. When evaluating this constraint, you should think creatively. You'll also need to avoid sensitive values, such as Social Security Numbers, as they raise privacy concerns.

 

3. NULL Is Not Zero or the Empty String

NULL is a very special value in the world of databases, but it's something that beginners often get confused about. When you see a NULL value, interpret it as "unknown". If a quantity is NULL, that doesn't necessarily mean that the quantity is zero. Similarly, if a text field holds a NULL value, that doesn't mean that there isn't an appropriate value, it's simply unknown. For example, consider a database containing information about children who attend a particular school. If the secretary entering the record does not know a student's age a NULL value is used to indicate the "unknown" placeholder. The student certainly has an age, it's just not present in the database.That value is not Zero.

4. Converting Spreadsheets to Databases Saves Time

If you already have tons of data stored in Excel (or other) spreadsheets, you can save yourself mountains of time by converting those spreadsheets into database tables. 

5. All Database Platforms Are NOT Created Equal

There are many different databases out there and all offer a variety of different features at different price points. Some are full-featured enterprise databases designed to host huge data warehouses serving multinational enterprises. Others are desktop databases better suited to tracking inventory for a small store with one or two users. Your business requirements will dictate the appropriate database platform for your needs.