MySQL SQL介绍

Posted woaiyunwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL SQL介绍相关的知识,希望对你有一定的参考价值。

mysql SQL介绍

一、MySQL SQL介绍

1.1.1 SQL 应用基础

  1. SQL(结构化查询语句)介绍

SQL标准:SQL 92 SQL99
5.7版本后启用SQL_Mode 严格模式

  1. SQL作用

SQL 用来管理和操作MySQL内部的对象
SQL对象:
库:库名,库属性
表:表名,表属性,列名,记录,数据类型,列属性和约束

  1. SQL语句的类型

DDL:数据定义语言 data definition language
DCL:数据控制语言 data control language
DML:数据操作语言 data manipulation language
DQL:数据查询语言 data query language

  1. 数据类型

1、 作用:
控制数据的规范性,让数据有具体含义,在列上进行控制
2、字符串
char(32)
定长长度为32的字符串。存储数据时,一次性提供32字符长度的存储空间,存不满,用空格填充。
varchar(32):
可变长度的字符串类型。存数据时,首先进行字符串长度判断,按需分配存储空间
会单独占用一个字节来记录此次的字符长度
超过255之后,需要两个字节长度记录字符长度。
定长 的字符串类型,在存储字符串时,最大字符长度11个,立即分配11个字符长度的存储空间,如果存不满,空格填充。
变长的字符串类型看,最大字符长度11个。在存储字符串时,自动判断字符长度,按需分配存储空间。
3、 enum 枚举类型
enum(‘bj‘,‘sh‘,‘sz‘,‘cq‘,‘hb‘,......)
数据行较多时,会影响到索引的应用
枚举类型,比较适合于将来此列的值是固定范围内的特点,可以使用enum,可以很大程度的优化我们的索引结构。
注意:数字类禁止使用enum类型
4、 数字
tinyint : -128~127
int :-231~231-1
说明:手机号是无法存储到int的。一般是使用char类型来存储收集号
5、时间
timestamp
datetime
技术图片
技术图片
技术图片
技术图片

  1. 表属性
存储引擎:engine  =(myisam  innodb)
字符集:charset  = (utf8mb4)
utf8    中文  三个字节长度
utf8mb4 中文  四个字节长度    才是真正的utf8 
支持emoji(表情)字符
排序规则(校对规则) collation 
针对英文字符串大小写问题
  1. 列的类型与约束
6.1 主键: primary key (PK)
说明:
唯一
非空

数字列,整数列,无关列,自增的.

聚集索引列? 
是一种约束,也是一种索引类型,在一张表中只能有一个主键。

6.2 非空: Not NULL 
说明:
我们建议,对于普通列来讲,尽量设置not null 
默认值 default : 数字列的默认值使用0 ,字符串类型,设置为一个nil null

6.3 唯一:unique 
不能重复

6.4 自增 auto_increment
针对数字列,自动生成顺序值

6.5 无符号 unsigned
针对数字列  

6.6 注释 comment 

二、MySQL DDL 语句操作

2.1.1 MySQL DDL 库表语句操作

  1. DDL:数据定义语言 库操作
#mysql帮助命令
mysql> help create database;
Name: ‘CREATE DATABASE‘
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: http://dev.mysql.com/doc/refman/5.7/en/create-database.html

#创建数据库
mysql> create database chenhj character set utf8mb4 collate  utf8mb4_general_ci;  #后面是字符集合校对规则
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenhj             |
| kaode              |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
7 rows in set (0.08 sec)

#查看建库语句
mysql> show create database chenhj;
+----------+--------------------------------------------------------------------+
| Database | Create Database                                                    |
+----------+--------------------------------------------------------------------+
| chenhj   | CREATE DATABASE `chenhj` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ |
+----------+--------------------------------------------------------------------+
1 row in set (0.00 sec)


#修改库
mysql> alter database chenhj character set utf8;  #修改字符集
Query OK, 1 row affected (0.01 sec)
mysql> show create database chenhj;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| chenhj   | CREATE DATABASE `chenhj` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

#删除数据库
mysql> drop database kaode;
Query OK, 22 rows affected (0.17 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| chenhj             |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)
  1. 表操作
#建表建库规范:
1、库名和表名是小写字母不要大写字母
为啥?不同的开发和系统平台可能会出现问题,比如window环境不区分大小写,
但是Linux平台的区分大小写的。
2、不能以数字开头
3、不支持-  支持_
4、内部函数名不能使用
5、名字和业务功能有关(his,jf,yz,oss,erp,crm...)

