FGGG
FGGG
A unique key is nearly the same as primary key. It is a candidate key to uniquely identify each row in a
table. A unique key or primary key comprises a single column or set of columns. No two distinct rows in a
table can have the same value or combination of values in those columns. Depending on its design, a table
may have arbitrarily many unique keys but at most one primary key. A unique key must uniquely identify
all possible rows that exist in a table and not only the currently existing rows. Examples of unique keys
are Social Security numbers associated with a specific person or ISBNs associated with a specific book.
Telephone books and dictionaries cannot use names or words or Dewey Decimal system numbers as
primary keys because they do not uniquely identify telephone numbers or words. A primary key is a
special case of unique keys. The major difference is that for unique keys the implicit NOT NULL constraint
is not automatically enforced, while for primary keys it is. Thus, the values in a unique key column may or
may not be NULL. Another difference is that primary keys must be defined using another syntax. Unique
keys as well as primary keys can be referenced by foreign keys.
Foreign key
A foreign key is linking two tables together, it must be a primary key in one table. It identifies a column
or a set of them in one table that refers to a column or columns in 10 another referenced table. The key
should uniquely identify a column or columns in the referenced table but it is not necessary for it to be
unique itself. The values in one row of the referencing columns must occur in a single row in the
referenced table. Thus, a row in the referencing table can’t contain values that don't exist in the
referenced table (except potentially NULL). This way references can be made to link information together
and it is an essential part of database normalization. Multiple rows in the referencing table may refer to
the same row in the referenced table. Most of the time, it reflects the one (master table, or referenced
table) to many (child table, or referencing table) relationship. The referencing and referenced table may
be the same table, i.e. the foreign key refers back to the same table. Such a foreign key is known in
SQL:2003 as a self-referencing or recursive foreign key. [11] A table can have more than one foreign keys,
which then can have a different referenced table. Foreign key depends on the primary key in the
referential table, that’s why cascading links between tables can be made with foreign keys. If these
relationships are not used correctly it can lead to serious problems in the database .
What is a Database?
A database is a separate application that stores a collection of data. Each database has
one or more distinct APIs for creating, accessing, managing, searching and replicating
the data it holds.
Other kinds of data stores can also be used, such as files on the file system or large
hash tables in memory but data fetching and writing would not be so fast and easy with
those type of systems.
RDBMS Terminology
Before we proceed to explain the MySQL database system, let us revise a few
definitions related to the database.
Table − A table is a matrix with data. A table in a database looks like a simple spreadsheet.
Column − One column (data element) contains data of one and the same kind, for example
the column postcode.
Row − A row (= tuple, entry or record) is a group of related data, for example the data of
one subscription.
Primary Key − A primary key is unique. A key value can not occur twice in one table. With a
key, you can only find one row.
Foreign Key − A foreign key is the linking pin between two tables.
Compound Key − A compound key (composite key) is a key that consists of multiple
columns, because one column is not sufficiently unique.
MySQL Database
MySQL is a fast, easy-to-use RDBMS being used for many small and big businesses.
MySQL is developed, marketed and supported by MySQL AB, which is a Swedish
company. MySQL is becoming so popular because of many good reasons −
MySQL is released under an open-source license. So you have nothing to pay to use it.
MySQL is a very powerful program in its own right. It handles a large subset of the
functionality of the most expensive and powerful database packages.
MySQL works on many operating systems and with many languages including PHP, PERL, C,
C++, JAVA, etc.
MySQL works very quickly and works well even with large data sets.
MySQL is very friendly to PHP, the most appreciated language for web development.
MySQL supports large databases, up to 50 million rows or more in a table. The default file size
limit for a table is 4GB, but you can increase this (if your operating system can handle it) to a
theoretical limit of 8 million terabytes (TB).
MySQL is customizable. The open-source GPL license allows programmers to modify the
MySQL software to fit their own specific environments.
MySQL uses many different data types broken into three categories −
Numeric
String Types.
INT − A normal-sized integer that can be signed or unsigned. If signed, the allowable range
is from -2147483648 to 2147483647. If unsigned, the allowable range is from 0 to
4294967295. You can specify a width of up to 11 digits.
TINYINT − A very small integer that can be signed or unsigned. If signed, the allowable
range is from -128 to 127. If unsigned, the allowable range is from 0 to 255. You can specify
a width of up to 4 digits.
SMALLINT − A small integer that can be signed or unsigned. If signed, the allowable range is
from -32768 to 32767. If unsigned, the allowable range is from 0 to 65535. You can specify a
width of up to 5 digits.
BIGINT − A large integer that can be signed or unsigned. If signed, the allowable range is
from -9223372036854775808 to 9223372036854775807. If unsigned, the allowable range is
from 0 to 18446744073709551615. You can specify a width of up to 20 digits.
FLOAT(M,D) − A floating-point number that cannot be unsigned. You can define the display
length (M) and the number of decimals (D). This is not required and will default to 10,2,
where 2 is the number of decimals and 10 is the total number of digits (including decimals).
Decimal precision can go to 24 places for a FLOAT.
DOUBLE(M,D) − A double precision floating-point number that cannot be unsigned. You can
define the display length (M) and the number of decimals (D). This is not required and will
default to 16,4, where 4 is the number of decimals. Decimal precision can go to 53 places for
a DOUBLE. REAL is a synonym for DOUBLE.
DATE − A date in YYYY-MM-DD format, between 1000-01-01 and 9999-12-31. For example,
December 30th, 1973 would be stored as 1973-12-30.
DATETIME − A date and time combination in YYYY-MM-DD HH:MM:SS format, between
1000-01-01 00:00:00 and 9999-12-31 23:59:59. For example, 3:30 in the afternoon on
December 30th, 1973 would be stored as 1973-12-30 15:30:00.
TIMESTAMP − A timestamp between midnight, January 1 st, 1970 and sometime in 2037.
This looks like the previous DATETIME format, only without the hyphens between numbers;
3:30 in the afternoon on December 30 th, 1973 would be stored as 19731230153000
( YYYYMMDDHHMMSS ).
YEAR(M) − Stores a year in a 2-digit or a 4-digit format. If the length is specified as 2 (for
example YEAR(2)), YEAR can be between 1970 to 2069 (70 to 69). If the length is specified
as 4, then YEAR can be 1901 to 2155. The default length is 4.
String Types
Although the numeric and date types are fun, most data you'll store will be in a string
format. This list describes the common string datatypes in MySQL.
CHAR(M) − A fixed-length string between 1 and 255 characters in length (for example
CHAR(5)), right-padded with spaces to the specified length when stored. Defining a length is
not required, but the default is 1.
BLOB or TEXT − A field with a maximum length of 65535 characters. BLOBs are "Binary
Large Objects" and are used to store large amounts of binary data, such as images or other
types of files. Fields defined as TEXT also hold large amounts of data. The difference between
the two is that the sorts and comparisons on the stored data are case sensitive on BLOBs
and are not case sensitive in TEXT fields. You do not specify a length with BLOB or TEXT.
ENUM − An enumeration, which is a fancy term for list. When defining an ENUM, you are
creating a list of items from which the value must be selected (or it can be NULL). For
example, if you wanted your field to contain "A" or "B" or "C", you would define your ENUM
as ENUM ('A', 'B', 'C') and only those values (or NULL) could ever populate that field.
To begin with, the table creation command requires the following details −
Syntax
Here is a generic SQL syntax to create a MySQL table −
CREATE TABLE table_name (column_name column_type);
submission_date DATE,
);
Field Attribute NOT NULL is being used because we do not want this field to be NULL. So, if a
user will try to create a record with a NULL value, then MySQL will raise an error.
Field Attribute AUTO_INCREMENT tells MySQL to go ahead and add the next available
number to the id field.
Keyword PRIMARY KEY is used to define a column as a primary key. You can use multiple
columns separated by a comma to define a primary key.
Example
Here is an example, which will create tutorials_tbl −
Enter password:*******
Database changed
mysql> CREATE TABLE tutorials_tbl(
-> );
mysql>
NOTE − MySQL does not terminate a command until you give a semicolon (;) at the
end of SQL command.