SQL overview and data definition

1. Overview of SQL

1 Introduction

SQL (Structured Query Language) is a database language with multiple functions such as data manipulation and data definition . This language is interactive and can provide users with great convenience. The database management system should make full use of SQL language to improve computer application systems. work quality and efficiency. SQL language can not only be independently applied to terminals, but also can be used as a sub-language to provide effective assistance for other program designs. In this program application, SQL can optimize program functions together with other program languages, thereby providing users with more and more comprehensive information.

2. Features

  1. Comprehensive and unified
  2. highly non-procedural
  3. Set-Oriented Operations
  4. Provides two usage methods with the same syntax structure
  5. Simple language, easy to learn and use

3. Composition

  1. A SQL database is a collection of tables (Table), which is defined by one or more SQL schemas .
  2. A SQL table is composed of a row set , a row is a sequence (set) of columns, and each column and row corresponds to a data item.
  3. A table is either a base table or a view . A basic table is a table that is actually stored in the database , and a view is a definition of a table composed of several basic tables or other views .
  4. A basic table can span one or more storage files, and a storage file can also store one or more basic tables . Each storage file corresponds to a physical file on the external storage.
  5. Users can use SQL statements to query views and basic tables . From the user's point of view, the view and the basic table are the same, there is no difference, they are all relationships (tables).
  6. SQL users can be applications or end users . SQL statements can be embedded in programs of host languages, such as FORTRAN, COBOL, PASCAL, PL/I, C, and Ada. The SQL user can also serve as a stand-alone user interface for end-users in an interactive environment.

4. SQL classification

  1. DDL : Data Definition Language. Includes CREATE / ALTER / DROP / RENAME / TRUNCATE
  2. DML : Data Manipulation Language. including INSERT/DELETE/UPDATE/ SELECT
  3. DCL : Data Control Language. Include COMMIT / ROLLBACK / SAVEPOINT / GRANT / REVOKE

5. Writing specifications

  • The database name, table name, table alias, field name, field alias, etc. are all lowercase
  • SQL keywords, function names, bind variables, etc. are capitalized

Learning skills : think big, start small!


2. Data definition

Functions include schema definitions, table definitions, views, and index definitions.

1. Definition and deletion of patterns

①Definition mode

Schema definition statement :

CREATE SCHEMA <模式名> AUTHORIZATION <用户名> [<表定义子句> | <视图定义子句> | <授权定义子句>]

Note :

  1. If <schemaname> is not specified, it is implied as <username>
  2. To create a schema, the user calling the command must have DBA authority
  3. Defining a schema actually defines a namespace in which the database objects contained in the schema can be defined, such as basic tables, views, indexes, etc.
  4. CREATE TABLE, CREATE VIEW and GRANT clauses are acceptable in CREATE SCHEMA.

②Delete mode

Schema delete statement :

DROP SCHEMA <模式名> <CASCADE | RESTRICT>

Note :

  1. One of CASCADE and RESTRICT must be selected
  2. The former means cascading , which means that all database objects in the schema are deleted at the same time as the schema is deleted
  3. The latter represents a restriction , which means that if the subordinate database objects (such as tables, views, etc.) have been defined in the schema, the execution of the delete statement will be refused.

2. Definition, deletion and modification of basic tables

① Define the basic table

Definition format :

CREATE TABLE <表名>(<列名> <数据类型> [列级完整性约束条件]
			[,<列名> <数据类型> [列级完整性约束条件]][,<表级完整性约束条件>]);

Explanation of terms :

  • <table name>: the name of the basic table to be defined
  • <column name>: each attribute (column) that makes up the table
  • <column-level integrity constraints>: Integrity constraints involving the corresponding attribute column
  • <table-level integrity constraints>: Integrity constraints involving one or more attribute columns

②Data type

insert image description here

③ Mode and table

Each table belongs to a schema, and a schema contains multiple basic tables.

A way to define the schema to which a base table belongs :

  1. Give the schema name explicitly in the table name
  2. Create the table concurrently in the create schema statement
  3. Set the schema to which it belongs, so that you don't have to give the schema name when creating the table name

When a user creates a basic table without specifying a schema, the system determines the schema to which the object belongs based on the search path.
The search path contains a list of schemas, and the RDBMS uses the first existing schema in the schema list as the schema name of the database object. If none of the schema names in the search path exist, an error will be given.

Display the current search paths :

SHOW search_path;

Defaults:$user, PUBLIC

Set search paths (DBA users) :

SET search_path TO "S-T",PUBLIC;

Define the base table :

CREATE TABLE Student();

Create S-T.Studentbasic tables. Because the RDBMS finds that the first schema name ST exists in the search path, it takes this schema as the schema to which the basic table Student belongs

④ modify the basic table

Statement format :

ALTER TABLE <表名>
[ADD <新列名> <数据类型> [完整性约束]]
[DROP <完整性约束名>]
[ALTER COLUMN <列名> <数据类型>];
  • <table name>: the basic table to be modified
  • ADD clause: add new columns and new integrity constraints;
  • DROP clause: delete the specified integrity constraints;
  • MODIFY clause: used to modify column names and data types;

⑤Delete the basic table

Statement format :

DROP TABLE <表名> [RESTRICT|CASCADE]
  1. RESTRICT: Drop tables are restricted. The table to be dropped cannot be referenced by constraints on other tables. The table cannot be dropped if there are objects that depend on it.
  2. CASCADE: There is no restriction on dropping the table. When deleting the basic table, related dependent objects are deleted together.

3. Index creation and deletion

Indexing is an effective means of speeding up queries. Indexing is established by the DBA or the owner of the table (that is, the person who created the table) as needed.

①Create an index

Statement format :

CREATE [UNIQUE] [CLUSTER] INDEX <索引名> 
ON <表名>(<列名>[<次序>][,<列名>[<次序>] ])

Explanation of terms :

  • <table name>: the name of the basic table to be indexed;
  • Index: It can be built on one or more columns of the table, and the column names are separated by commas;
  • <Order>: Specify the sorting order of the index value, ascending order: ASC, descending order: DESC, default ascending order;
  • UNIQUE indicates that each index value of this index only corresponds to a unique data record
  • CLUSTER indicates that the index to be built is a clustered index . Refers to the index organization in which the order of index entries is consistent with the physical order of records in the table

Clustered index :

  1. Building clustered indexes on frequently queried columns can improve query efficiency.
  2. At most one clustered index can be created

②Delete index

When you delete an index, the description for that index is removed from the data dictionary. No user intervention required

DROP INDEX Stusname;

Guess you like

Origin blog.csdn.net/z135733/article/details/128889483