Database-1: Introduction to SQL statements and DDL

One, the concept of SQL

SQL: is the abbreviation of Structured Query Language . It is an operating language for relational databases , which defines some unified specifications for operating relational databases. And there are some differences between each database management system, these differences are called "dialects".

Second, the classification of SQL

SQL statements are divided into the following four categories:

  • DCL (Data Control Language): Data control language, used to operate users and complete related control of user permissions (hardly needed for development);
  • DDL (Data Definition Language): Data Definition Language, used to manipulate libraries and tables;
  • DML (Data Manipulation Language): Data manipulation language, used to manipulate the data stored in the table, but only add, delete, and modify;
  • DQL (Data Query Language): Data query language, used to manipulate the data stored in the table, but can only complete the query.

Three, matters needing attention

  1. The keywords of SQL statements are not case-sensitive (the values ​​in the database are not case-sensitive when querying in MySQL, and the values ​​in the database when querying in Oracle are strictly case-sensitive);
  2. SQL statements all end with';';
  3. Single quotes and double quotes in SQL represent strings the same;
  4. The keyword (keyword) is a reserved word as part of SQL, and the keyword cannot be used as the name of a table or column;
  5. Comments are divided into single-line comments (- or #) and multi-line comments (/* */ ).

Four, SQL DDL (operation library)

1. Add: CREATE DATABASE [IF NOT EXISTS] [database name];

          如: create database mydb ;     DDL increase

2. Delete: DROP DATABASE [IF EXISTS] [database name];

          如: drop database mydb ;     

3. Change: ALTER DATABASE [database name] CHARACTER SET utf8; (change the character set of the database)

          Such as: alter database mydb character set utf8; (The character set of the modified data is UTF-8)    

4. Check: SHOW DATABASES; (note the S at the end, and all SQL statements end with';')

          如:show databases ;     

5. View the definition information of the created database: SHOW CREATE DATABASE [database name];

          Such as: show create database mydb; (View the definition information of the database created earlier)    

6. Use the database: USE [database name];

          如: use mydb ;    

7. Query the database being used: SELECT DATABASE();

          Such as:  select database(); (null means that the database has not been selected)   

Five, SQL DDL (operation table)

1. Create a table: (usually design the table in the DBMS, and then automatically generate the table-building statement)

        create table table name ( 
            field name 1 data type,
            field name 2 data type,
            ...
            field name n data type 
        ); (note that there is no',' after the end of the last field)

    The data type in MySql database is
            int:              integer;
            double:       floating-point type, for example, double(5,2) means up to 5 digits, of which there must be 2 decimal places, that is, the maximum value is 999.99;
            char:           fixed-length string type;        
            varchar:      variable-length string type (most used);
            text:            string type;
            blob:           byte type;
            date:           date type, format: yyyy-MM-dd;
            time:           time type, format: hh: mm:ss
            timestamp: The format of the timestamp type is: yyyy-MM-dd hh:mm:ss (feature: if no value is assigned to this field, then the current time is stored)

          如:create table student (
                      sid int(11) ,
                      name varchar(20) ,
                      sal double(5,2)
                  );                                           

          

2. Delete table: drop table table name;

          如:drop table student ;    

3. Modify the table: (usually use graphical interface tools to operate in the DBMS, not SQL statements)

      3.1. Add column: ALTER TABLE [table name] ADD (field name data type);

            Such as: alter table stu add (classname varchar(100)); (add the classname column to the stu table)

      3.2. Modify column type: ALTER TABLE [table name] MODIFY field name data type;

            Such as: alter table stu modify name char(10); (modify the name column type of the stu table to char(10))

      3.3. Modify the column name: ALTER TABLE [table name] CHANGE [original column name] [new column name] data type; (generally do not do this, but add a new column, the original column is retained, and then the new column can be used) (Note: You can also modify the data type at the same time when you modify the column name)

            Such as: alter table stu change classname class varchar(100); (modify the classname column name of the stu table as class)

      3.4. Delete columns: ALTER TABLE [table name] DROP [column name]; (same as above, generally do not delete operations)

            Such as: alter table stu drop class; (delete the class column of the stu table)

      3.5. Modify the table name: ALTER TABLE [original table name] RENAME TO [new table name];

            Such as: alter table student rename to stu; (modify the student table name to stu)

4. Query table: show tables; (display all tables under the current library)

          Such as: show tables;     

5. Query table: desc table name; (show the structure of the table)

          Such as: desc student;     

 

 

Guess you like

Origin blog.csdn.net/beita08/article/details/112121867