SQL Basics
A database is a collection of data that is organized in a manner that facilitates ease of access, as well as efficient management and updating.
A database is made up of tables that store relevant information.
For example, you would use a database, if you were to create a website like PHPGurukul which contains lots of information like videos, usernames, passwords, and comments.
id | userName | password |
id | videoTitle | videoDate |
id | userId | comment |
Database Tables
A table stores and display data in a structured format consisting of columns and rows that are similar to those in an Excel spreadsheet.
Databases often contain multiple tables, each designed for a specific purpose. For example, creating a database table of names and mobile numbers.
First, we would set up columns with titles firstName, lastName, and mobileNumber. Each table includes its own set of fields, based on the data it will store.
firstName | lastName | mobileNumber |
---|---|---|
Anuj | Kumar | 1234567890 |
Atul | Singh | 1122334455 |
Garima | Kapoor | 123123000 |
John | Doe | 2233112233 |
Rahul | Gupta | 1425366633 |
A table has a specified number of columns but can have any number of rows.
Primary Key
A Primary Key is a field in the table that uniquely identifies the table record.
The primary key’s features:
- It must contain a unique value for each row.
- It cannot contain NULL values.
For example, our table contains a record of each name in the phone book. The unique id number would be a good choice for a primary key in the table, as there is always the chance for more than one person to have the same name
id | firstName | lastName | mobileNumber |
---|---|---|---|
1 | Anuj | Kumar | 1234567890 |
2 | Atul | Singh | 1122334455 |
3 | Garima | Kapoor | 123123000 |
4 | John | Doe | 2233112233 |
5 | Rahul | Gupta | 1425366633 |
1 2 |
Tables are limited to one primary key each. The primary key's value must be different for each row. |
What is SQL?
SQL stands for Structured Query Language.
SQL is used to access and manipulate a database.
SQL can:
- Insert, update, or delete records in a database.
- Create new databases, tables, stored procedures, and views
- retrieve data from a database, etc