--Query for create a table
create table student(s_name varchar(10),s_address varchar(10),s_mob varchar(10))
create table teacher(t_name varchar(10),t_address varchar(10),t_mob varchar(10))
--Query for insert in a table
insert into student values('sanju','janakpuri','1234560')
insert into teacher values('amitesh','dawzarka','45zs6789')
--Query for select command with where clause
--choose only that names who comes after j
select * from student where s_name>='j'
--Query for select top two data from table
select top 2 * from student
--Query for select data in given pattern using like keyword--choose only that name ends with second last character as a
SELECT * FROM student
WHERE s_name LIKE '%a_'
---choose only that names starts with a or d or f or j
SELECT * FROM student
WHERE s_name LIKE '[a,d,f,j]%'
--choose only that names starts with a or n or d or j
SELECT * FROM student
WHERE s_name LIKE '[andj]%'
--Query for using alias name
select s_name as Name from student
--Query for select statement using where clause and in keyword
--choose only that names which belongs in in situation
SELECT * FROM student
WHERE s_name IN ('amitesh','j')
--Query for using between and and operator keyword
--choose only that names which belongs between these two name including them
SELECT * FROM student
WHERE s_name between 'amitesh' and 'j'
The UNION operator is used to combine the result-set of two or more SELECT statements.
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.
select s_name from student union select t_name from teacher
The UNION operator selects only distinct values by default. To allow duplicate values, use UNION ALL.Display both table differently.
select * from student unionall select * from teacher
The SELECT INTO statement selects data from one table and inserts it into a different table.
The SELECT INTO statement is most often used to create backup copies of tables.
select * into tt from student
--select into statement with where clause
select s_name into tt1 from student where s_name='amitesh'
Selecting data from more than one table is also possible.
The following example creates a "Persons_Order_Backup" table contains data from the two tables "Persons" and "Orders":
SELECT Persons.LastName,Orders.OrderNo
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
INTO Persons_Order_Backup
FROM Persons
INNER JOIN Orders
ON Persons.P_Id=Orders.P_Id
The CREATE DATABASE statement is used to create a database.
create database databasename
Constraints are used to limit the type of data that can go into a table.
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field.
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
create table detail(s_name varchar(10) not null,s_id int unique,s_address varchar(10) not null unique,unique(s_name))
To create a UNIQUE constraint on the column when the table is already created, use the following SQL:
ALTER TABLE student
ADD UNIQUE (s_name)
ALTER TABLE teacher
ADD CONSTRAINT uc_t_name UNIQUE (t_address,t_mob)
To drop a UNIQUE constraint, use the following SQL:
ALTER TABLE teacher
DROP CONSTRAINT uc_t_name
A Primary Key value can not contain null values
Each Table can have only one primary key.
create table student(s_id int primary key,s_name varchar(25))
When we want to make more than one column to make primary key then we use following syntax:
create table student(s_id int,s_name varchar(25),s_address varchar(25) contraint pk_student(s_id,s_name))
Alter Keyword used with Primary Key
alter table student add primary key (s_id int)
or,
alter table student add constraint pk_student primary key(s_id,s_name)
Drop Keyword used with Primary Key
Alter table student drop constraint pk_student
The "Persons" table:
| P_Id | LastName | FirstName | Address | City |
|---|---|---|---|---|
| 1 | Hansen | Ola | Timoteivn 10 | Sandnes |
| 2 | Svendson | Tove | Borgvn 23 | Sandnes |
| 3 | Pettersen | Kari | Storgt 20 | Stavanger |
The "Orders" table:
| O_Id | OrderNo | P_Id |
|---|---|---|
| 1 | 77895 | 3 |
| 2 | 44678 | 3 |
| 3 | 22456 | 2 |
| 4 | 24562 | 1 |
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)
To allow foreign key in multiple column we use this syntax..
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)
Alter Keyword is used with foreign key(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
)
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
or
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
Drop Keyword is used with Foreign Key
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders
SQL check constraint is used to limit the value range that can be placed in a column
If you define a CHECK constraint on a single column it allows only certain values for this column.
create table emp(e_id int check(e_id>5),e_name varchar(10),e_add varchar(10) )
If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.
create table company(c_id int,c_name varchar(25),c_add varchar(10), constraint chk_company check (c_id>2 and c_name='anand'))
Alter keyword use with check constraint:
alter table student add check(s_name>'amitesh')
alter table teacher add constraint chk_teacher check(t_name>'amitesh' and t_address='janakpuri')
Drop Keyword use with check constraint:
alter table teacher drop constraint chk_teacher
No comments:
Post a Comment