(202307) wonderful-sql: Getting to know the database first (task1)

Learn

Getting to know the database

Introduction to Relational Databases

It is characterized by two-dimensional tables composed of rows and columns to manage data. This type of DBMS is called a relational database management system (Relational Database Management System, RDBMS). This course will introduce you to a database management system using the SQL language, that is, a relational database management system (RDBMS).

In relational databases, it is common toClient/server type (C/S type). The client issues the sql language to request data from the server, and the server reads the data from the database and returns it to the client.

The table structure stored in the database is similar to the rows and columns in excel. In the database, the row is calledRecord, which corresponds to a record with columns calledfield, which represents the data items stored in the table.

SQL is a language developed for manipulating databases. The International Organization for Standardization (ISO) has formulated corresponding standards for SQL, and SQL based on this is called standard SQL. But in fact there are very few RDBMSs that are completely based on standards, but the standard SQL language is used in the datawhale tutorial.

According to different instructions, SQL statements are divided into three categories,Data Definition Language DDL, Data Manipulation Language DML, Data Control Language DCL, the tutorial mainly focuses on DML, because it is used the most, and 90% of the SQL statements actually used belong to DML.

grammar specification

In the warehouse of the project of datawhale, there is a complete sql syntax specification

Naming rules

Only half-width English letters, numbers, and underscores (_) can be used as the names of databases, tables and columns
Names must start with half-width English letters

Specification of data type

Among data types, the ones that are rarely seen in other languages ​​​​areVARCHARType, as its name "VAR CHAR" variable-length string type.

Constraint settings

Constraints are functions that restrict or add conditions to the data stored in a column in addition to the data type.
NOT NULL is a non-empty constraint, that is, the column must enter data.
PRIMARY KEY is a primary key constraint, which means that this column is a unique value, and the data of a specific row can be retrieved through this column.

Moreover, in the example of creating a table given in the tutorial, the primary key constraint can also be added to the row for constraint, namely:

CREATE TABLE product
(
  product_id CHAR(4) NOT NULL PRIMARY KEY,
  product_name VARCHAR(100) NOT NULL,
  product_type VARCHAR(32) NOT NULL,
  sale_price INTEGER,
  purchase_price INTEGER,
  regist_date DATE
);

Or we can also name the constraints, writing the constraints as:

CONSTRAINT pk_product PRIMARY KEY (product_id)

That is, name the constraint as pk_product.
You can also DEFAULTset default values ​​when creating a table.

table deletion and update

Table deletion and update mainly include commands such as DROP, DELETE, ALTER, and TRUNCATE. It should be noted that the deletion cannot be restored and where must be specified.
If you do not specify where, then the modification (DELETE or UPDATE) will affect the entire table.
The SET statement of UPDATE also supports multiple columns as update objects at the same time.

-- 合并后的写法
UPDATE product
   SET sale_price = sale_price * 10,
       purchase_price = purchase_price / 2
 WHERE product_type = '厨房用具';  

insert data

The INSERT statement can be used to add data. You can add data without listing the column list, and if you do not list the column list, it will be added sequentially from left to right.

-- 包含列清单
INSERT INTO productins (product_id, product_name, product_type, sale_price, purchase_price, regist_date) VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');
-- 省略列清单
INSERT INTO productins VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');  

Also, the INSERT statement can insert multiple rows at a time.

INSERT INTO productins VALUES ('0002', '打孔器', '办公用品', 500, 320, '2009-09-11'),
                              ('0003', '运动T恤', '衣服', 4000, 2800, NULL),
                              ('0004', '菜刀', '厨房用具', 3000, 2800, '2009-09-20'); 

If you want to assign a value to be empty, you can write the value as NULL, but you can only assign a value to a column without a non-null constraint.

Can also be used INSERT INTO...SELECT...FROM...to copy data from other tables.

index

Indexes are divided into primary key index, unique index, ordinary index, prefix index, full-text index* (an index that uses "word segmentation technology" to search for keywords in long text), single-column index, joint index (composite index, multi-column index)* , etc.

practice questions

1

Write a CREATE TABLE statement that creates a table Addressbook (address book) that contains the items listed in Table 1-A and sets a primary key constraint on the regist_no (registration number) column.

CREATE TABLE Addressbook
(
	regist_no INTEGER NOT NULL,
	name VARCHAR(128) NOT NULL,
	address VARCHAR(256) NOT NULL,
	tel_no CHAR(10),
	mail_address CHAR(20),
	PRIMARY KEY (regist_no)
);

insert image description here

2

Suppose you forgot to add the following column postal_code (postal code) when creating the Addressbook table in Exercise 1.1, please write SQL to add this column to the Addressbook table.
Column name: postal_code
Data type: Fixed-length string type (length 8)
Constraint: Cannot be NULL

ALTER TABLE Addressbook ADD postal_code CHAR(8) NOT NULL;

insert image description here

3

Please add the following SQL statement to delete the Addressbook table.

DROP TABLE Addressbook;

4

This is unrecoverable, whether you use TRUNCATE TABLE or DELETE to clear data, use DROP to delete a table, or use ALTER to delete a column, it is unrecoverable, because the data is not stored anywhere after deletion, and of course it cannot be recovered.

Guess you like

Origin blog.csdn.net/raw_inputhello/article/details/131822045