Introduction to SQL
We have already learnt that databases and DBMS are key to organising and analysing data for business uses. From here on, let’s get busy working around with databases using SQL!
- SQL stands forStructured Query Language
- SQL is used to perform operations on Relational DBMS.
- SQL is declarative. Hence, easy to learn.
SQL provides multiple clauses (commands) to perform various operations like create, retrieve, update and delete the data.
The first step towards working with the database would be creating a table.
Create Table
Creates a new table in the database.
Syntax
Here,
Example
Create a
column_name | data_type |
---|---|
name | VARCHAR(200) |
age | INT/INTEGER |
score | INT/INTEGER |
We can check the details of the created table at any point in time using the
Try it Yourself!
Assume that we have to build a database that stores all the information about the students in a school, subjects, exam schedules, etc. Lets build a few tables to store the data!
- Create astudenttable to store the following details of students.
details | data_type |
---|---|
name | VARCHAR(200) |
date_of_birth | DATE |
address | TEXT |
- Create anexam_scheduletable to store the information about exams.
details | data_type |
---|---|
name | VARCHAR(200) |
course | VARCHAR(200) |
exam_date_time | DATETIME |
duration_in_sec | INT |
pass_percentage | FLOAT |
Data Types
Following data types are frequently used in SQL.
Data Type | Syntax |
---|---|
Integer | INTEGER / INT |
Float | FLOAT |
String | VARCHAR |
Text | TEXT |
Date | DATE |
Time | TIME |
Datetime | DATETIME |
Boolean | BOOLEAN |
- Boolean values are stored as integers 0 (FALSE) and 1 (TRUE).
- Date object is represented as: ‘YYYY-MM-DD’
- Datetime object is represented as: ‘YYYY-MM-DD HH:MM:SS’
PRAGMA
Syntax
Example
Let's find out the information of the
Output
cid | name | type | notnull | dflt_value | pk |
---|---|---|---|---|---|
0 | employee_id | INTEGER | 0 | 0 | |
1 | name | VARCHAR(200) | 0 | 0 | |
2 | salary | INTEGER | 0 | 0 |
Try it Yourself!
Try checking out the information of the tables that you have created above.