Some Feature not Supported in SQLite

Feature
Support
Not Support
Right Outer Join
No
Only Left Outer Join
Full Outer Join
No
Only Left outer Join
Alter Table
Rename Table
Add Column
Alter Table commands are supported
Drop Column
Alter Column
Add Constraint
Above commands are not supported
View
Yes(Read Only)
But View is read only
You can’t execute following command in view
Delete
Insert
Update



SQLite

How to create Database?

How to Get All Database Name?

SQLite>.Database
Above query retrieve all database name with location
“ Main “ is default database name .

How to Create New Database Name?
Database name same as “Main “but location of database storage we can store different name
Like “Testdb.db

Step 1:

Go to Run

Step 2:

Type CMD

Step 3:

Sync location. Where exe located
My Exe located in C:\Sqlite.exe

Step 4:

C:\>sqlite3.exe Sample.db
Example Screen below

.db file store location ? Is it store C:\Sample.db?
No
C:\Users\<YourName>\AppData\Local\VirtualStore\sample.db

How to modify above location?

Step 1:

Go to My computer

Step 2:

Right Click èProperty

Step 3:

Advanced System Settings

Step 4:

Environment variables

Step 5:


Note:

Whenever open New Sqlite window execute below query
C:\>sqlite3.exe Sample.db












SQLite

Define:

SQLite is an embedded relational database engine.

Other Names:
Self –Contained(no external dependencies)
Server Less
Zero – Configuration
Transactional Sql Database Engine

The SQLite engine is not a standalone process. Instead, it is statically or dynamically linked into the application

History:

Started Year 2000
SQLite was written in the C Programming language

Why SQLite?

The SQLite library is small
No Server
No installation
It is require less than 300 KIB
SQLite located any ware in directory
Cross platform file, it can be used various operating system(MAC OS,Windows,Linux,Unix)

step 1:

CREATE TABLE COLLEGE(SID NVARCHAR(10),SNAME NVARCHAR(20),PLACE NVARCHAR(20))

Step 2:

INSERT INTO COLLEGE VALUES(1,'SURESH',NULL),(NULL,'RAMESH','PUDUKKOTTAI'),(3,'RAMU','COIMBATORE')

-- ABOVE QUERY WILL RUN ONLY SQL 2008,2012

STEP 3:

SELECT COALESCE(SID,SNAME,PLACE) AS 'STUDNAME' FROM COLLEGE

STEP 4:

Post your Ans http://jssql.blogspot.in/







A trigger is a SQL procedure that initiates an action .when an event (INSERT,UPDATE,DELETE) occures. Triggers can be viewed as similar to stored procedure in that both consist of procedural logic that is stored at the database level.

STEP 1:

CREATE TABLE Student(SID int IDENTITY, SNAME varchar(10))

Create a trigger that displays the count student table when a row is inserted into the table to which it is attached.

STEP 2:

CREATE TRIGGER tr_student_insert
ON STUDENT
FOR INSERT
AS
SELECT COUNT(*) AS 'NUMBER OF RECORD' FROM Student

STEP 3:

INSERT INTO Student VALUES('SUTHAHAR')

RESULT



Use the inserted and deleted Tables

STEP 1:

CREATE TABLE STUMARK(SID int IDENTITY, MARK NUMERIC(10))

STEP 2:

CREATE TRIGGER tr_STUMARK_insert
ON STUMARK
FOR INSERT
AS
IF((SELECT MARK FROM inserted) < 40)
BEGIN
PRINT 'FAIL'
END
ELSE
BEGIN
PRINT 'PASS'
END

STEP 3:

INSERT INTO STUMARK VALUES(78)

OUTPUT


AFTER UPDATE

CREATE TRIGGER tr_STUDENT_UPDATE
ON STUDENT
AFTER UPDATE
AS
AFTER DELETE
CREATE TRIGGER tr_STUDENT_DELETE
ON DELETE
AFTER UPDATE
AS
INSTEAD OF TRIGGERS
CREATE TRIGGER tr_STUDENT_INSERT_InsteadOf
ON STUDENT
INSTEAD OF INSERT
AS
PRINT 'Updateable Views are Messy'
go




The CUBE,COMPUTE,COMPUTE BY and ROLLUP operators are useful in generating reports that contain subtotals and totals.

There are extensions of the GROUP BY clause.

The result set of a ROLLUP operation has functionality similar to that returned by a COMPUTE BY; however, ROLLUP has these advantages:
ROLLUP returns a single result set; COMPUTE BY returns multiple result sets that increase the complexity of application code.

ROLLUP can be used in a server cursor; COMPUTE BY cannot.

The query optimizer can sometimes generate more efficient execution plans for ROLLUP than it can for COMPUTE BY.

