Tuesday, 29 March 2016

Different Types of Keys in Database

To maintain data integrity we use concept of keys.There are nine types of keys in database.keys are used to fetch records from tables and to make relationship among tables or views.

                                                        1.Primary Key
                                                        2.Foreign Key
                                                        3.Super Key
                                                        4.Candidate key
                                                        5.Composite Key
                                                        6.Alternate Key
                                                        7.Unique Key
                                                        8.compound key
                                                        9.Surrogate key

1.Primary Key:
Primary key is a set of one or more fields/columns of a table that uniquely identify a record in database table.

Primary keys must contain unique values. There is only one primary key per table.

When multiple fields are used as a primary key, they are called a composite key.It can not accept null, duplicate values. 

Only one Candidate Key can be Primary Key.Primary key can be made foreign key into another table.

In SQL Server when we create primary key to any table then a clustered index is automatically created to that column.

Primary Key is the column you choose to maintain uniqueness in a table at row level.

2.Foreign Key:
Foreign keys are both a method of ensuring data integrity and a manifestation of the relationship between tables. It is also known as referential integrity.

Foreign Key is a field in database table that is Primary key in another table. 

It can accept multiple null, duplicate values. We can have more than one foreign key in a table.

Foreign key do not automatically create an index, clustered or non-clustered. You can manually create an index on foreign key.

3.Super Key:
A super key is any set of attributes for which the values are guaranteed to be unique for all possible sets of tuples in a table at all times.

Super key is a set of one or more keys that can be used to identify a record uniquely in a table.Primary key, Unique key, Alternate key are subset of Super Keys.

4.Candidate key:
A candidate key is simply the "shortest" super key. Candidate Key are individual columns in a table that qualifies for uniqueness of each row/tuple.

A candidate key is a "minimal" super key - meaning the smallest subset of super key attributes which are unique. 

Removing any attribute from a candidate key would therefore make it non-unique.

Candidate keys are those keys which is candidate for primary key of a table.Every table must have at least one candidate key but at the same time can have several.

In simple words we can understand that such type of keys which full fill all the requirements of primary key which is not null and have unique records is a candidate for primary key.

5.Composite Key:

A composite key contains at least one compound key and one more attribute. Composite keys may also include simple keys and non-key attributes.

When we create keys on more than one column then that key is known as composite key.

If a table do have a single column that qualifies for a Candidate key, then you have to select 2 or more columns to make a row unique. 

Composite Key is a combination of more than one fields/columns of a table. It can be a Candidate key, Primary key. It is also called the compound key.

6.Alternate Key:
If any table have more than one candidate key, then after choosing primary key from those candidate key, rest of candidate keys are known as an alternate key of  that table. 

Alternate key is a key that can be work as a primary key. Basically it is a candidate key that currently is not primary key.

7.Unique Key:
Unique key is a set of one or more fields/columns of a table that uniquely identify a record in database table. 

We can have more than one unique key in a table.Unique key is to prevent duplicate values in a column.

In SQL Server, Unique key can be made foreign key into another table.It is like Primary key but it can accept only one null value and it can not have duplicate values.

It can be a candidate key.You can’t change or delete primary values but Unique-key values can.

8.compound key:
A compound key is a key that consists of two or more attributes that uniquely identify an entity occurrence.A simple key is one that has only one attribute. 

Compound keys may be composed of other unique simple keys and non-key attributes, but may not include another compound key.

9.Surrogate key:
A surrogate key in a database is a unique identifier for either an entity in the modeled world or an object in the database. 

The surrogate key is not derived from application data, unlike a natural (or business) key which is derived from application data.

The surrogate is internally generated by the system but is nevertheless visible to the user or application.The value contains no semantic meaning

Example:

In student table have four fields such as  studentid, firstname.lastname,courseid
primary key : student id
Unique key : student id
Foreignkey : courseid
Alternate Key :  firstname +lastname
Composite Key:  firstname +lastname
Candidate keys : student id or firstname +lastname
Super key : {student id,firstname +lastname}

Saturday, 5 March 2016

Basics of Database

Database:
  • A database is an organized collection of data.
  • Database is the collection of schemas, tables, queries, reports, views and other objects.
  • In one view, databases can be classified according to types of content: bibliographic, full-text, numeric, and images.
  • A database is a collection of Tables, Schemas, Buffer pools, Logs, Storage groups and Table spaces working together to handle database operations efficiently.
  • To access information from a database, you need a DBMS. This is a collection of programs that enables you to enter, organize, and select data in a database.
  • You can create a database in instance using the “CREATE DATABASE” command.
DataBase Management System(DBMS):
  • A database management system (DBMS) is a computer software application that interacts with the user, other applications, and the database itself to capture and analyze data. 
  • A database management system (DBMS) is a collection of programs that manages the database structure and controls access to the data stored in the database.
  • DBMSs include MySQL, PostgreSQL,Microsoft SQL Server, Oracle, Sybase and IBM DB2.
  • Sometimes a DBMS is loosely referred to as a database.
  • The DBMS serves as the intermediary between the user and the database. 
