SQL语言

一、什么是SQL

SQL是Structure Query Language(结构化查询语言)的缩写;

二、SQL语句的分类

DDL(Data Definition Languages)数据定义语言(create、drop、alter、rename、truncate);

DML(Data Manipulation Language) 数据操纵语言,用于添加、删除、更新和查询数据库记录,并价差数据完整性,常用的关键字包括insert、delete、update;

DCL(Data Control  Lanuage)数据控制语句 grant 、revoke

DQL(Data  Query Lanuage)数据查询语言

三、数据库

一个数据库管理系统,可以包含多个数据库,一个数据库包含多个数据表,一个数据表包含多个字段,每个字段就是一个熟悉;一个数据表除了字段外,还有很多行,每一行都是一条完整的记录;

四、数据库操作

创建数据库

mysql> create database db2;
Query OK, 1 row affected (0.00 sec)
mysql> create database if not exists db4 default character set utf8;
Query OK, 1 row affected (0.00 sec)

查看创建的数据库

mysql> show create database db4;
+----------+--------------------------------------------------------------+
| Database | Create Database                                              |
+----------+--------------------------------------------------------------+
| db4      | CREATE DATABASE `db4` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+--------------------------------------------------------------+
1 row in set (0.02 sec)

修改数据库

mysql> alter database db4 default charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)

删除数据库

mysql> drop database db4;
Query OK, 0 rows affected (0.08 sec)

创建表

mysql> use db1;
Database changed
mysql> create table admin(id int,name varchar(20),passd char(20));
Query OK, 0 rows affected (0.04 sec)

创建表

mysql> create table tb_article(id int,title varchar(50),author varchar(20),content text); 
Query OK, 0 rows affected (0.04 sec)

查看表结构

mysql> desc tb_article;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| title   | varchar(50) | YES  |     | NULL    |       |
| author  | varchar(20) | YES  |     | NULL    |       |
| content | text        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.04 sec)

查看创建表的语句

mysql> show create table tb_article;
+------------+----------------------------------------------
| Table      | Create Table                                                             
+------------+-------------------------------------------------
| tb_article | CREATE TABLE `tb_article` (
  `id` int(11) DEFAULT NULL,
  `title` varchar(50) DEFAULT NULL,
  `author` varchar(20) DEFAULT NULL,
  `content` text
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+-----------------------------------------------

1 row in set (0.00 sec)

 修改数据表

mysql> alter table tb_article2 add pirce int after title;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_article2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| title   | varchar(50) | YES  |     | NULL    |       |
| pirce   | int(11)     | YES  |     | NULL    |       |
| author  | varchar(20) | YES  |     | NULL    |       |
| content | text        | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

 修改表的字段

mysql> alter table admin change name username varchar(30);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc admin;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
| time     | date        | YES  |     | NULL    |       |
| passd    | char(20)    | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

 修改字段类型

mysql> alter table admin modify passd varchar(20);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc admin;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | YES  |     | NULL    |       |
| username | varchar(30) | YES  |     | NULL    |       |
| time     | date        | YES  |     | NULL    |       |
| passd    | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

删除字段

mysql> alter table admin drop passd;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

修改编码格式

mysql> alter table admin default charset=utf8;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

重命名表

mysql> rename table admin to tb_admin;
Query OK, 0 rows affected (0.03 sec)

移动表到别的库

mysql> rename table db1.tb_admin  to db2.tb_admin2;
Query OK, 0 rows affected (0.03 sec)

 五、表的操作

mysql> create table tb_user(
    ->     id int,
    -> username varchar(20),
    -> age tinyint unsigned,
    -> gender enum('',''),
    -> address varchar(255)
    -> ) engine=innodb default charset=utf8mb4;
Query OK, 0 rows affected (0.02 sec)

 更新操作

mysql> update tb_user set id=2 where age=23;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

删除

mysql> delete from tb_user where id=3;
Query OK, 1 row affected (0.01 sec)

删除表

mysql> delete from tb_user;
Query OK, 1 row affected (0.01 sec)

删除表

mysql> truncate tb_user;
Query OK, 0 rows affected (0.06 sec)

delete from 与 truncate的区别

delete:删除数据记录

              数据操作语言(DML)

              在事务控制里边,DML语句要么commit,要么rollback

              删除大量记录速度慢,只删除数据,不回收高水位线

              可以带条件删除

truncate:删除所有数据记录

               数据定义语言(DDL)

              不在事务控制里边,DDL语句执行前会提交前面所有未提交的事务;

               清理大量事务速度快,回收高水位;(high  water  mark)

               不能带条件删除;

六、数据库的数据类型

 小数类型

 

 

字段decimal(18,9),18-9=9,这样整数部分和小数部分都是9,那两边分别占用4个字节; 2、字段decimal(20,6),20-6=14,其中小数部分为6,就对应上表中的3个字节,而整数部分为14,14-9=5,就是4个字节再加上表中的3个字节

decimal小数位数直接截取,不四舍五入;

字符类型:

     char类型:定长,0-255之间,占用定长的存储空间,不足的部分用空格填充,读取时候删掉后面的空格;

     varchar:变长,存储长度不固定的字符类型;

     text:超过255个字符,使用text

 enum:枚举类型;

blob:二进制类型;

 日期类型——datetime和timestamp区别

mysql> create table  tb_time(
    ->    id int not null auto_increment,
    ->    title varchar(80),
    ->    description varchar(255),
    ->    addtime datetime,
    ->    primary key(id)
    ->  )engine=innodb default  charset=utf8;
Query OK, 0 rows affected (0.04 sec)

mysql> desc tb_time;
+-------------+--------------+------+-----+---------+----------------+
| Field       | Type         | Null | Key | Default | Extra          |
+-------------+--------------+------+-----+---------+----------------+
| id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| title       | varchar(80)  | YES  |     | NULL    |                |
| description | varchar(255) | YES  |     | NULL    |                |
| addtime     | datetime     | YES  |     | NULL    |                |
+-------------+--------------+------+-----+---------+----------------+
4 rows in set (0.03 sec)

timestamp——默认用当前时间填充

mysql> create table  tb_time2(
    ->    id int not null auto_increment,
    ->    title varchar(80),
    ->    description varchar(255),
    ->    addtime timestamp,
    ->    primary key(id)
    ->  )engine=innodb default  charset=utf8;
Query OK, 0 rows affected (0.03 sec)

mysql> desc tb_time2;
+-------------+--------------+------+-----+-------------------+-----------------------------+
| Field       | Type         | Null | Key | Default           | Extra                       |
+-------------+--------------+------+-----+-------------------+-----------------------------+
| id          | int(11)      | NO   | PRI | NULL              | auto_increment              |
| title       | varchar(80)  | YES  |     | NULL              |                             |
| description | varchar(255) | YES  |     | NULL              |                             |
| addtime     | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+--------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.01 sec)

七、查询语句

like子句,%代表任意一个或者多个字符,_代表一个字符;

&& 逻辑与;

or 逻辑或;

 distinct去重复值

八、Group by 子句

 

MySQL5.7版本以后,分组的列必须出现在select后边

having 出现在group by 子句后面;

 limit子句:

             显示确定的几行  select * from  employee limit 5;

             分页

 九、多表联合查询

UNION联合查询:把多个表中的数据联合在一起进行显示,应用场景:分库分表;

select * from tb_student1 union select * from tb_student2;

 

posted @ 2023-12-25 18:40  中仕  阅读(21)  评论(0编辑  收藏  举报