MySQL data model and SQL

1. Relational database

A relational database is a database composed of multiple two-dimensional tables that can be connected to each other

Table of contents

1. Relational database

Two, SQL language

 1. Introduction to SQL

 2. SQL general syntax

 3. SQL classification

 4. DDL (Data Definition Language)-------operation database 

 5. DML (Data Manipulation Language)------operation table

 6. Data types in MySQL


Two, SQL language

1. Introduction to SQL

  1. English: Structured Query Language, referred to as SQL
  2. Structured Query Language , a programming language for manipulating relational databases
  3. Define a unified standard for operating all relational databases
  4. For the same requirement, each database operation method may have some differences, which are jokingly called "dialects"

 2. SQL general syntax

  1. SQL statements can be written in a single line or in multiple lines, ending with a semicolon .
  2. The SQL statements of the MySQL database are not case-sensitive , and keywords are recommended to be capitalized.
  3. note

          Single-line comment: --comment   content or #comment content (MySQL specific)

          Multi-line comment: /* comment content  */

 3. SQL classification

DDL (Data Definition Language) : Database definition language, used to define database objects; databases, tables , lists, etc.

DML (Data Manipulation Language) : Database operation language, used to add, delete, and modify data in database tables .

DQL (Data Query Language) : Database query language, used to query the records (data) in the database.

DCL (Data Control Language) : Database control language, used to define the access rights and security levels of the database and create users.

4.DDL (Data Definition Language)-------operation database 

  1. Inquire
    show databases;
  2. create database
    create database 数据库名称;
  3. Create a database (judgment, create if it does not exist)
    create database if not exists 数据库名称:
  4.  delete database
    drop database 数据库名称;
  5.  Delete the database (judgment, delete if it exists)
    drop databases if exists 数据库名称:
  6. View the currently used database
    select database();
  7.  Use the database (enter the database)
    use 数据库名称;

5. DML (Data Manipulation Language) ------ operation table

  1. Query all table names under the current database
    show tables;
  2.  query table structure
    desc 表名称;
    
  3. create table
     create table 表名(
                字段名1 数据类型1, 
                字段名2 数据类型2,
                .....
                字段名n 数据类型n
    );/*注意:最后一行末尾,不能加逗号*/
  4. modify table name

    alter table 表名 rename to 新表名;
  5. Add a column in the table

    alter table 表名 add 列名 数据类型;
  6. modify data type
    alter table 表名 modify 列名 新数据类型;
  7. Modify column names and data types
    alter table 表名 change 列名 新列名 新数据类型;
    
  8. delete column
    alter table 表名 drop 列名;
  9. delete table

    drop table 表名;
  10. Determining whether a table exists when deleting a table

    drop table if exists 表名;
    

6. Data types in MySQL

MySQL supports a variety of data types, which can be divided into three categories:

  • value
  • date
  • string

create table student(
	
	id int,
	name varchar(10),
	gender char(1),
	birthday date,
	score double(5,2),
	email varchar(64),
	tel varchar(15),
	status tinyint

);

Guess you like

Origin blog.csdn.net/weixin_48373085/article/details/128512542