Relational DataBase Management System(RDBMS):
  • RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. 
  • It is based on the relational model as invented by E.F.Codd.
  • Relational databases have often replaced legacy hierarchical databases and network databases because they are easier to understand and use
Levels of Database:
  • The external level defines how each group of end-users sees the organization of data in the database. A single database can have any number of views at the external level.
  • The conceptual level unifies the various external views into a compatible global view.It provides the synthesis of all the external views.
  • The internal level or physical level is the internal organization of data inside a DBMS. It is concerned with cost, performance, scalability and other operational matters.
Languages:
  • Data definition language – defines data types and the relationships among them
  • Data manipulation language – performs tasks such as inserting, updating, or deleting data occurrences
  • Query language – allows searching for information and computing derived information

Applications:
  • Support internal operations of organizations.
  • It is used to hold more specialized information.
Examples:
  • Computerized Library System
  • Telephone directories
  • Online Reservation System
  • School registers
Advantages:
  • Improved data sharing.
  • Integrity can be enforced
  • Minimized data inconsistency.
  • Providing Backup and Recovery
  • Improved data security.
  • Concurrency Control 
  • Data Atomicity
  • Controlling Redundancy
  • Cost of developing and maintaining system is lower
  • Data Independence
  • Support Transactions
  • Improved decision making
Disadvantages:

  • Cost of Staff Training is high
  • Cost of Hardware and Software
  • Damage occur in Database 
  • It is only efficient for particularly large organizations.
  • Occupy more size
  • Database systems are complex (due to data independence), difficult, and time-consuming to design.

Friday, 12 February 2016

Websites for execute SQL Online

SQL Editor is a coding editor that lets you work on your SQL code, either locally or by connecting directly to your online database.It helps to let you immediately execute your SQL statements and view the output.

Websites for execute SQL Online:

        This online SQL editor, you can edit the SQL statements, and click on a button to view the result.

        Use this panel to setup your database problem and execute the sql queries.











3) Tutorials Point

        This helps to execute our SQL statements and get the output for that queries.













4) Online SQL interpreter

        Online SQL interpreter. Enter some SQL queries and execute.






Thursday, 11 February 2016

Basics of SQL

  • SQL first appeared in 1974 and the initial release in 1986.
  • It is designed by Donald D.Chamberlin and Raymond F.Boyce.
  • The filename extension in sql is .sql.
  • Sql is developed by ISO/IEC and the standard is ISO/IEC 9075.
  • SQL Versions: SQL-86,89,92,1999,2003,2006,2008,2011.
  • SQL initially called SEQUEL(Structured English QUEry Language).
  • It is became a standard of the ANSI in 1986 and the ISO in 1987.
  • Transact-SQL (T-SQL) is an extension of SQL that is used in SQL Server.
  • The origins of the SQL take us back to the 1970s, when in the IBM laboratories, new database software was created - System R.
  • various open-source SQL database solutions such as MySQL, PostgreSQL, SQLite, Firebird, etc are there.
Definition of SQL:
  • SQL is responsible for querying and editing information stored in a certain database management system.
  • Structured Query Language (SQL) is a language used to view or change data in databases.
  • SQL, which stands for Structured Query Language, is a programming language that is used to communicate with and manipulate databases.
Advantages of SQL:
  • SQL runs on Windows,Linux, and Unix as well as Mac OS X.
  • It is easy to use.To hide data complexity.
  • It is a free and Open source.
  • Interactive language.
  • No coding required.
  • It is used to protect the data.
  • Portable.
  • High Speed.
  • Easy to learn and understand.
  • Restrict the access of a table so that nobody can insert the rows into the table. 
  • Client/Server language.
  • Dynamic.
  • Join two or more tables and show it as one object to user.
Disadvantages of SQL:
  • Interfacing is more complex in an SQL database.
  • It Suffers From Relatively Poor Performance Scaling. 


Thursday, 28 January 2016

How to install SQL into the System?

The following applications make it very easy to install both MySQL and PhpMyAdmin on your local machine:

  • WAMP for Windows OS
  • LAMP for Linux OS
  • MAMP for Mac OS
  • SAMP for Solaris OS

WAMP:
WAMP is created by Romain Bourdon.
WAMP includes four key elements like  "Windows, Apache, MySQL, and PHP".
It supports dynamic scripting languages like PHP,Python,Perl.
Wamp Server package is used to install and configure the three open source components.
It is an Windows application server platform.

  • Windows - Operating System
  • Apache - Web Server
  • MySql - Database
  • PHP - Scripting Language

Select Versions and Download WAMP server from this link.