SQL Basics

SQL Basics

Viewing existing databases:

To view existing databases use : SHOW DATABASES;



 

 

 

 

 

 

 

Creating A Database: 

 To create a new database use: CREATE DATABASE [IF NOT EXISTS]  database_name;

  • The database_name is the name of the desired database we want to create, which should be meaningful and descriptive.
  • The IF NOT EXISTS is an optional statement which gives an error while creating a new database if it already exists. (But if the IF NOT EXISTS clause is used in MariaDB it will return a warning instead of an error if the specified database already exists.)

Eg. CREATE DATABASE JOHNDRIPPER; 




Now if we run SHOW DATABASES; again we will see the new database johndripper there.









Selecting a Database:

To select a database use the query: USE database_name;

Eg. USE JOHNDRIPPER;


 



Removing a Database:

Removing a database deletes the entire data and all its related objects associated with it permanently therefore use it cautiously.

To delete a database use the statement:

DROP DATABASE [IF EXISTS] database_name;

database_name is the name of the database we want to remove.

IF EXISTS clause is optional, it prevents us from removing the database that does not exist.

Eg. DROP DATABASE JOHNDRIPPER;





MYSQL TABLES:

A table consists of rows and columns. A column denotes a field/attribute and rows contain the data for each unit/record/tuple of the fields. The number of rows denotes the Cardinality of the table. The number of columns denotes the Degree of the table.

Rules for naming a table:
  • A table name can have a maximum of 30 characters.
  • It can contain alphanumeric characters viz A-Z, a-z and numbers 0-9.
  • A table name must begin with an alphabet.
  • Use of special character _(underscore) is allowed which is used for joining two words.
  • Reserved words are not allowed. eg CREATE, SELECT, ALTER.

 Creating a Table:

 To create a table use: CREATE TABLE [IF NOT EXISTS] <table_name> (
<column_name> <data_type> [size] [PRIMARY KEY] [NOT NULL | NULL],
- - - - - -,
);

column_name is the name of the column. It needs a data type and size eg COLUMN_1 VARCHAR(20) where column_1 is the name of the column varchar is a data type containing character and with a max size of 20.
The NOT NULL or NULL option indicates whether the column accepts NULL values or not.
PRIMARY KEY means that the column only accepts unique value.



Comments

Popular posts from this blog

Chapter 05 - File Handling

Dictionary (Python)

Tuple (Python)