SQL can be used to perform the following actions:
- Retrieve data
- Update data
- Delete data
- Create new tables and databases
- Add / remove users
- Assign permissions to these users
Command Line
mysql
utility can be used to interact with MySQL/MariaDB database.
-u
- username
-p
- password, should be passed empty so that password is prompted later.
Create Database
To list databases and choose a database:
Tables
e.g Create a table named logins
to store user data, using CREATE TABLE
SQL query:
To list tables:
DESCRIBE
- Used to list the table structure with its fields and data types:
Table Properties
AUTO_INCREMENT
- automatically increments the id by one every time a new item is added to the table:
NOT NULL
- ensures that a particular column is never left empty.
UNIQUE
- ensures that the inserted item are always unique.
DEFAULT
- Specify the default value:
PRIMARY KEY
- Used to uniquely identify each record in the table.
Below, we make the id
column the PRIMARY KEY
: