SQL Basics (MySQL)

1. The relationship between DB/DBMS/SQL is: DBMS operates DB by executing SQL.

(1) Database: DataBase, referred to as DB. A combination of files that stores data in a certain format.

(2) Database management system: DataBaseManagement, referred to as DBMS. It is specially used to manage data in the database. The database management system can add, delete, modify and check the data in the database. Common database management systems: MySQL, Oracle, MS, SQL server, DB2, Sybase, etc.

(3) Structured query language: SQL for short.

2. The syntax for using commands to start or shut down the mysql service in Windows systems:

(1) Close the service:

net stop 服务名称;

(2) Start the service:

net start 服务名称;

3. The most basic unit in the database is the table. Databases represent data in the form of tables because tables are more intuitive.

(1) Row: called data/record.

(2) Column: called a field. Each field has a field name, data type (such as number, string, date, etc.), constraints (such as uniqueness constraints), etc.

4. SQL statement classification:

(1) DQL: Data query statement (anything with the select keyword is a query statement)

(2) DML: Data manipulation language (anything that processes the data in the table is DML)

    insert: 增
    delete: 删
    update: 改

(3) DDL: Data Definition Language (anything with create, drop, alter is DDL)

DDL mainly operates on the structure of the table, not the data in the table.

    create: 新建(等同于增)
    alter: 修改
    drop: 删除

(4) TCL: Transaction Control Language

    事务提交:commit
    事务回滚:rollback

(5) DCL: Data Control Language

For example: authorize grant, revoke permission revoke.....

5. MySQL common commands

Note: SQL statements are not case-sensitive and end with an English semicolon to indicate the end. If the semicolon is missing, it will not be executed.

Log in to MySQL in dos window:

mysql -uroot -p

(1) Check the version number of the MySQL database:

select version();

(2) Exit MySQL in the dos window:

exit;

(3) Check which databases are in MySQL:

show databases;

(4) Choose to use a database:

use 数据库名称;

(5) Create database:

create 数据库名称;

(6) Check what tables are in the database:

show tables;

(7) How to import data in .sql file in dos window:

source 绝对路径\xxx.sql;

(8) View the database currently in use:

select database();

(9) View the table currently in use:

select table();

(10) View all data in the table:

select * from 表名;

(11) View the structure of the table (without looking at the data):

desc 表名;

6. Simple query

(1) Query a certain field:

select 字段名 from 表名;

(2) Query multiple fields separated by commas:

select 字段名1,字段名2 from 表名;

(3) Query all fields:

select 字段名1,字段名2,...... from 表名;
select * from 表名; //(一般不建议使用该种方式)

(4) Use the as keyword to alias the columns displayed in the query, such as:

select 字段名(列) as 显示的别名 from 表名;

Note: There should be no spaces in the alias. If spaces are required, use single quotes ''.

(5) Fields can use mathematical expressions, such as:

select ename,sal*12 as '年薪' from emp;

7. Conditional query

 语法格式:select 字段1,字段2,字段3... from 表名 where 条件;

(1) = equal to

select empno,ename from emp where sal = 800;

(2) <> or != is not equal to

select empno,ename from emp where sal != 800;
select empno,ename from emp where sal <> 800;

(3)< less than

 
 

Guess you like

Origin blog.csdn.net/sinat_33101665/article/details/128936205