Dec 16, 2013
Dec 10, 2013
Surrogate keys - system generated primary keys
This post should be a quick reference to how to generate surrogate keys in SQL Server.
(1) IDENTITY (column property)
: allows the database to generate keys for the column of a numeric types such as INT, BIGINT, SMALLINT, TINYINT.
CREATE TABLE guests
( GuestId int IDENTITY (100, 1) -- starting from 100 and increment by 1
, Firstname nvarchar (200)
,Lastname nvarchar (200)
,Email nvarchar (100) )
INSERT INTO guests(Firstname, Lastname, Email)
VALUES('Hoon', 'Lee', 'hoonerLee@hoon.com');
SELECT * FROM guests;
Studentid Firstname Lastname Email
100 Hoon Lee hoonerLee@hoon.com
(2) Sequence (database object)
: an independent object that can be shared by all the tables in the entire database. Numeric types.
Create SEQUENCE dbo.GuestID
AS BIGINT
START WITH 1000
INCREMENT BY 1 ;
GO
DECLARE @guestID as INT;
SELECT @guestID = NEXT VALUE FOR dbo.GuestID;
SELECT @guestID as GuestID; -> 10001
SELECT @guestID as GuestID; -> 10002
...
SELECT @guestID as GuestID; -> 10011
(3) Non-sequential GUIDs (Global Unique Identifiers)
: For UNIQUEIDENTIFIER type of column. Use NEWID() function to get a new GUID in INSERT statement or create a UNIQUEIDENTIFIER column with NEWID() as DEFAULT constraint.
CREATE TABLE students
( studentID UNIQUEIDENTIFIER NOT NULL
, Name VARCHAR(30) );
INSERT students (studentid, name) VALUES ( NEWID() , 'Hooner');
OR
CREATE TABLE students
( studentID UNIQUEIDENTIFIER DEFAULT NEWID()
, Name VARCHAR(30) );
INSERT students ( name) VALUES ( 'Hooner');
SELECT * FROM students;
studentID Name
A5971324-30BF-4781-B2B2-DA8C7B36C18F Hooner
4F79B592-3CCF-4C11-B85F-DCFFB2BD84D7 Hooner
F4D94A6B-9D5A-4D3C-A590-B91F3D462E2F Hooner
(4) Sequential GUIDs
: For UNIQUEIDENTIFIER type of column. Use NEWSEQUENTIALID() function with DEFAULT constraints of table columns to get a new GUID.
** The database generates a GUID that is greater than any GUID previously generated by the function since Windows was started. Whenever Windows restarts, the GUID starts again from a lower range, but it is still globally unique. Becuase GUIDs are generated sequentially, one can easily guess the next new GUID. This can become a privacy/security concern. NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function.
CREATE TABLE students
( studentID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID()
, Name VARCHAR(30) )
INSERT students ( name) VALUES ( 'Hooner')
select * from students
studentID Name
AD27E74A-1862-E311-9B46-0023AE0AD326 Hooner
AE27E74A-1862-E311-9B46-0023AE0AD326 Hooner
AF27E74A-1862-E311-9B46-0023AE0AD326 Hooner
B027E74A-1862-E311-9B46-0023AE0AD326 Hooner
B127E74A-1862-E311-9B46-0023AE0AD326 Hooner
8B48F850-1862-E311-9B46-0023AE0AD326 Hooner
------------------
Reference: Querying Microsoft SQL Server 2012 Training Kit by Itzik Ben-Gan, Dejan Sarka, Ron Talmage
http://technet.microsoft.com
(1) IDENTITY (column property)
: allows the database to generate keys for the column of a numeric types such as INT, BIGINT, SMALLINT, TINYINT.
CREATE TABLE guests
( GuestId int IDENTITY (100, 1) -- starting from 100 and increment by 1
, Firstname nvarchar (200)
,Lastname nvarchar (200)
,Email nvarchar (100) )
INSERT INTO guests(Firstname, Lastname, Email)
VALUES('Hoon', 'Lee', 'hoonerLee@hoon.com');
SELECT * FROM guests;
Studentid Firstname Lastname Email
100 Hoon Lee hoonerLee@hoon.com
(2) Sequence (database object)
: an independent object that can be shared by all the tables in the entire database. Numeric types.
Create SEQUENCE dbo.GuestID
AS BIGINT
START WITH 1000
INCREMENT BY 1 ;
GO
DECLARE @guestID as INT;
SELECT @guestID = NEXT VALUE FOR dbo.GuestID;
SELECT @guestID as GuestID; -> 10001
SELECT @guestID as GuestID; -> 10002
...
SELECT @guestID as GuestID; -> 10011
(3) Non-sequential GUIDs (Global Unique Identifiers)
: For UNIQUEIDENTIFIER type of column. Use NEWID() function to get a new GUID in INSERT statement or create a UNIQUEIDENTIFIER column with NEWID() as DEFAULT constraint.
CREATE TABLE students
( studentID UNIQUEIDENTIFIER NOT NULL
, Name VARCHAR(30) );
INSERT students (studentid, name) VALUES ( NEWID() , 'Hooner');
OR
CREATE TABLE students
( studentID UNIQUEIDENTIFIER DEFAULT NEWID()
, Name VARCHAR(30) );
INSERT students ( name) VALUES ( 'Hooner');
SELECT * FROM students;
studentID Name
A5971324-30BF-4781-B2B2-DA8C7B36C18F Hooner
4F79B592-3CCF-4C11-B85F-DCFFB2BD84D7 Hooner
F4D94A6B-9D5A-4D3C-A590-B91F3D462E2F Hooner
(4) Sequential GUIDs
: For UNIQUEIDENTIFIER type of column. Use NEWSEQUENTIALID() function with DEFAULT constraints of table columns to get a new GUID.
** The database generates a GUID that is greater than any GUID previously generated by the function since Windows was started. Whenever Windows restarts, the GUID starts again from a lower range, but it is still globally unique. Becuase GUIDs are generated sequentially, one can easily guess the next new GUID. This can become a privacy/security concern. NEWSEQUENTIALID is a wrapper over the Windows UuidCreateSequential function.
CREATE TABLE students
( studentID UNIQUEIDENTIFIER DEFAULT NEWSEQUENTIALID()
, Name VARCHAR(30) )
INSERT students ( name) VALUES ( 'Hooner')
select * from students
studentID Name
AD27E74A-1862-E311-9B46-0023AE0AD326 Hooner
AE27E74A-1862-E311-9B46-0023AE0AD326 Hooner
AF27E74A-1862-E311-9B46-0023AE0AD326 Hooner
B027E74A-1862-E311-9B46-0023AE0AD326 Hooner
B127E74A-1862-E311-9B46-0023AE0AD326 Hooner
8B48F850-1862-E311-9B46-0023AE0AD326 Hooner
------------------
Reference: Querying Microsoft SQL Server 2012 Training Kit by Itzik Ben-Gan, Dejan Sarka, Ron Talmage
http://technet.microsoft.com
Mar 31, 2013
CHAR vs VARCHAR in terms of performance
CHAR, NCHAR
- fixed type: the system always allocates the indicated size in memory.
- UPDATEs don't need data shifting or physical row expansion.
- If frequent UPDATES operation is anticipated for a column, CHAR/NCHAR types is preferred.
VARCHAR, NVARHCAR
- variable type: the system allocates variable size in memory depending on size of the value.
- Less storage is used
- Thus, the less there is for a query to read.
- Faster query performance.
- fixed type: the system always allocates the indicated size in memory.
- UPDATEs don't need data shifting or physical row expansion.
- If frequent UPDATES operation is anticipated for a column, CHAR/NCHAR types is preferred.
VARCHAR, NVARHCAR
- variable type: the system allocates variable size in memory depending on size of the value.
- Less storage is used
- Thus, the less there is for a query to read.
- Faster query performance.
Mar 25, 2013
Logical query processing order
The logical query processing order
(1) FROM
(2) WHERE
(3) GROUP BY
(4) HAVING
(5) SELECT
(6) ORDER BY
> "Each phase operates on one or more tables as inputs and returns a virtual table as output. The output table of one phase is considered the input of the next phase. This is in accord with operation on relations that yield a relation."
> "If you assign an alias to a table, you basically rename the table for the duration of the query. The original table name isn't visible anymore; only the alias is."
------------------
Reference: Querying Microsoft SQL Server 2012 Training Kit by Itzik Ben-Gan, Dejan Sarka, Ron Talmage
(1) FROM
(2) WHERE
(3) GROUP BY
(4) HAVING
(5) SELECT
(6) ORDER BY
> "Each phase operates on one or more tables as inputs and returns a virtual table as output. The output table of one phase is considered the input of the next phase. This is in accord with operation on relations that yield a relation."
> "If you assign an alias to a table, you basically rename the table for the duration of the query. The original table name isn't visible anymore; only the alias is."
------------------
Reference: Querying Microsoft SQL Server 2012 Training Kit by Itzik Ben-Gan, Dejan Sarka, Ron Talmage
Subscribe to:
Posts (Atom)