STEP 1:

create table JSSTUD(COL nvarchar(20),PER numeric(10))

STEP 2:

INSERT INTO JSSTUD VALUES('JJ COLLEGE',100)
INSERT INTO JSSTUD VALUES('JJ COLLEGE',200)
INSERT INTO JSSTUD VALUES('JJ COLLEGE',300)
INSERT INTO JSSTUD VALUES('PSG',150)
INSERT INTO JSSTUD VALUES('PSG',50)
INSERT INTO JSSTUD VALUES('PSG',1050)
INSERT INTO JSSTUD VALUES('GRG',2000)

STEP 3:

ROLLUP
SELECT COL,SUM(PER) FROM JSSTUD GROUP BY COL
WITH ROLLUP


OR
COMPUTE bY

SELECT COL,PER FROM JSSTUD ORDER BY COL
COMPUTE SUM(PER) BY COL



OR

SELECT COL,PER FROM JSSTUD ORDER BY COLCOMPUTE SUM(PER)

CUBE

select COL, sum(per)  from JSSTUD group by COL with CUBE

Error in SQL Server

Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 servers with SQL Server Configuration Manager. The specified module could not be found. [0x8007007e]


Solution 

Start ==> Run => Type below command
mofcomp "C:\Program Files (x86)\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"

Hope it will work for you .
CREATE TABLE WITHOUT PRIMARY KEY

CREATE TABLE JSSTUDTABLE (SNO NUMERIC(10) NOT NULL)ADD PRIMARY KEY EXISTING TABLE

ALTER TABLE JSSTUDTABLE ADD PRIMARY KEY (SNO);

REMOVE PRIMARY KEY EXISTING TABLE

ALTER TABLE JSSTUDTABLE DROP constraint PK__JSSTUDTA__CA1EE06C63D8CE75
“ COALESCE “ Method in Sql Server

Table Have so many column like that

Name
HomeAddress
OfficeAddress
Temp_Address
Suthahar
Null
Null
Pudukkottai
Suresh
Pullanviduthi
Null
Null
Sumathi
Null
Alangudi
Null
Sujatha
Pullanviduthi
Pudukkottai
Trichy

If someone have home address or office address suppose if you display available first record means you can use coalesce method
CREATE TABLE devenvexe(Name nvarchar(10),homeaddress nvarchar(10),officeaddress nvarchar(10), Temp_addressnvarchar(10))

Query :

SELECT name,COALESCE(homeaddree,officeaddress,temp_address) Addreess FROM devenvexe

Output:

Name
Address
Sutahhar
Pudukkottai
Suresh
Pullanviduthi
Sumathi
Alangudi
Sujatha
Pullanviduthi

Concatinate Column in Single Column

CREATE TABLE JS(SNAME NVARCHAR(10))
INSERT INTO JS VALUES('SUTHAHAR')
INSERT INTO JS VALUES('SURESH')
INSERT INTO JS VALUES('SUMATHI')
INSERT INTO JS VALUES('SUJATHA')

DECLARE @VAL NVARCHAR(1024)
SELECT @VAL=COALESCE(@VAL+',', '')+ SNAME FROM JS
SELECT JS= @VAL

Output:

Suthahar,Suresh,Sumathi,Sujatha

View in Sql Server

  • View is a virtual table
  • It s contains columns and data in different table
  • View does not contain any data directly. Its a set of select query
Table 1

Sno
Sname
Table 2
Sno
Lname
             
View_table1_table2

Sname
Lname

Above drawing table 1 and table we will write join query after we can create view

Syntax

CREATE VIEW JS_VIEW_NAME
AS
[SELECT STATEMENT]

Why we are use View ?
 View is used for security mechanism .if you restricted particular column for users .

Sql Server Syntex for View
CREATE VIEW
CREATE VIEW JS_VIEW
AS
SELECT *FROM STUD A,LIB L WHERE A.SNO=L.SON
ALTER VIEW
ALTER VIEW JS_VIEW
AS
SELECT *FROM STUDENTTABLE A,LIB L WHERE A.SNO=L.SON
SELECT VIEW
SELECT*FROM JS_VIEW
DROP VIEW:
DROP VIEW JS_VIEW
How many types of triggers are there in Sql Server 2005?
 There are two types of triggers
  •  Data Manipulation language (DML) triggers
  •  Data Definition language (DDL) triggers
DML triggers (implementation) will run when INSERT, UPDATE, or DELETE statements modify data in a specified table or view.

DDL triggers will run in response to DDL events that occur on the server such as creating, altering, or dropping an object, are used for database administration tasks

What are the different modes of firing triggers?

After Trigger: An AFTER trigger fires after SQL Server completes all actions successfully

