Fundamental SQL Statements
- Create: creating a basic table involves naming the table and defining its columns and each column’s data type.
Syntax :
CREATE TABLE “table_name”(
“column 1” “data type for column 1” [column 1 constraint(s)],
“column 2” “data type for column 2” [column 2 constraint(s)],
……
“column n”
[“table constraint(s)”]);
Example :
create table Engineer_table (
Cust_id int,
First_name varchar,
Last_name varchar,
age int,
email_id varchar);
)
Constraints define the type of data which will be entered into the table column.
Now I am defining the type of Constraints, so please look below for a better understanding of constraints:
- NOT NULL Constraint: Ensures that a column cannot have NULL value.
- DEFAULT Constraint: Provides a default value for a column when none is specified.
- UNIQUE Constraint: Ensures that all values in a column are different.
- CHECK Constraint: Makes sure that all values in a column satisfy certain criteria.
- Primary Key Constraint: Used to uniquely identify a row in the table.
- Foreign Key Constraint: used to ensure referential integrity of the data.
Keys
- A primary key is used to uniquely identify each row in a table and consist of one or more columns on a table.
- When multiple columns are used as a primary key, they are called a composite key.
- A foreign key is a column( or columns) that references a column (most often the primary key) of another table.
2. INSERT: The INSERT INTO statement is used to add new records into a database table.
Syntax:
INSERT INTO “table_name” (“column1″,”column2”,…) VALUES(“value1″,”value2”,…);
Example:
Single row (without column names specified)
INSERT INTO customer_table
VALUES(1, ‘chandan’,’Kumar’,23,’chandan@devopsschool.com’);
Single row (with column name specified)
INSERT INTO customer_table(cust_id,first_name,last_name,age,email_id)
VALUES(2, ‘Ajay’,’Kumar’,24,’ajay@devopsschool.com’);
Multiple rows
INSERT INTO customer_table
VALUES (1,’ck’,’dk’,35,’az@xyz.com’),
(2,’ak’,’pj’,36,’hg@xyz.com’);
- Top 10 DevOps Blogs and Websites To Follow in 2023 - December 13, 2022
- How To Set Up Apache Virtual Hosts on Ubuntu 20.04.2 LTS - October 28, 2021
- How to Fix ” Vue packages version mismatch:” error in Laravel 5.5 - April 15, 2021