mysql> create user root@‘10.0.0.%‘ identified by ‘123456‘ ;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all on *.* to root@‘10.0.0.%‘;
Query OK, 0 rows affected (0.00 sec)

#建表
mysql> CREATE TABLE `NewTable` (
`id`  int NOT NULL COMMENT ‘学号‘ ,
`name`  varchar(255) NOT NULL COMMENT ‘学生姓名‘ ,
`age`  tinyint UNSIGNED ZEROFILL NOT NULL COMMENT ‘学生年龄‘ ,
`gender`  enum(‘f‘,‘n‘,‘m‘) NOT NULL DEFAULT ‘n‘ COMMENT ‘性别‘ ,
PRIMARY KEY (`id`)
)
;

#查看建表语句
mysql> show create table chenhj.stu;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                                                                                                 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| stu   | CREATE TABLE `stu` (
  `id` int(11) NOT NULL COMMENT ‘学号‘,
  `name` varchar(255) NOT NULL COMMENT ‘学生姓名‘,
  `age` tinyint(3) unsigned zerofill NOT NULL COMMENT ‘学生年龄‘,
  `gender` enum(‘f‘,‘n‘,‘m‘) NOT NULL DEFAULT ‘n‘ COMMENT ‘性别‘,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4             |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

CREATE TABLE oldchen(
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘学号‘,
name varchar(255) not null comment ‘姓名‘,
age TINYINT UNSIGNED NOT NULL DEFAULT ‘0‘ COMMENT ‘年龄‘,
gender ENUM(‘M‘,‘F‘,‘N‘) NOT NULL  default ‘n‘ COMMENT ‘性别‘
)charset=utf8mb4 engine=innodb;

#改表
#添加一个手机号码列 (add)
mysql> alter table oldchen add telnum char(11) unique not null  comment ‘手机号码‘;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| name   | varchar(255)        | NO   |     | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   |     | NULL    |                |
+--------+---------------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> alter table oldchen add state tinyint unsigned not null default 1 comment ‘状态列‘;
Query OK, 0 rows affected (0.34 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| name   | varchar(255)        | NO   |     | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

#name列后面添加qq列 (add 和 after)
mysql> alter table oldchen add qq varchar(255) unique not null comment ‘qq号码‘ after name;
Query OK, 0 rows affected (1.68 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)

#在name 之前添加wechat列 
mysql> alter table oldchen add wechat varchar(255) unique not null comment ‘微信‘ after id;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

#在首列添加学生号
mysql> alter table oldchen add sid varchar(255) unique not null comment ‘学生号‘ first;
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| sid    | varchar(255)        | NO   | UNI | NULL    |                |
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

#修改列属性(modify)将sid列的varchar(255)改成varchar(128)
mysql> alter table oldchen modify sid varchar(128) unique not null;
Query OK, 0 rows affected, 1 warning (1.77 sec)
Records: 0  Duplicates: 0  Warnings: 1

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| sid    | varchar(128)        | NO   | UNI | NULL    |                |
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.01 sec)

#修改列名(change)将sid改成SID
mysql> alter table oldchen change sid  SID varchar(128) not null;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| SID    | varchar(128)        | NO   | UNI | NULL    |                |
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

#再改回来
mysql> alter table oldchen change SID sid varchar(128) not null comment ‘学生号‘;
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

#删除列(drop)
mysql> alter table oldchen drop sid;
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

#添加回来
mysql> alter table oldchen add sid varchar(128) unique not null comment ‘学生号‘ first;
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0


#删除表
mysql> show create table chenhj.oldchen;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                                                                                               |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| oldchen | CREATE TABLE `oldchen` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘学号‘,
  `name` varchar(255) NOT NULL COMMENT ‘姓名‘,
  `age` tinyint(3) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘年龄‘,
  `gender` enum(‘M‘,‘F‘,‘N‘) NOT NULL COMMENT ‘性别‘,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4         |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> drop table chenhj.oldchen;
Query OK, 0 rows affected (0.31 sec)
mysql> show create table chenhj.oldchen;
ERROR 1146 (42S02): Table ‘chenhj.oldchen‘ doesn‘t exist
  1. 创建一个已经存在且属性结构相同的空表(link)
mysql> create table chen like oldchen ;
Query OK, 0 rows affected (0.30 sec)

mysql> desc chen;
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| sid    | varchar(128)        | NO   | UNI | NULL    |                |
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)

