You are on page 1of 3

DATABASE COMMANDS(DB):

-CREATE DATABASE databasename;


-DROP DATABASE databasename;
-BACKUP DATABASE databasename TO DISK = 'filepath';

-USE databasename; -- select database


-----------------------------------------------------------------------------------
--------------------------------
TABLE COMMANDS:

CREATE TABLE Persons (


PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

CREATE TABLE TestTable AS -- create from another table


SELECT customername, contactname
FROM customers;
-----------------------------------------------------------------------------------
--------------------------------
DROP TABLE Shippers;

TRUNCATE TABLE table_name; -- clean table


-----------------------------------------------------------------------------------
--------------------------------
ALTER TABLE Customers -- add column
ADD Email varchar(255);

ALTER TABLE Customers -- drop column


DROP COLUMN Email;

ALTER TABLE Customers -- modify column


MODIFY COLUMN column_name datatype;
-----------------------------------------------------------------------------------
--------------------------------
CONSTRAINTS:
-constraints are column & table level
-can be applied during table creation or after creation.
-constraints enforce rules on tables whenever new record inserted, updated &
deleted.
-it prevents table deletion if there is a dependency on other table.
-----------------------------------------------------------------------------------
--------------------------------
-NOT NULL: enforces a column to NOT accept NULL values.
-----------------------------------------------------------------------------------
--------------------------------
-UNIQUE: ensures that all values in a column are different.
-----------------------------------------------------------------------------------
--------------------------------
-PRIMARY KEY: combination of NOT NULL & UNIQUE.

ID int PRIMARY KEY, -- single column

CONSTRAINT PK_Person PRIMARY KEY (ID,LastName) -- multiple columns primary


key
ALTER TABLE Persons -- add primary key as
constraint
ADD PRIMARY KEY (ID);

ALTER TABLE Persons -- droping constraint


DROP PRIMARY KEY;
-----------------------------------------------------------------------------------
--------------------------------
FOREIGN KEY:
-constraint is used to prevent actions that would destroy links between tables.
-a field (or collection of fields) in one table, that refers to the PRIMARY KEY in
another table.

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);

ALTER TABLE Orders -- adding


foreign key
ADD FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);

ALTER TABLE Orders -- droping


constraint
DROP FOREIGN KEY FK_PersonOrder;
-----------------------------------------------------------------------------------
--------------------------------
CHECK:
-CHECK constraint is used to limit the value range that can be placed in a column.
-CHECK constraint on a column it will allow only certain values for this column.

CREATE TABLE Persons (


ID int NOT NULL,
Age int,
CHECK (Age>=18)
);
-----------------------------------------------------------------------------------
--------------------------------
DEFAULT:
-DEFAULT constraint is used to set a default value for a column.

CREATE TABLE Persons (


ID int NOT NULL,
City varchar(255) DEFAULT 'Sandnes'
);
-----------------------------------------------------------------------------------
--------------------------------
INDEX:
-Indexes are used to retrieve data from the database more quickly than otherwise.
-The users cannot see the indexes, they are just used to speed up searches/queries.
-Updating a table with indexes takes more time than updating a table without
(because the indexes also need an update).

CREATE INDEX idx_lastname -- single column index


ON Persons (LastName);
CREATE INDEX idx_pname -- multiple column index
ON Persons (LastName, FirstName);

ALTER TABLE table_name -- drop index


DROP INDEX index_name;
-----------------------------------------------------------------------------------
--------------------------------
AUTO_INCREMENT:
-Auto-increment allows a unique number to be generated automatically when a new
record is inserted into a table.

CREATE TABLE Persons (


Personid int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Personid)
);

ALTER TABLE Persons AUTO_INCREMENT=100; -- To start the sequence with


another value
-----------------------------------------------------------------------------------
--------------------------------
VIEW:
-a view is a virtual table based on the result-set of an SQL statement.
-A view contains rows and columns, just like a real table.
-The fields in a view are fields from one or more real tables in the database.

CREATE VIEW [Products Above Average Price] AS


SELECT ProductName, Price
FROM Products
WHERE Price > (SELECT AVG(Price) FROM Products);

SELECT * FROM [Products Above Average Price];

**********************************************
CREATE OR REPLACE VIEW [Brazil Customers] AS
SELECT CustomerName, ContactName, City
FROM Customers
WHERE Country = 'Brazil';

**********************************************
DROP VIEW [Brazil Customers];
-----------------------------------------------------------------------------------
--------------------------------
DATE:
SELECT * FROM Orders WHERE OrderDate='2008-11-11' or '2021-05-11 08:48:16'

You might also like