CREATE DATABASE Db_Name
To Create Table :-
CREATE TABLE Person
(
@ID INT NOT NULL,
@Name VARCHAR(20),
@ADDRESS VARCHAR(255) NOT NULL,
@PS_NO SMALLINT,
@FIRST_CHAR CHAR
)
if a column contains NOT NULL values then it means it does not allow the null values.
Unique :-
CREATE TABLE Person
(
@ID INT NOT NULL UNIQUE,
@Name VARCHAR(20),
@ADDRESS VARCHAR(255) NOT NULL,
@PS_NO SMALLINT,
@FIRST_CHAR CHAR
)
Another way to represent is and the way to define primary key :-
CREATE TABLE Person
(
@ID INT NOT NULL,
@Name VARCHAR(20),
@ADDRESS VARCHAR(255) NOT NULL,
@PS_NO SMALLINT,
@FIRST_CHAR CHAR,
UNIQUE(@ID),
PRIMARY KEY(@ID)
)
A primary key value can not have null values.
If more than one column has unique value then below way we will handle :-
UNIQUE(@ID, @ADDRESS)
FOREIGN KEY :-
If a table does not have primary key then it points to a primary key to another table then that key calls a foreign key for that table like :-
CREATE TABLE Person_Values
(
@P_ID INT NOT NULL,
@ID INT NOT NULL,
@P_Name VARCHAR(20),
@P_ADDRESS VARCHAR(255) NOT NULL,
@P_PS_NO SMALLINT,
@P_FIRST_CHAR CHAR,
FOREIGN KEY (@Id) REFERENCES Persons(@Id)
)
@ID is a primary key of Persons table and now working as a foreign key for Person_Values table.
UNION AND UNION ALL :-
Union will select the distinct values from both columns. In more general form Union with distinct values
Union All select all the values from selected columns.
UNION :-
SELECT column_name From table1
UNION
SELECT column_name From table2
UNION ALL :-
SELECT column_name From table1
UNION ALL
SELECT column_name From table2
0 comments:
Post a Comment