mysql> desc oldchen;  
+--------+---------------------+------+-----+---------+----------------+
| Field  | Type                | Null | Key | Default | Extra          |
+--------+---------------------+------+-----+---------+----------------+
| sid    | varchar(128)        | NO   | UNI | NULL    |                |
| id     | int(11)             | NO   | PRI | NULL    | auto_increment |
| wechat | varchar(255)        | NO   | UNI | NULL    |                |
| name   | varchar(255)        | NO   |     | NULL    |                |
| qq     | varchar(255)        | NO   | UNI | NULL    |                |
| age    | tinyint(3) unsigned | NO   |     | 0       |                |
| gender | enum(‘M‘,‘F‘,‘N‘)   | NO   |     | N       |                |
| telnum | char(11)            | NO   | UNI | NULL    |                |
| state  | tinyint(3) unsigned | NO   |     | 1       |                |
+--------+---------------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
  1. 清空表数据(删除内容、释放空间但不删除定义)
mysql> truncate tbale 表名
truncate table oldchen;

三、MySQL DML语句操作

3.1.1 MySQL DML语句表操作

  1. MySQL DML语句表操作
#删除不必要的列
mysql> alter table oldchen drop sid;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table oldchen drop wechat;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table oldchen drop state;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table oldchen drop gender;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table oldchen drop telnum;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc oldchen;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | int(11)             | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255)        | NO   |     | NULL    |                |
| qq    | varchar(255)        | NO   | UNI | NULL    |                |
| age   | tinyint(3) unsigned | NO   |     | 0       |                |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
  1. insert 插入数据语句的使用
mysql> desc oldchen;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | int(11)             | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255)        | NO   |     | NULL    |                |
| qq    | varchar(255)        | NO   | UNI | NULL    |                |
| age   | tinyint(3) unsigned | NO   |     | 0       |                |
+-------+---------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> insert into oldchen values(1,‘chen‘,‘1010235677‘,18);
Query OK, 1 row affected (0.00 sec)

mysql> select id ,name, qq, age from oldchen;
+----+------+------------+-----+
| id | name | qq         | age |
+----+------+------------+-----+
|  1 | chen | 1010235677 |  18 |
+----+------+------------+-----+
1 row in set (0.00 sec)

#生产规范插入
mysql> insert into oldchen(name,qq,age) values(‘liu‘,‘577397908‘,25);
Query OK, 1 row affected (0.00 sec)
mysql> select id ,name, qq, age from oldchen;
+----+------+------------+-----+
| id | name | qq         | age |
+----+------+------------+-----+
|  1 | chen | 1010235677 |  18 |
|  2 | liu  | 577397908  |  25 |
+----+------+------------+-----+
2 rows in set (0.00 sec)
#id是自增长可加可不加

mysql> insert into oldchen (name ,qq,age) values(‘wang‘,‘1314‘,18);
Query OK, 1 row affected (0.00 sec)

mysql> select id ,name, qq, age from oldchen;
+----+------+------------+-----+
| id | name | qq         | age |
+----+------+------------+-----+
|  1 | chen | 1010235677 |  18 |
|  2 | liu  | 577397908  |  25 |
|  3 | wang | 1314       |  18 |
+----+------+------------+-----+
3 rows in set (0.00 sec)

mysql> insert into oldchen (name ,qq,age) values(‘qiuan‘,‘553208‘,18);
Query OK, 1 row affected (0.00 sec)

mysql> select id ,name ,qq,age from oldchen;
+----+-------+------------+-----+
| id | name  | qq         | age |
+----+-------+------------+-----+
|  1 | chen  | 1010235677 |  18 |
|  2 | liu   | 577397908  |  25 |
|  3 | wang  | 1314       |  18 |
|  4 | qiuan | 553208     |  18 |
+----+-------+------------+-----+
4 rows in set (0.00 sec)
  1. update 更新数据语句使用 (update set) #这条命令相当危险,注意一定要加where条件
mysql> select id ,name ,qq,age from oldchen;
+----+-------+------------+-----+
| id | name  | qq         | age |
+----+-------+------------+-----+
|  1 | chen  | 1010235677 |  18 |
|  2 | liu   | 577397908  |  25 |
|  3 | wang  | 1314       |  18 |
|  4 | qiuan | 553208     |  18 |
+----+-------+------------+-----+
4 rows in set (0.00 sec)

mysql> update oldchen set qq=‘1314520‘ where id=4;
Query OK, 1 row affected (1.10 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select id ,name ,qq,age from oldchen;
+----+-------+------------+-----+
| id | name  | qq         | age |
+----+-------+------------+-----+
|  1 | chen  | 1010235677 |  18 |
|  2 | liu   | 577397908  |  25 |
|  3 | wang  | 1314       |  18 |
|  4 | qiuan | 1314520    |  18 |
+----+-------+------------+-----+
4 rows in set (0.00 sec)

mysql> update oldchen set name=‘wanglin‘ where id=3;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select id ,name ,qq,age from oldchen;
+----+---------+------------+-----+
| id | name    | qq         | age |
+----+---------+------------+-----+
|  1 | chen    | 1010235677 |  18 |
|  2 | liu     | 577397908  |  25 |
|  3 | wanglin | 1314       |  18 |
|  4 | qiuan   | 1314520    |  18 |
+----+---------+------------+-----+
4 rows in set (0.00 sec)
  1. delete 删除行数据语句使用(delete from ) 这条命令相当危险,注意一定要加where条件
mysql> select id ,name ,qq,age from oldchen;
+----+---------+------------+-----+
| id | name    | qq         | age |
+----+---------+------------+-----+
|  1 | chen    | 1010235677 |  18 |
|  2 | liu     | 577397908  |  25 |
|  3 | wanglin | 1314       |  18 |
|  4 | qiuan   | 1314520    |  18 |
+----+---------+------------+-----+
4 rows in set (0.00 sec)

mysql> delete from oldchen where id=4;
Query OK, 1 row affected (0.01 sec)
mysql> select id ,name ,qq,age from oldchen;
+----+---------+------------+-----+
| id | name    | qq         | age |
+----+---------+------------+-----+
|  1 | chen    | 1010235677 |  18 |
|  2 | liu     | 577397908  |  25 |
|  3 | wanglin | 1314       |  18 |
+----+---------+------------+-----+
3 rows in set (0.00 sec)

mysql> delete from oldchen where name=‘wanglin‘;
Query OK, 1 row affected (0.00 sec)

mysql> select id ,name ,qq,age from oldchen;
+----+------+------------+-----+
| id | name | qq         | age |
+----+------+------------+-----+
|  1 | chen | 1010235677 |  18 |
|  2 | liu  | 577397908  |  25 |
+----+------+------------+-----+
2 rows in set (0.00 sec)

#清空表数据而不改变表结构属性
delete from oldchen;  (逻辑逻辑删除,语句一条一条执行,效率慢)
truncate table oldchen (物理清空 ,效率高)
DELETE 和 TRUNCATE 区别
1、 DELETE 逻辑逐行删除,不会降低自增长的起始值。
效率很低,碎片较多,会影响到性能
2、TRUNCATE ,属于物理删除,将表段中的区进行清空,不会产生碎片。性能较高
  1. 生产需求:使用update替代delete,进行伪删除
#首先添加一个状态列(1代表存在,0代表删除)
mysql> alter table oldchen add state tinyint unsigned not null default 1 comment ‘状态‘;
Query OK, 0 rows affected (0.39 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select id ,name ,qq,age ,state from oldchen;
+----+------+------------+-----+-------+
| id | name | qq         | age | state |
+----+------+------------+-----+-------+
|  1 | chen | 1010235677 |  18 |     1 |
|  2 | liu  | 577397908  |  25 |     1 |
+----+------+------------+-----+-------+
2 rows in set (0.00 sec)

#利用update将某一列的状态改为0 模拟设置伪删除
mysql> update oldchen set state=0 where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select id ,name ,qq,age ,state from oldchen;
+----+------+------------+-----+-------+
| id | name | qq         | age | state |
+----+------+------------+-----+-------+
|  1 | chen | 1010235677 |  18 |     1 |
|  2 | liu  | 577397908  |  25 |     0 |
+----+------+------------+-----+-------+
2 rows in set (0.00 sec)

#业务语句修改,利用select条件查询

mysql> select id,name,age,qq,state from oldchen where state=1;
+----+------+-----+------------+-------+
| id | name | age | qq         | state |
+----+------+-----+------------+-------+
|  1 | chen |  18 | 1010235677 |     1 |
+----+------+-----+------------+-------+
1 row in set (0.00 sec)

#这样你就看不到状态为0的数据,这就是伪删除

































以上是关于MySQL SQL介绍的主要内容,如果未能解决你的问题,请参考以下文章

sql sql里面的代码片段

SQL Prompt7.2下载及破解教程

Microsoft SQL Server 代码片段收集

缺少 SQL SERVER 2014 代码片段

sql Oracle代码片段

sql 日期转换代码片段 - Dato,120,konvertere