SQLite

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

SQLite:

--------
->SQLite is a light weight RDBMS (Relational DataBase Management Software)
->helps us in maintaining data in an organized, secure manner.
->we maintain the data in Databases (which is a collection of tables)

sqlite3.exe

Create a new database file syntax:


or
open an existing database file syntax:
--------------------------------
.open dbfilename.sqlite

eg:
---
.open mydb.sqlite

--------------------------------------------

To view the list of tables in a database syntax:


------------------------------------------------
.tables

To create a new database table syntax:


---------------------------------------
CREATE TABLE tablename
(colname datatype,
colname datatype,
colname datatype);

datatypes in SQLite:
integer for saving numbers
real for saving decimal numbers
text for saving string

eg:
---
CREATE TABLE students
(roll integer,
name text,
m1 real,
m2 real,
per real);

To view a table's structure syntax:


----------------------------------
.schema tablename

eg:
--
.schema students

To ADd a new column in the table syntax:


-------------------------------------------
ALTER TABLE tablename
ADD COLUMN colname datatype;

eg:
--
ALTER TABLE students
ADD COLUMN phone text;

To drop a table's structure syntax:


-----------------------------------
DROP TABLE tablename;

eg:
---
DROP TABLE students;

To exit from SQLITE:


---------------------
.exit

===============================================

INSERT:
is used to insert / add a new row / record in the table.

syntax:
-------
INSERT INTO tablename
(colname, colname, colname)
VALUES(value1, 'value2', value3);

Rules:
->no. of values must match the no. of cols given
->the text values need to be written within ' '

eg:
---
INSERT INTO students
(roll, name, m1, m2)
VALUES (101, 'Siddharth', 89.5, 65.5);

INSERT INTO students


(roll, name, m1, m2)
VALUES (102, 'Vidyut', 44.5, 55.5);

INSERT INTO students


(roll, name, m1, m2)
VALUES (103, 'Amit', 23.5, 33.5);

INSERT INTO students


(roll, name, m1, m2)
VALUES (104, 'Sumit', 83.5, 93.5);
=================================================

To view all the records of a table:


SELECT * FROM tablename;
SELECT * FROM students;

=================================================

UPDATE:
-------
->is used to edit a row / record's value

syntax:
-------
UPDATE tablename
SET colname=newvalue , colname='newvalue'
WHERE condition;

Rules:
->WHERE is optional
->when we put WHERE, only some of the records get updated
->when we do not put WHERE, all of the records get updated
->text values to be written within ' '

eg:
---
Q1. add the m1 of roll no 102 by 3 marks
Query:
UPDATE students
SET m1=m1+3
WHERE roll=102;

Q2. subtract the m1 and m2 of Vidyut by 5 marks


Query:
UPDATE students
SET m1=m1-5 , m2=m2-5
WHERE name='Vidyut';

Q3. update the per of all the students in the table


Query:
UPDATE students
SET per = (m1+m2)/2.0;

=====================================================

DELETE:
-------
->is used to delete a row / record

syntax:
-------
DELETE FROM tablename
WHERE condition;

Rules:
->WHERE is optional
->when we put WHERE, only some of the records get deleted
->when we do not put WHERE, all of the records get deleted
->text values to be written within ' '

Q. Delete all the students whose per is below 20 %


Query:
DELETE FROM students
WHERE per < 20;

======================================================

SELECT:
-> records retrieval

syntax:
------
SELECT colname, colname, colname
FROM tablename
WHERE condition;

RUles:
->WHERE is optional
->if we put WHERE only some of the records will get retrieved
->if we do not put WHERE all of the records will get retrieved

!Q. display the records of the students who have scored per greater than 70

SELECT roll, name, per


FROM students
WHERE per > 70;

=====================================================

You might also like