Lab Ex 1
Lab Ex 1
1) To work in sqlite3
Open command prompt
Type sqlite3 and press enter
2) To open & create database
Type sqlite3 databasename.db
sqlite3 student.db
3) To list name of all created databases
Type .databases
4) To list name of all created tables
Type .tables
5) To check Dynamic type / Manifest Type
To create table
CREATE TABLE a(i INTEGER, n numeric, t text, b blob);
To insert data
sqlite> INSERT INTO a values(9.581,9.581,9.581,9.581);
sqlite> INSERT INTO a values('9.581','9.581','9.581','9.581');
sqlite> INSERT INTO a values(9581,9581,9581,9581);
To display records
sqlite> select * from a;
9.581|9.581|9.581|9.581
9.581|9.581|9.581|9.581
9581|9581|9581|9581
Example 2:
sqlite> create table s(rno primary key,nm,marks);
sqlite> insert into s values(1,'Het',90),(2,'Viral',85),(3,'Devsay',92);
sqlite> select * from s;
.mode box
select * from emp;
Example of rollback
sqlite> begin;
sqlite> insert into emp values(6,'Dhruvi','Vapi');
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
│ 6 │ Dhruvi │ Vapi │
└─────┴─────────┴─────────┘
sqlite> rollback;
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
Dr. Jyoti Rana
Naran Lala College, Navsari Page 2
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
└─────┴─────────┴─────────┘
Example of commit
sqlite> begin;
sqlite> insert into emp values(6,'Dhruvi','Vapi');
sqlite> insert into emp values(7,'Diya','Valsad');
sqlite> commit;
sqlite> select * from emp;
┌─────┬─────────┬─────────┐
│ eno │ enm │ city │
├─────┼─────────┼─────────┤
│ 1 │ Gaurav │ Navsari │
│ 2 │ Vishva │ Surat │
│ 3 │ Darshit │ Vapi │
│ 4 │ Dev │ Valsad │
│ 5 │ Dhruv │ Surat │
│ 6 │ Dhruvi │ Vapi │
│ 7 │ Diya │ Valsad │
Depttbl.sql
Dr. Jyoti Rana
Naran Lala College, Navsari Page 3
CREATE TABLE dept_master(dept_id INTEGER PRIMARY KEY AUTOINCREMENT,
dept_name TEXT);
To execute the query
.read depttbl.sql
.mode box
Select * from dept_master;
Deptins.sql
Insert following records into it:
Dept_id Dept_name
1 Admin
2 Sales
3 Quality Control
4 Marketing
emptbl.sql
empins.sql
Insert following records into it:
Emp_id First_name Last_name Salary Joining_date Dept_id
1 Dev Rana 50000 1-1-2010 1
2 Krishna Patel 30000 5-4-2021 1
3 Jhanvi Rana 70000 3-10-2006 1
4 Aditi Patel 65000 9-11-1979 2
5 Aryan Patel 55000 11-12-2005 2
6 Ahana Rastogi 44000 5-1-2003 3
7 Niti Gandhi 80000 18-8-2004 3
8 Dev Shah 75000 26-6-2009 3
9 Krishna Shah 60000 1-5-2010 2
10 Swayam Gandhi 20000 16-5-2010 1
5. Cat
Type Text
Color Text
Records
Type Color
Indoor white
Outdoor black
Records
Type Color
Haunting Black
Guard brown
insert into dog values('Haunting','Black'),('Guard','brown');
Records
Pub_id Pname
1 Tata Mc Graw hill
2 Edward
10.Book
Book_id Integer
Name Text
Price Real
Pub_id Integer
Records
Book_id Name Price Pub_id
1 Java 2000 1
Programming
2 Python Complete 3000 2
Reference
3 Sqlite Python 5000 1
4 Android 2500 1
Fundamentals
Queries
Example:
Aditya
Akash
Example:
┌────────┬────────┬───────┬─────────┬───────┐
│ rollno │ Name │ Age │ City │ Marks │
Example
SELECT * FROM STUDENT WHERE NAME LIKE 'A%';
┌────────┬────────┬─────────┬─────────┬───────┐
│ rollno │ Name │ Age │ City │ Marks │
├────────┼────────┼─────────┼─────────┼───────┤
│1 │ Aditya │ Patel │ Navsari │ 90.0 │
│2 │ Akash │ Gandhi │ Surat │ 80.0 │
│4 │ Aditi │ Patel │ Surat │ 70.0 │
│6 │ Ahana │ Rastogi │ Mumbai │ 95.0 │
│7 │ Aditya │ Shah │ Surat │ 93.0 │
└────────┴────────┴─────────┴─────────┴───────┘
SELECT * FROM emp_master WHERE first_name LIKE 's%';
┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│ 10 │ Swayam │ Gandhi │ 20000 │ 16-5-2010 │ 1 │
└────────┴────────────┴───────────┴────────┴──────────
SELECT * FROM emp_master WHERE last_name LIKE '%n%';
┌────────┬────────────┬───────────┬────────┬──────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼──────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │
│3 │ Jhanvi │ Rana │ 70000 │ 3-10-2006 │ 1 │
│7 │ Niti │ Gandhi │ 80000 │ 18-8-2004 │ 3 │
Syntax
Select * from tablename where column_name between value1 and value2;
Example
┌────────┬────────────┬───────────┬────────┬────────
│ emp_id │ first_name │ last_name │ salary │ jdate │ dept_id │
├────────┼────────────┼───────────┼────────┼─────────
│1 │ Dev │ Rana │ 50000 │ 1-1-2010 │ 1 │
SQLite IN Operator
In SQLite IN operator is used to determine whether the given value
matching with subquery returned result set values or list of given
values.
Example
Union All
It is used to combine the result sets of 2 or more SELECT statements
and it will return all the rows including duplicates.
It is useful to get only matching rows from two or more select statements.