Instead of Triggers: An INSTEAD OF trigger causes SQL Server to execute the code in the trigger instead of the operation that caused the trigger to fire.

Trigger is a special kind of Store procedure Modifications to the table are made using INSERT,UPDATE OR DELETE trigger will run

It is automatically run

Triggers prevent incorrect , unauthorized, or inconsistent changes to data.

Syntax in Trigger:

CREATE TRIGGER trigger_name ON table_name
FOR [INSERT/UPDATE/DELETE] AS
IF UPDATE(column_name)
{AND/OR} UPDATE(COLUMN_NAME)...]
{ sql_statements }

Trigger Rules:

  • A table can have only three triggers action per table : UPDATE ,INSERT,DELETE.
  • Only table owners can create and drop triggers for the table.This permission cannot be transferred.
  • A trigger cannot be created on a view or a temporary table but triggers can reference them.
  • They can be used to help ensure the relational integrity of database.On dropping a table all triggers associated to the triggers are automatically dropped .

INSERT TRIGGER

  • When an INSERT trigger statement is executed ,new rows are added to the trigger table and to the inserted table at the same time. 
  • The inserted table allows to compare the INSERTED rows in the table to the rows in the inserted table.

DELETE TRIGGER

When a DELETE trigger statement is executed ,rows are deleted from the table and are placed in a special table called deleted table.

UPDATE TRIGGER

When an UPDATE statement is executed on a table that has an UPDATE trigger,
Avoid Temp Table use Table Data Type

Micrsoft indroduced table data type in sql server altenative to using temporary table
Table variables store a set of records, so naturally the declaration syntax looks very similar to a

CREATE TABLE statement, as you can see in the following example:
DECLARE @Student TABLE
{
SNO INT ,SNAME NVARCAHR
}

IF YOU WANT INSERT RECORD
INSERT INTO @ Student VALUES(2,’JSSUTHHAAR’)
OR
INSERT INTO @Student SELECT SNO,SNAME FROM STUDENT
List Of Table Name In Sql Server

SELECT *FROM INFORMATION_SCHEMA.TABLESWHERE
Or
SELECT [name]FROM sys.tables;
table_type = 'BASE TABLE'

T-SQL Queries

1. 2 tablesEmployee Phone
empid
empname
salary
mgrid empid
phnumber

2. Select all employees who doesn't have phone?

SELECT DISTINCT m1.moviename
FROM MovieTable m1 INNER JOIN
MovieTable m2 ON m1.moviename = m2.moviename
WHERE (m1.person = 'amitabh' AND m2.person = 'vinod' OR
m2.person = 'amitabh' AND m1.person = 'vinod') AND (m1.role = 'actor')
AND (m2.role = 'actor')
ORDER BY m1.moviename

11. There are two employee tables named emp1 and emp2. Both contains
same structure (salary details). But Emp2 salary details are incorrect
and emp1 salary details are correct. So, write a query which corrects
salary details of the table emp2
update a set a.sal=b.sal from emp1 a, emp2 b where a.empid=b.empid

12. Given a Table named "Students" which contains studentid, subjectid
and marks. Where there are 10 subjects and 50 students. Write a Query
to find out the Maximum marks obtained in each subject.

13. In this same tables now write a SQL Query to get the studentid
also to combine with previous results.

14. Three tables – student , course, marks – how do go @ finding name
of the students who got max marks in the diff courses.

SELECT student.name, course.name AS coursename, marks.sid, marks.mark
FROM marks INNER JOIN
student ON marks.sid = student.sid INNER JOIN
course ON marks.cid = course.cid
WHERE (marks.mark =
(SELECT MAX(Mark)
FROM Marks MaxMark
WHERE MaxMark.cID = Marks.cID))

15. There is a table day_temp which has three columns dayid, day and
temperature. How do I write a query to get the difference of
temperature among each other for seven days of a week?

SELECT a.dayid, a.dday, a.tempe, a.tempe - b.tempe AS Difference
FROM day_temp a INNER JOIN
day_temp b ON a.dayid = b.dayid + 1
OR
Select a.day, a.degree-b.degree from temperature a, temperature b
where a.id=b.id+1

16. There is a table which contains the names like this. a1, a2, a3,
a3, a4, a1, a1, a2 and their salaries. Write a query to get grand
total salary, and total salaries of individual employees in one query.
SELECT empid, SUM(salary) AS salary
FROM employee
GROUP BY empid WITH ROLLUP
ORDER BY empid

17. How to know how many tables contains empno as a column in a database?

SELECT COUNT(*) AS Counter
FROM syscolumns
WHERE (name = 'empno')

18. Find duplicate rows in a table? OR I have a table with one column
which has many records which are not distinct. I need to find the
distinct values from that column and number of times it's repeated.

