Principles of Database Systems (Chapter IV: SQL and relational database basic operation)

A, SQL Overview

sql is a structured query language (Structured Query Language, SQL) database is designed to communicate with the language, it can help users operate a relational database.

SQL features:

SQL is not a particular database vendor's proprietary language; SQL is easy to learn; SQL powerful, flexible, can be very complex and advanced database operations

SQL consists of:

  • data query
  • Data definition language (Data Definition Language, DDL)
  • Data manipulation (DML)
  • Control data (DCL)

******************* data definition language **********************

CREATE Create a database or database objects

ALTER database or database objects to be modified

DROP delete a database or database objects

************ data manipulation language (Data Manipulation Language, DML) ***********

SELECT retrieve data from a table or view

INSERT the data into a table or view

UPDATE modify data in a table or view

DELETE delete data from a table or view

*************** Data Control Language (Data Control Language, DCL) ****************

GRANT used to grant permissions

For permission to recover REVOKE

 

Two, MySQL prior knowledge

Embedded and dynamic SQL rules: the provisions of the specification method SQL statements used in high-level language programming, in order to accommodate more complex application

SQL call (in order to increase the flexibility and effectiveness, as well as sharing the SQL language has more advanced features of SQL): SQL routine call rules  

Use MySQL base : LAMP mode, WAMP mode L (Linux) A (Apache) M (MySQL) P (PHP, Perl, Python)

 

 

Relational database management system (the RDBMS) : Advantages: small size, high speed, open-source, the GPL

MySQL extension language elements

Constant: Also known as a literal or a scalar value 

  • String constants: a sequence of characters with a single or double quotation marks, the string constants into ASCII and Unicode string constants
  • Numerical constants: integer constant, floating-point constants
  • Hexadecimal constant: each pair of hex digits is converted to a character, its front there is a letter "X" (or "x")
  • The date and time constants: a single quote character string representing the date and time, for example, constituted enclosed: '2018-06-05'
  • Bit Field Value
  • Boolean: TRUE: 1; FALSE: 0
  • NULL value

variable:

  • User variables: variables often add a user before the @ symbol, which is used to distinguish the column name region
  • System variables: Most system variables used in other SQL statement, you must add two before the system variable @

 

 

 Expressions An expression is a constant, a variable, a combination of column names, complex arithmetic, operators and functions.

  • Character expressions
  • Numeric expression
  • Date type expressions

 

 

 Third, the data definition (DDL)

Create a database (CREATE): using the CREATE DATABASE or CREATE SCHEMA statement

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
 [DEFAULT]CHARACTER SET[=]charset_name
 |[DEFAULT]COLLATE[=]collation_name

例句:create DATABASE test_sy CHARACTER SET ="UTF8" COLLATION ="utf8_general_ci";     CREATE DATABASE mysql_test;

  1. Character Set (character set): defines the character encoding and character.
  2. Character sequence (collation): collation specified character set.

Check database (SHOW): see all tables in the database

SHOW {DATABASES | SCHEMAS}
 [LIKE’pattern’ | WHERE expr]

Conditions Where clause is used to specify the name of the database query range; Like keyword is used to match the specified database name

例句:SHOW DATABASES;  SHOW DATABASES LIKE "%test%";

SHOW TABLES: Table View non-system database

 

Select the database (the USE): the USE db_name; from a database "jump" to another database.

Modify the database: alert

mysql>ALTER DATABASE mysql_test
 -> DEFAULT CHARACTER SET gb2312
 -> DEFAULT COLLATE gb2312_chinese_ci;

Delete the database:

DROP{DATABASE|SCHEMA}[IF EXISTS]db_name

例句:DROP DATABASE hahaha;   DROP DATABASE IF EXISTS hahaha;

***************************** table definition ******************* ***************

Create a table : data tables in a relational database is the most important and most basic fundamental unit of data objects, data is stored.

Data table is defined as a field set by the (row) and format (columns) to store, for each (row) represents a record, each of the (column) represents the value of a field in the record.

The CREATE [ TEMPORARY ] TABLE tbl_name 
 ( 
 Field Name Data Type 1 [ column-level integrity constraints ] [ Default ] 
 [ , 2 Field Name Data Type [column-level integrity constraints ] [ Default ] ]
  [ , ...... ] 
 [ , table-level integrity constraints ] 
 ) [ ENGINT engine type = ] ;

Addition of "TEMPORARY", was a temporary table

Create a table containing customer basic information content of the customer's name, gender, address and contact information in an existing database mysql_test, the requirements of the customer id number primary key for the specified table.

 

 

PRIMARY KEY (cust_id): specify the primary key

type of data:

  • Int int
  • Float double
  • Boolean bool
  • Date type date
  • Timestamp timestamp
  • Time-of-time fixed-length
  • Character type char
  • Variable-length character varchar

Update the table (ALTER): ALTER TABLE statement, add or delete columns, create or destroy indexes, change the data type of the original columns, rename columns or tables, change table commentary and engine type of table, recreate the table triggers, stored procedures, indexes and foreign keys.

1、ADD COLUMN 

例如:向数据库mysql_test的表customers中添加一列,并命名为 cust_city,要求其不能为NULL,默认值为字符串“Wuhan”,且该列位 于原表cust_sex列之后。

ALTER TABLE mysql_test.customers ->ADD COLUMN cust_city char(10)NOT NULL DEFAULT ‘Wuhan’ AFTER cust_sex;

2、CHANGE[COLUMN]子句 修改表中列的名称或数据类型

 

 3、ALTER[COLUMN]子句 修改或删除表中指定列的默认值

  ALTER TABLE mysql_test.customers ->ALTER COLUMN cust_city SET DEFAULT ‘Beijing’;

4、MODIFY[COLUMN]子句 只修改指定列的数据类型,不会干涉它的列名

  ALTER TABLE mysql_test.customers ->MODIFY COLUMN cust_name char(20) FIRST;

5、DROP[COLUMN]子句 删除表中多余的列

ALTER TABLE mysql_test.customers ->DROP COLUMN cust_contact;

6、RENAME[TO]子句 为表重新赋予一个表名

ALTER TABLE mysql_test.customers ->RENAME TO mysql_test.backup_customers;

给表重命名表的第二种写法:RENAME TABLE db_a.old_table TO db_b.new_table;

7、DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [ ,tbl_name ] … [RESTRICT | CASCADE]

 

查看表结构:

SHOW [FULL] COLUMNS {FROM | IN} tbl_name[{FROM | IN} db_name] [LIKE’pattern’ | WHERE expr]

例如:SHOW COLUMNS FROM t_role;

{DESCRIBE | DESC} tbl_name [col_name | wild]

例如:DESC t_role;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

数据更新

 

数据查询

 

 

视图

Guess you like

Origin www.cnblogs.com/jalja/p/11605648.html