SQL Statements - Common (Foundation)

Structured Query Language (SQL) is a standard computer language for accessing and processing databases. SQL is an ANSI (American National Standards Institute) standard computer language.

1. Classification of SQL Languages (Four Categories)

  • DML (Data Manipulation Language) is a data manipulation language:
    select, insert, update, delete, merge, call, explain, plan, lock table, etc.

  • DDL (Data Definition Language) is a data definition language:
    drop, create, alter, comment, replace, truncate, etc.

  • DCL (Data Control Language) is a database control language:
    grant ,revoke .

  • TCL (Transaction Control Language) is a transaction control language:
    Statements such as transaction, rollback, commit, set, savepoint, etc.

2. Basic operations of SQL statements

Select statement

select * from table_name;
select column_name,column_name from table_name;
//The distinct keyword is used to return a unique different value.
select  distinct column_name,column_name from table_name;
//where clause is used to extract records that meet specified criteria
select column_name,column_name from table_name where column_name operator value;
//The group by statement is used to group result sets according to one or more columns in conjunction with aggregation functions.
select column_name, aggregate_function(column_name) from table_name
where column_name operator value group by column_name;

AND & OR operator
If both the first and second conditions hold, the AND operator displays a record.

select * from table_name where Websites where country='CN' AND alexa > 50;//Example

If only one of the first and second conditions holds, the OR operator displays a record.

select * from Websites where country='USA' OR country='CN';//Example

update statement

update table_name set column1=value1,column2=value2 where some_column=some_value;

delete statement
Note: The where clause specifies which records or which records need to be deleted. If you omit the where clause, all records will be deleted!

delete from table_name where some_column=some_value;

insert into statement

insert into table_name values (value1,value2,value3,...);
insert into table_name (column1,column2,...) values (value1,value2,...);

create view statement
In SQL, views are visualized tables based on the result set of SQL statements.
Views contain rows and columns, just like a real table. A field in a view is a field in a real table from one or more databases. You can add SQL functions, WHERE, and JOIN statements to the view, or you can render data as if it came from a single table.

create view view_name AS select column_name(s) from table_name where condition

Create a database

create database database_name;

Delete the database

drop database database_name;

Create tables

create table table_name
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
name varchar(30);
primary key (O_Id),//Primary key
foreign key (P_Id) references Persons(P_Id)//Setting Foreign Key
)

See Specific Details of SQL Language Learning[ http://www.runoob.com/sql/sql-tutorial.html ]

Keywords: SQL Database

Added by raffielim on Thu, 04 Jul 2019 00:30:52 +0300