"SQL Must Know and Know" Appendix


在数据库中,除了表中的数据,其他都不区分大小写。

"SQL Must Know and Know (Fourth Edition)" eBook

https://pan.baidu.com/s/1sUzdNZSChK-zZz4YfZfLQQ
extraction code: 4oyt

Appendix A Sample table script

A.1 Sample table

The tables used in this book are part of an order entry system used by a hypothetical toy dealer. These tables are used to accomplish the following tasks:

  • Manage suppliers;
  • Manage product catalogs;
  • Manage customer lists;
  • Enter customer orders.

Five tables are needed to complete them (they are closely related as part of a relational database design). The following sections give a description of each table.

Table description

Here are five tables and the column names in each table.
1.
Vendors table The Vendors table stores vendors who sell products. Each supplier has a record in this table, and the supplier ID column (vend_id) is used to match products and suppliers.
Insert picture description here
All tables should have primary keys. This table should use vend_id as its primary key.

2. Products table The
Products table contains a product catalog, one product per line. Each product has a unique ID
(prod_id column), and is associated with the supplier with the help of vend_id (the unique ID of the supplier).
Insert picture description here
All tables should have primary keys. This table should use prod_id as its primary key.
To implement referential integrity, a foreign key should be defined on the vendor_id, which is associated with the vendor_id column of vendors.

3. Customers table The
customers table stores all customer information. Each customer has a unique ID (cust_id column).
Insert picture description here
All tables should have a primary key. This table should use cust_id as its primary key.

4. Orders table The
orders table stores customer orders (not order details). Each order has a unique number (order_num column). The orders table is associated with the corresponding customer by the cust_id column (the unique customer ID associated with the Customers table).
Insert picture description here

  • All tables should have primary keys. This table should use order_num as its primary key.
  • To implement referential integrity, a foreign key should be defined on cust_id, which is associated with the cust_id column of Customers.