SELECT sid, mark, COUNT(*) AS Counter
FROM marks
GROUP BY sid, mark
HAVING (COUNT(*) > 1)

19. How to delete the rows which are duplicate (don't delete both
duplicate records).

SET ROWCOUNT 1
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.name1 = a.name1 AND
b.age1 = a.age1) > 1
WHILE @@rowcount > 0
DELETE yourtable
FROM yourtable a
WHERE (SELECT COUNT(*) FROM yourtable b WHERE b.name1 = a.name1 AND
b.age1 = a.age1) > 1
SET ROWCOUNT 0

20. How to find 6th highest salary

SELECT TOP 1 salary
FROM (SELECT DISTINCT TOP 6 salary
FROM employee
ORDER BY salary DESC) a
ORDER BY salary

21. Find top salary among two tables

SELECT TOP 1 sal
FROM (SELECT MAX(sal) AS sal
FROM sal1
UNION
SELECT MAX(sal) AS sal
FROM sal2) a
ORDER BY sal DESC

22. Write a query to convert all the letters in a word to upper case

SELECT UPPER('test')

23. Write a query to round up the values of a number. For example even
if the user enters 7.1 it should be rounded up to 8.

SELECT CEILING (7.1)

24. Write a SQL Query to find first day of month? 

SELECT DATENAME(dw, DATEADD(dd, - DATEPART(dd, GETDATE()) + 1,
GETDATE())) AS FirstDay
Datepart Abbreviations
year yy, yyyy
quarter qq, q
month mm, m
dayofyear dy, y
day dd, d
week wk, ww
weekday dw
hour hh
minute mi, n
second ss, s
millisecond ms

25. Table A contains column1 which is primary key and has 2 values (1,
2) and Table B contains column1 which is primary key and has 2 values
(2, 3). Write a query which returns the values that are not common for
the tables and the query should return one column with 2 records.

SELECT a.col1
FROM a, b
WHERE a.col1 <>
(SELECT b.col1
FROM a, b
WHERE a.col1 = b.col1)
UNION
SELECT b.col1
FROM a, b
WHERE b.col1 <>
(SELECT a.col1
FROM a, b
WHERE a.col1 = b.col1)

26. There are 3 tables Titles, Authors and Title-Authors. Write the
query to get the author name and the number of books written by that
author, the result should start from the author who has written the
maximum number of books and end with the author who has written the
minimum number of books.

27.

UPDATE emp_master
SET emp_sal =
CASE
WHEN emp_sal > 0 AND emp_sal <= 20000 THEN (emp_sal * 1.01) WHEN emp_sal > 20000 THEN (emp_sal * 1.02)
ENDSELECT fname
FROM employee
WHERE (empid IN
(SELECT empid
FROM city a
WHERE city IN
(SELECT city
FROM city b
GROUP BY city
HAVING COUNT(city) > 1)))

10. There is a table named MovieTable with three columns - moviename,
person and role. Write a query which gets the movie details where Mr.

Amitabh and Mr. Vinod acted and their role is actor.SELECT *
FROM employee LEFT OUTER JOIN
phone ON employee.empid = phone.empid
WHERE (phone.office IS NULL OR phone.office = ' ')
AND (phone.mobile IS NULL OR phone.mobile = ' ')
AND (phone.home IS NULL OR phone.home = ' ')

8. Find employee who is living in more than one city.
Two Tables:SELECT e1.empname AS EmpName, e2.empname AS ManagerName
FROM Employee e1 INNER JOIN
Employee e2 ON e1.mgrid = e2.empid
ORDER BY e2.mgrid

7. 2 tables emp and phone.
emp fields are - empid, name
Ph fields are - empid, ph (office, mobile, home). Select all employees
who doesn't have any ph nos.SELECT empname
FROM employee
WHERE (empid IN
(SELECT DISTINCT mgrid
FROM employee))

6. Write a Select statement to list the Employee Name, Manager Name
under a particular manager?SELECT TOP 3 empid, salary
FROM employee
ORDER BY salary DESC

5. Display all managers from the table. (manager id is same as emp id)
SELECT empname FROM employe WHERE (empid IN
(SELECT empid     FROM phone GROUP BY empid HAVING COUNT(empid) > 1))

4. Select the details of 3 max salaried employees from employee table.

SELECT empname
FROM Employee
WHERE (empid NOT IN
(SELECT DISTINCT empid
FROM phone))


3. Select the employee names who is having more than one phone numbers.
Emp City,Empid Empid,empName City,Salary

SELECT empname, fname, lname
FROM employee
WHERE (empid IN
(SELECT empid
FROM city
GROUP BY empid
HAVING COUNT(empid) > 1))

9. Find all employees who is living in the same city. (table is same
as above)

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts