sql basic knowledge (notes) (three)

sql basic knowledge (notes) (three)

Personal study notes

Continued

sql basic knowledge (notes) (1)

sql basic knowledge (notes) (two)

DDL part:

Build a table:

create table 表名
(
	列名 数据类型(长度),
	列名 数据类型(长度),
	列名 数据类型(长度)
);
comment on table 表名 is '注释内容';
comment on column 表名.列名 is '注释内容';

1. Selection of table name: try to reflect the function of the table, word abbreviation and underscore composition
2. Column name naming: reflect the content of column data, word abbreviation and underline composition
3. Both tables and columns need to have comments;
4. Default values ​​and Non-empty check:

create table 表名
(
	列名 数据类型(长度) default 0,
	列名 数据类型(长度) defualt sys_guid(),--32位
	列名 数据类型(长度) not null
);

New row:

alter table 表名 add 列名 数据类型(长度);--加一列
alter table 表名 add (列名 数据类型(长度), 列名 数据类型(长度),列名 数据类型(长度));

Delete column:

alter table 表名 drop column 列名;--删除一列
alter table 表名 drop(列名1,列名2,列名3);--删除多列

Modify column:

alter table 表名 modify 列名 新的数据类型(新的长度);

1. When modifying the column type, the column must be empty;
2. The column length can be expanded, but the number type cannot be reduced, and the string type is reduced to the maximum length of the existing data;

Table rename:

alter table 表名 rename to 新表名;

Column rename:

alter table 表名 rename column 列名 to 新列名;

view

Basic syntax:

 create or replace view as select 语句;

select can be a basic filtering query, or a statement that uses group by, having
1. The basic function of the view: shielding data;
2. Not all views can be updated;
3. The view can not improve the efficiency of the query. effect;

Temporary tables

Basic syntax:

create global temporary table
(
	列名 数据类型(长度),
	列名 数据类型(长度),
	列名 数据类型(长度)
) on commit delete rows;--提交删除数据 事务级别

create global temporary table
(
	列名 数据类型(长度),
	列名 数据类型(长度),
	列名 数据类型(长度)
) on preserve delete rows;--关闭窗口删除数据 session级别

In addition, on commit preserve rows, similar to on preserve delete rows, delete data at the end of the session

1. The temporary table occupies the temporary table space;

create user 用户名 identified by 密码 default tablespace 固定表空间 temporary tablespace 临时表空间;

index

Index concept: to speed up the query, the same as the index page of the dictionary

Normal index

create index 索引名字 on 表名(列名);

1. The index name starts with IND or IDX;
2. A single-column index, each column has only one single-column index;
3. The length of the index name is limited to 30 bits;
4. The index column is selected with high uniqueness (less data duplication);
5 The index of each table should be kept within 5 as far as possible;

Unique index:

create unique index 索引名 on 表名(列名);--要求索引列里的数据唯一(不重复),但可以为空;

Composite index

create index 索引名 on 表名(1,2);

1. The order of column 1 determines the order in which the conditions are written;
2. The column with high uniqueness is placed first;
3. When using a composite index, the first index column is required;

constraint

non empty:

1.alter table table name modify column name not null;
2.alter table table name modify column name null; -the value of the modified column can be empty, not that the value of the column is changed to empty (update)
3. The column name when building the table Data type (length) followed by not nul;

only:

The value cannot be repeated, but it can be empty; a unique index with the same name will be automatically built
1. alter table table name add constraint constraint name unique (column name);
2. At the end of the table creation , a single line of constraint constraint name unique (column name);
3 When building the table, add the constraint name unique after the column name data type (length);

an examination:

1. alter table table name add constraint constraint name check (check content);
2. At the end of the table creation , a single line of constraint constraint name check (check content);
3. when building the table, add constraint after the column name data type (length) Constraint name check (check content); **

Primary key:

Different from the unique constraint, the index column cannot be empty, and the cut value cannot be repeated;
syntax:

1. alter table 表名 add constraint 约束名 primary key(列名);--索引列不会用来查询
2. create unique index 索引名 on 表名(列名);--索引列是有业务含义的,会经常拿来查询
	 alter table 表名 add constraint 索引名 primary key(列名) using index 索引名;

Note:
1. Each table can only have one primary key;
2. The primary key starts with PK;
3. The on in merge should be the primary key;

Foreign key

When inserting data, insert the main table first, then insert the child table;
when deleting data, delete the child table first, and then delete the parent table; **

Basic syntax:

1.alter table 表名 add constraint 约束名 foreign key(列名) references 主表(主表列);
2.alter table 表名 add constraint 约束名 foreign key(列名) references 主表(主表列) on delete cascade;--级联删除,删除父表数据时,同时删除子表数据
3.alter table 表名 add constraint 约束名 foreign key(列名) references 主表(主表列) on delete set null;--级联置空, 删除父表数据时,子表外键列对应的行数据置空

Note:
1. The foreign key starts with FK;
2. The main table column of the dependent main table must be the primary key or have a unique constraint;
3. Because the use of foreign keys is complicated, it is less used now;

Guess you like

Origin blog.csdn.net/yang_z_1/article/details/111878713