5. OrderItems table The
orderItems table stores the actual items in each order, and one row for each item in each order. For each row of the orders table, there are one or more rows in the OrderItems table. Each order item is uniquely identified by the order number plus the order item ((first item, second item, etc.). The order item is associated with its corresponding order using the order_num column (the unique ID associated with the order in the orders table). In addition, , Each order item contains the product ID of the item (associate the item to the Products table).
Insert picture description here

  • All tables should have primary keys. This table should use order_num and order_item as its primary keys.
  • To implement referential integrity, foreign keys should be defined on order_num and prod_id, and order_num should be associated with the order_num column of orders, and prod_id should be associated with the prod_id column of Products.

Database administrators usually use diagrams to illustrate how database tables are related. Remember, as mentioned in the table description above, foreign keys define these relationships. Figure A-1 is the relationship diagram of the five tables described in this appendix.
Insert picture description here

A.2 Get the sample table

To study each example, you need a set of tables filled with data. Everything you need to get and run can be found on the book page http://www.forta.com/books/0672336073/ .
MySQL sample table and data sql: https://pan.baidu.com/s/1024yj3uonDz1bFsbJ0iHlg
extraction code: eqz8

A.2.1 Download available data files

A file in the following format filled with data can be downloaded from the above URL:

  • Apache Open Office Base
  • Microsoft Access (2000 sum 2007)
  • SQLite

If you use these files, you do not need to execute any SQL creation and filling scripts.

A.2.2 Download DBMS SQL script

Most DBMSs store data in a format that does not complete file distribution by themselves (such as Access, Openoffice Base, and sQLite). For these DBMSs, SQL scripts can be downloaded from the above URL. For each DBMS, there are two files:

  • create.text contains sQL statements to create 5 database tables (including defining all primary keys and foreign key constraints).
  • populate.txt contains the SQL INSERT statements used to populate these tables.

The SQL statements in these files depend on the specific DBMS, so you should execute the one that suits your DBMS. These scripts are provided for the convenience of readers, and the author does not assume any responsibility for problems caused by executing them.

  • IBM DB2;
  • Microsoft SQL Server (Comprehensive Microsoft sQL Server Express);
  • MariaDB
  • MySQL;
  • Oracle(包括Oracle Express );
  • PostgreSQL。

There are so many available, it is recommended to use MySQL, the most used in enterprises.

Appendix B Popular applications

B.0 Use MySQL

There are two ways to use MySQL. The DBMS comes with a command line utility named mysq1. This is a plain text tool that is usually installed as part of the MySQL installation program to execute any SQL statement. In addition, the creator of MySQL released an interactive tool called MySQLWorkbench, which usually needs to be downloaded and installed separately, so it will not appear in other installation programs. When learning MySQL, it is highly recommended.
Using mysql from the command line, proceed as follows.

(1)输人mysq1运行实用程序。根据如何定义安全性,可能需要使用-u和-p参数指定登录信息。
(2)在mysq1>提示下输人USE database打开数据库,例如USE tysq7就是打开tysq1数据库。
(3)在mysql>提示下输入SQL语句,每条语句必须以分号(;)结束。结果将显示在屏幕上。
(4)为可能使用的命令列表输人\h,为状态信息输人\s(包括MySQL版本信息)。
(5)输人\q退出mysq1实用程序。

Use MySQL Workbench to perform the following operations.

(1)运行MySQL Workbench。
(2) MySQL Workbench的最左侧列出了可用的 MySQL数据库连接,允许你访问它们。点击任何连接就可以打开这个数据库;如果数据库没有在此列出,请选择New Connection。
(3)一旦连接,就会显示多个窗口。左侧的Object Browser列出了可用的数据库,中间是用于输人SQL语句的大的文本编辑器,结果或信息显示在下方。		
(4)点击+SQL按钮,打开新的SQL窗口。
(5)输人.SQL语句后,点击Execute(有闪电图片的那个)运行SQL。结果显示在下面。

B.1 Use Oracle

Oracle has a large set of tools and clients. Oracle sQL Developer is the first choice when learning SQL. It can be installed together with MySQL DBMS, or it can be downloaded and installed independently. Here is how to use this tool.

(1)运行Oracle sQL Developer ( Windows用户需要使用提供的批处理文件运行,不能通过应用本身运行)。
(2)在使用数据库前,你需要定义一个连接。这可以使用左侧Connections面板中的选项完成。
(3)连接完成后,使用SQL Worksheet标签页,在Query Builder 屏幕中输入sQL语句。
(4)执行sQL语句,单击Execute按钮(带闪电图形)。结果将显示在下面的面板中。

It is recommended that after installing the DBMS (that is, MySQL or Oracle as we commonly call it), use visual tools to operate the database. It is recommended Navicat Premiumthat most DBMSs are easy to use and explode.

Appendix C Syntax of SQL Statement

To help readers find the syntax of the corresponding sentence when needed, this appendix lists the syntax of the most commonly used sQL sentence. Each sentence starts with a brief description, and then its grammar is given. To make the query easier, the class where the corresponding sentence is located is also marked.

When reading the sentence grammar, you should remember the following conventions.

  • The | symbol is used to indicate one of several choices, therefore, NULL / NOT NULL means either NULL or NOT NULL is given.
  • Keywords or clauses contained in square brackets (such as [like this]) are optional.
  • The syntax listed below is valid for almost all DBMSs. Regarding the details of possible changes in specific syntax, readers are advised to refer to their own DBMS documentation.

C.0 ALTER TABLE

ALTER TABLE is used to update the structure of an existing table. In order to create a new table, CREATE TABLE should be used. For details, see Lesson 17.

ALTER TABLE tablename
(
ADD|DROP column datatype [NULL|NOT NULL] [CONSTRAINTS],
ADD|DROP column datatype [NULL|NOT NULL] [CONSTRAINTS],
...
);

C.1 COMMIT

COMMIT is used to write transactions to the database. Please refer to Lesson 20 for details.

COMMIT [TRANSACTION];

C.2 CREATE INDEX

CREATE INDEX is used to create an index on one or more columns. Please refer to Lesson 22 for details.

create index indexname on tablename (column....);

C.3 CREATE PROCEDURE

CREATE PROCEDURE is used to create stored procedures. Please refer to Lesson 19 for details. As mentioned, the syntax used by Oracle is slightly different.

create procedure procedurename [parameters] [options]
as
sql statement;

C.4 CREATE TABLE

create table is used to create a new database table. To update the existing table structure, use alter table. See lesson 17.

create table tablename
(
  column datatype [null|not null] [constraints],
  ...
);

C.5 CREATE VIEW

CREATE VIEW is used to create views. See lesson 18.

create view viewname as
select columns,....
from tables,...
[where ...]
[group by ...]
[having ...];

C.6 DELETE

DELETE deletes one or more rows of data in the table. See lesson 16.

delete from tablename
[where ...];

C.7 DROP

DROP permanently deletes database objects (tables, views, indexes, etc.), see Lesson 17.

drop index|procedure|table|view
indexname|procedurename|tablename|viewname;

C.8 INSERT

Add a row to the table. See lesson 15.

insert into tablename[(columns,...)]
values (values,...);

C.9 INSERT SELECT

Insert the result of select into a table. See lesson 15.

insert into tablename[(columns,...)]
select columns,... from tablename,...
[where ...] ;

C.10 ROLLBACK

Undo a transaction block. See lesson 20.

rollback [to savepointname];rollback transaction;

C.11 SELECT *

The most commonly used one, the most important.
Retrieve data, find data. Refer to lessons 2, 3, and 4.

select columnname,...
from tablename
[where ...]
[union ...]
[group by ...]
[having ...]
[order by ...];

C.12 UPDATE

Update one or more rows. See lesson 16.

update tablename
set columnname = value,...
[where ...];

Appendix D SQL data types

The data types are used for the following purposes.

  • The data type allows to limit the data that can be stored in the column. For example, a numeric data type column can only accept numeric values.
  • Data types allow more efficient storage of data internally. You can store numeric and datetime values ​​in a more concise format than text strings.
  • The data type allows changing the sort order. If all data is treated as a string, then 1 is before 10, and 10 is before 2 (strings are sorted lexicographically, and compared from the left, one character at a time). As a numeric data type, numeric values ​​can be sorted correctly.

When designing the table, special attention should be paid to the type of data used. Using the wrong data type may severely affect the functionality and performance of the application. Changing the column that contains the data is not a trivial matter (and doing so may result in data loss).

D.0 String data type

Insert picture description here
不管使用何种形式的字符串数据类型,字符串值都必须括在单引号内。

D.1 Numerical data type

Insert picture description here
与字符串不一样,数值不应该括在引号内。

D.2 Date and time data types

Insert picture description here
The most commonly used are these three types, and there are other binary data types, etc., which are rarely used for beginners and do not need to pay attention.

Appendix E SQL reserved words

SQL is a language composed of keywords, which are special words used to perform sQL operations. When naming databases, tables, columns, and other database objects, you must not use these keywords. Therefore, these keywords must be reserved.
This appendix lists the most commonly used reserved words in major DBMSs. Please note the following points.

  • The keywords vary with different DBMSs, and not all keywords below are adopted by all DBMSs.
  • Many DBMSs have extended SQL reserved words to include implementation-specific terms. Most DBMS-specific keywords are not listed below.
  • To ensure future compatibility and portability, you should avoid using these reserved words, even if they are not reserved words of the DBMS you use.

Those who are interested can take a look. Just forget it if you are not interested. . .
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

Quick check of commonly used SQL statements

ALTER TABLE
ALTER TABLE is used to update the schema of an existing table. You can use CREATE TABLE to create a new table. See lesson 17 for details.

COMMIT
COMMIT is used to write transactions to the database. See lesson 20 for details.

CREATE INDEX
CREATE INDEX is used to create an index for one or more columns. See lesson 22 for details.

CREATE TABLE
CREATE TABLE is used to create a new database table. You can use ALTER TABLE to update the schema of an existing table. See lesson 17 for details.

CREATE VIEW
CREATE VIEw is used to create a view of one or more tables. See lesson 18 for details.

DELETE
DELETE is used to delete one or more rows from the table. See lesson 16 for details.

DROP
DROP is used to permanently delete database objects (tables, views, indexes, etc.). Please refer to
Lesson 17 and Lesson 18 for details .

INSERT
INSERT is used to add a new row to the table. See lesson 15 for details.

INSERT SELECT
INSERT SELECT is used to insert the result of SELECT into the table. See lesson 15 for details.

ROLLBACK
ROLLBACK is used to undo transaction blocks. See lesson 20 for details.

SELECT
SELECT is used to retrieve data from one or more tables (or views). For details, see Lesson 2, Lesson 3 and Lesson 4 (Lesson 2 to Lesson 14 involve SELECT in different ways).

UPDATE
UPDATE is used to update one or more rows in the table. See lesson 16 for details.

Guess you like

Origin blog.csdn.net/qq_43600467/article/details/112917369