Basics Flashcards
(28 cards)
Update statement
UPDATE
SET =, =
WHERE
insert statement
INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value3, …);
Declare variable
DECLARE @ =
varchar(n)
Variable width character string
nvarchar(n)
Variable width Unicode string
Bit
Integer that can be 0, 1, or NULL
tinyint
Allows whole numbers from 0 to 255
Smallint
Allows whole numbers between -32,768 and 32,767
Int
Allows whole numbers between -2,147,483,648 and 2,147,483,647
bigint
Allows whole numbers between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 8 bytes
decimal(p,s)
COALESCE()
returns the first non-null value in a list
COALESCE(val1, val2, …., val_n)
CAST()
Converts a value of any type into a specific datatype
CAST(value AS DataType)
ISNULL()
Checks to see if the first value is null, if so it is the second argument gets returned
ISNULL(expression, value)
ISNUMERIC()
Returns 1 if the value is a number, otherwise 0
CONCAT()
Adds two or strings together
Create table statement
Declare the table name, then list the columns and datatypes CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... );
Create temp table
It is the same as a create table statement, the table name should bee declared with #.
Primary key
The PRIMARY KEY constraint uniquely identifies each record in a table. you can declare a primary key when creating a table CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, .... PRIMARY KEY (column1) );
CONSTRAINT
SQL constraints are used to specify rules for data in a table.
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
Constraints are used to limit the type of data that can go into a table. This ensures the accuracy and reliability of the data in the table. If there is any violation between the constraint and the data action, the action is aborted.
Constraints can be column level or table level. Column level constraints apply to a column, and table level constraints apply to the whole table.
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
FOREIGN KEY - Uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
INDEX - Used to create and retrieve data from the database very quickly
CLUSTERED index
The rows are stored physically on the disk in the same order as the index. Therefore, there can be only one clustered index.
It is generally faster to read from a clustered index if you want to get back all the columns. You do not have to go first to the index and then to the table.
Writing to a table with a clustered index can be slower, if there is a need to rearrange the data.
NON CLUSTERED index
There is a second list that has pointers to the physical rows. You can have many non clustered indices, although each new index will increase the time it takes to write new records.
ALTER TABLE ADD COLUMN
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE DROP COLUMN
ALTER TABLE table_name
DROP COLUMN column_name;
Get current date
DECLARE @Today date = GETDATE();