当前位置:网站首页>SQL related knowledge - DDL

SQL related knowledge - DDL

2022-06-26 04:05:00 Code Xiaoyou

SQL:Structured Query Language: Structured query language

SQL General grammar :

1.SQL Grammar can be written in one or more lines , It ends with a semicolon

2. You can use spaces and indents to enhance the readability of statements

3.MySQL Database sql Statement is case insensitive , It is recommended to use uppercase

4.3 Species notes

       * Single-line comments :-- The comment or # The comment (-- Put a space after it ,# No requirement )

       * Multiline comment :/* notes */

SQL classification :

1)DDL(Data Definition Language) Data definition language    Used to define database objects : database , surface , Column, etc. . keyword :create,drop,alter etc.

2)DML(Data Manipulation Language) Data operation language    It is used to add, delete and modify the data in the database . keyword :insert,delete,update etc.

3)DQL(Data Query Language) Data query language   Used to query the records of tables in the database ( data ). keyword :select,where etc.

4)DCL(Data Control Language) Data control language    It is used to define the access rights and security level of the database , And Create user . keyword :GRANT,REVOKE etc.

DDL: Operating the database , surface

 1. Operating the database :CRUD

       1.1 C(Create): establish

                * Create database :

                        create database Database name ;

                * Create database , Judgment does not exist , To create a

                        create database if not exists Database name ;

                * Create database , And specify the character set

                        create database Database name character set Character set name ;

       1.2 R(Retrieve): Inquire about

                * Query all database names ;

                        show databases;

                * Query the character set of a database : Query the creation statement of a database ;

                        show database Database name ;

       1.3 U(Updata): modify

                * Modify the character set of the database

                        alter database Database name character set Character set ;

       1.4 D(Delect): Delete

                * Delete database

                        drop database Database name ;

                * Determine that the database exists , Exist and delete

                        drop database if exists Database name ;

       1.5 Using a database

                * Query the currently used database name

                        select database();

                * Using a database

                        use Database name ;

2. Operation table

        1.1 C(Create): establish

                * Create table grammar

                        create table Table name (

                                Name 1 data type 1,

                                Name 2 data type 2,

                                ....

                                Name n data type n

                        );

                * Copy table

                        create table Table name like Assigned table name ;

practice : Create a student table

create table Student(
    num int,
    name varchar(20),
    age int,
    score double(5,1), # Retain 5 Significant digits , Decimal places reserved 1 position 
    birthday date,
    insert_time timestamp
);

 

(timestamp: This is a data type , If you don't assign a value to this field , Or assign the value to NULL, The current system time is used by default , To automatically assign )

        1.2 R(Retrieve): Inquire about

                * Query all table names in a database

                        show tables;

                * Query table structure

                        desc Table name ;

        1.3 U(Updata): modify        
                * Modify the name of the table

                        alter table Table name rename to New table name ;

                * Modify the character set of the table

                        alter table Table name character set Character set ;

                * Add a column

                        alter table Table name add Name data type ;

                * Change column name type

                        alter table Table name change Name New column names New data types ;

                        alter table Table name modify Name New data types ;

                * Delete column

                        alter table Table name drop Name ;

        1.4 D(Delect): Delete

                * Delete table

                        drop table The name of the table ;

                * The judgment table exists , Exist and delete

                        drop table if exists The name of the table ;

原网站

版权声明
本文为[Code Xiaoyou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180540095088.html