1. Basic - create db, table
SQL is a Structured query language
1. create / drop database
CREATE DATABASE database_name;
SHOW DATABASES;
USE database_name
DROP DATABASE database_name;
Common Errors and How to Avoid Them
- Database Already Exists
If you try to create a database with a name that already exists, you’ll see an error. To avoid this, either choose a new name or use theIF NOT EXISTSclause to only create the database if it doesn't already exist.
Example:
CREATE DATABASE IF NOT EXISTS GeeksForGeeks;
2. create table
CREATE TABLE Customer(
CustomerID INT PRIMARY KEY,
CustomerName VARCHAR(50),
LastName VARCHAR(50),
Country VARCHAR(50),
Age INT CHECK (Age >= 0 AND Age <= 99),
Phone int(10)
);
here the age column must specify the check criteria
To check weather the table has any criteria use command
SHOW CREATE TABLE tablename;
output
mysql> SHOW CREATE TABLE class1;
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| class1 | CREATE TABLE `class1` (
`id` int NOT NULL,
`name` varchar(50) DEFAULT NULL,
`lastname` varchar(50) DEFAULT NULL,
`age` int DEFAULT NULL,
`phone` int DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `class1_chk_1` CHECK (((`age` > 10) and (`age` < 15)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
mysql>
using the above command we can check wheather any table has a specific criteria
TRUNCATE TABLE:
○ Used to delete the data inside a table, but not the table itself.
○ Syntax – TRUNCATE TABLE table_name