In database design, it is a good practice to have a primary key for each table. There are two ways to specify a primary key: The first is to use part of the data as the primary key. For example, a table that includes information on employees may use Social Security Number as the primary key. This type of key is called a natural key. The second is to use a new field with artificially-generated values whose sole purpose is to be used as a primary key. This is called a surrogate key.
A surrogate key has the following characteristics:
Composite key, or composite primary key, refers to cases where more than one column is used to specify the primary key of a table. In such cases, all foreign keys will also need to include all the columns in the composite key. Note that the columns that make up a composite key can be of different data types.
Below is the SQL syntax for specifying a composite key:
The matchup for Super Bowl XLVII (47) has been set: San Francisco 49ers vs Baltimore Ravens.
The 49ers were the NFC West Champion and was the #2 seed in the NFC. They reached the Super Bowl by first beating the Green Bay Packers, 45-31, where quarterback Colin Kaepernick ran wild and set a playoff rushing record for quarterbacks. In the NFC Championship Game, the 49ers went to Atlanta and was able to overcome Atlanta’s strong passing game in the first half to win, 28-24.
Sometimes, it may be necessary to rename a table. There is no standard way to rename a table, and the implementation varies by RDBMS. Below we discuss how we can rename a table in MySQL, Oracle, and SQL Server.
In MySQL, we can rename a table using one of the following methods:
RENAME OLD_TABLE_NAME TO NEW_TABLE_NAME
ALTER TABLE OLD_TABLE_NAME
RENAME TO NEW_TABLE_NAME
We all know that the MAX function can be used to find the largest value in SQL. How, then, can we write a single-pass SQL that can be used across different database systems to find the second largest value in a column? Single-pass means only one SQL query gets executed, as opposed to having multiple SQL statements using temporary tables to store intermediate results.