DDL, DML, DCL, TCL, transaction, where 1=1 of SQL database common statements and concepts

For the convenience of description, suppose there is a table flow now, including fields pday, id, uv

DDL - Database Definition Language

The data definition language
database definition language is used to operate on the table, including: create creation, alter modify the fields in the table (add column, change column, delete column), drop delete, truncate truncation, comment comment, rename rename; note,
DDL Modifications that do not involve records do not require a commit command (commit, confirm, take effect) and cannot be rolled back.

BEGIN TRANSACTION;
--增加列
alter table flow add (vv bigint);
--更改列pday的类型
alter table flow modify (pday integer) ;
--更改字段默认值
alter table flow add default(1) for id with values;
--删除列
alter table flow delete (id);
alter table flow drop column id;
-- 根据约束名称删除约束
alter table flow drop constraint 约束名;

A few more words about alter
MySQL can use MODIFY COLUMN, ALTER COLUMN, and CHANGE COLUMN to modify column attributes. For some operations that only need to modify the table definition but not the table data, use the ALTER COLUMN operation to avoid data movement and improve the efficiency of the ALTER operation.
In general, alter column is used to set or delete the default value of the column very quickly; use change column to handle column renaming, type change and position movement; modify column is consistent with change column except that it cannot rename the column

-- 增加据表名向字段中增加新的默认值
alter table flow add default (0) for pday with values;

alter table flow alter column id set default 123;
alter table flow alter column id drop default;
--ID是id的新名字
alter table flow change column id ID bigint not null first;
alter table flow change column id ID bigint not null after pday;

alter table flow modify column id bigint not null after pday;

The operations that only need to modify the table structure are:
1. Changing the default value of the field
2. Adding and deleting the AUTO_INCREMENT attribute of the field (mainly adding attributes instead of adding fields)
3. Adding, deleting, and modifying the constant value of ENUM

For more, please refer to

DML - Database Manipulation Language

The data manipulation language
includes: select query records, insert insert records, update update/change records, delete delete one or more records in the table, explain plan execution plan, lock table lock table, merge, call; DML requires commit to take effect.

delete from flow where id=3;
update flow set id=4 where id=3;
insert into flow values(20200907,10086,55);

--注,以下from后面的子句都是可选的
select * ,sum(uv) as sum_uv from flow 
into flow1 
where id=3 
group by pday having sum(uv)>0
order by pday desc

DQL - Database Query Language

DCL - Database Control Language

The database control language is used to change or set user/role permissions, including: grant authorization, revoke deauthorization.

TCL - Transaction Control Language

Including: commit submission, rollback rollback, savepoint setting save point, you can roll back here.

transaction commit

Things
Transaction (Transaction) is the unit of concurrency control and an operation sequence defined by the user. These operations are either done or not done, and are an indivisible logical unit of work. A transaction binds a group of logically related operations together so that the server maintains data integrity and recoverability. For example, when multiple tables are updated, a certain execution fails. In order to maintain data integrity, transaction rollback needs to be used.
Reference
ACID
ACID refers to the acronym for the four basic elements of the correct execution of database transactions. Including: Atomicity, Consistency, Isolation, Durability. A database that supports transactions (Transaction) must have these four characteristics, otherwise the correctness of the data cannot be guaranteed in the transaction process (Transaction processing), and the transaction process is likely to fail to meet the requirements of the transaction party

The begin transaction command starts a transaction. In order to save the changes made and terminate the transaction, the END TRANSACTION command should be issued. If the transaction fails (such as a server failure or workstation failure, and Visual FoxPro exits without committing the transaction), or if the user issues a ROLLBACK command, the file in the transaction is restored to its original state.
Transaction submission
Only when the transaction is submitted to the database, the insertion, deletion and modification operations are considered to be completed. Before the transaction is submitted, only the person who operates the database has the right to see what he has done. Others can only see it after the submission is completed. arrive. There are three types of submission data:

  1. Explicit submission, use the COMMIT command to directly complete the submission of
    COMMIT;
  2. Implicit commit, done indirectly with SQL commands
    These commands are: CREATE, ALTER, DROP, RENAME, COMMENT, CONNECT, DISCONNECT, EXIT, GRANT, NOAUDIT, QUIT, REVOKE, AUDIT.
  3. Automatic submission, set AUTOCOMMIT to ON
    SET AUTOCOMMIT ON;

SHOW

  • SHOW COLUMNS FROM table: List the columns in the table with their data types and other attributes
  • Select current_user
  • SHOW TABLES [ FROM schema ] [LIKE pattern : List the tables in the specified library or the current library. Listed table names can be controlled with the LIKE clause
  • SHOW SCHEMAS [ FROM catalog ]: List catalog or libraries in the current catalog
  • SHOW SESSION (list current session attributes)
  • SHOW CATALOGS
  • DESCRIBE table_name: view an existing table structure
  • SHOW FUNCTIONS: List all functions available for the query
  • EXPLAIN [ ( option [, …] ) ] statement: Display the logical or distributed execution plan of a statement. where option is FORMAT { TEXT | GRAPHVIZ } or TYPE { LOGICAL | DISTRIBUTED }

Dynamic SQL: where 1=1

The main function of where 1=1 is to piece together dynamic SQL statements, that is, to avoid grammatical errors caused by the first word after the where keyword is directly "and", so as to facilitate program logic processing. Of course, improper use will also affect efficiency.

--使用场景:
lv_string = 'select tbl_name,tbl_desc from tbl_test where 1=1' +l_condition;
--当用户选择了查询的名称'abc'时
l_condition ='and tbl_name = ''abc'''
--但是当用户没有选择名称查询时l_condition就为空串''这样运行也不会出错,相当于没有限制名称条件。
lv_string = 'select tbl_name,tbl_desc from tbl_test where 1=1 '
--但如果没有1=1的条件,则报错
lv_string = 'select tbl_name,tbl_desc from tbl_test where '

Detailed explanation reference

1<>1 Usage

It is used when only taking structure but not data.
For example:
create table table_temp tablespace tbs_temp as
select * from table_ori where 1<>1
creates a table table_temp with the same structure as table_ori, but does not need the data in table_ori. (Except for the table structure, other structures are also the same)

insert into & insert overwrite

overwrite will overwrite the existing data (delete the data in the hive table first, and then perform the write operation), while into will directly write the data to the database (append data to the end of the hive table).
hive dynamic partition

Select Condition Conditional Query Simulation Analysis

Full scale and incremental scale

The full table represents all the data information, and the incremental table represents the newly added data. For example, for importing the user table information in mysql into hive, the import operation is performed every day. There can be incremental import and full import. At this time, the full Import is to import all the user information of the data every day, and incremental import is to import the new user information of the day, because the previous ones have been imported, and the subsequent imports only need to add new ones

Guess you like

Origin blog.csdn.net/weixin_43545069/article/details/108555972