非关系型数据库
关系型数据库
Oracle mysql sqlserver db2 Postgresql Sqlite access

sqlserver 微软
db2 ibm
========================================
数据库: 一些关联表的集合
主键: 主键是唯一的 标识
外键: 用于关联两个表

复合键:
索引: 把一个字段做优化

参照完整性: 引用的实体必须要存在

二叉树:

==============================================
mysql 增删查改

offset 偏移量
limit

limit必须要和offset联合使用不能单独使用

mysql中%表示匹配所有
查找
select * from student where sname like "宋%";

修改:
update student set sname="赵丽颖",gender="女" where sid="4";

删除:
delete from student where sid>12;

==============================================

排序: order by
asc 升序 desc降序
mysql中默认是升序

select * from student order by class_id desc; #降序
select * from student order by class_id; #默认升序

分组排序: group by
将查询的数据可以按照某个字段来分组(字段中不同的值即为一个组)

如:将student表中按照class_id(班级)分组,同时统计每个班级的人数.
mysql> select * from student;
+-----+-----------+--------+----------+
| sid | sname | gender | class_id |
+-----+-----------+--------+----------+
| 1 | 张三 | 男 | 4 |
| 2 | 李四 | 男 | 4 |
| 3 | 王五 | 男 | 3 |
| 4 | 赵丽颖 | 女 | 3 |
| 5 | 宋2 | 男 | 3 |
| 6 | 宋3 | 女 | 3 |
| 7 | 宋4 | 女 | 3 |
| 8 | 宋5 | 女 | 3 |
| 9 | 成龙1 | 男 | 4 |
| 10 | 成龙2 | 男 | 4 |
| 11 | 成龙3 | 女 | 4 |
| 12 | 成龙4 | 女 | 4 |
| 15 | 李白 | 男 | 3 |
| 16 | 林允儿 | 女 | 1 |
+-----+-----------+--------+----------+
14 rows in set (0.00 sec)

mysql> select class_id,count(*) from student group by class_id;
+----------+----------+
| class_id | count(*) |
+----------+----------+
| 4 | 6 |
| 3 | 7 |
| 1 | 1 |
+----------+----------+
3 rows in set (0.00 sec)

使用 WITH ROLLUP
WITH ROLLUP 可以统计该字段数据的总和

mysql> select class_id,count(*) from student group by class_id with rollup;
+----------+----------+
| class_id | count(*) |
+----------+----------+
| 1 | 1 |
| 3 | 7 |
| 4 | 6 |
| NULL | 14 |
+----------+----------+
4 rows in set (0.00 sec)

这里的NULL表示三个班级的总人数
我们可以使用 coalesce 来设置一个可以取代 NUll 的名称,coalesce 使用如下:

mysql> select coalesce(class_id,"总人数"),count(class_id) as number from student group by class_id with rollup;
+--------------------------------+--------+
| coalesce(class_id,"总人数") | number |
+--------------------------------+--------+
| 1 | 1 |
| 3 | 7 |
| 4 | 6 |
| 总人数 | 14 |
+--------------------------------+--------+
4 rows in set (0.00 sec)

添加字段:

mysql enum是指字段的类型 表示枚举类型

mysql> alter table student add adders enum("sichuang","shanghai");
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0

这里表示在student表中添加一个adders的字段,这个字段里面的值只能是sichuang 或者shanghai 如果添加的一行新的数据不为这两个值则会报错,(如果允许了可以为空,那么让其为空也是可以)
否则插入一个其他的值则会报错,如下:

mysql> insert into student (sname,gender,class_id,adders) values("李自成1","男","1","aaaa");
ERROR 1265 (01000): Data truncated for column 'adders' at row 1

删除字段:
删除adders字段:

mysql> alter table student drop adders;
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0

修改字段数据类型(modify和change都可以修改):

mysql> alter table student modify sname char(16);
Query OK, 15 rows affected (0.16 sec)
Records: 15 Duplicates: 0 Warnings: 0

使用change修改字段类型的时候需要,即使不修改字段名称但是也要把原名称写上,否则会报错
mysql> alter table student change name name varchar(32) not null;
Query OK, 15 rows affected (0.03 sec)
Records: 15 Duplicates: 0 Warnings: 0

修改字段名称: (只能是用change)
注意修改字段名称只能是用change,modify不能修改字段名称。

mysql> alter table student change gender sex char(32) not null;
Query OK, 15 rows affected (0.11 sec)
Records: 15 Duplicates: 0 Warnings: 0

ALTER TABLE 对 Null 值和默认值的影响
当你修改字段时,你可以指定是否包含只或者是否设置默认值。

以下实例,指定字段 j 为 NOT NULL 且默认值为100 。
mysql> ALTER TABLE testalter_tbl
-> MODIFY j BIGINT NOT NULL DEFAULT 100;

修改表名:

mysql> alter table student rename student_new;
Query OK, 0 rows affected (0.07 sec)

查看表的创建命令记录:

mysql> show create table student_new;
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student_new | CREATE TABLE `student_new` (
`sid` int(32) NOT NULL,
`name` varchar(32) NOT NULL,
`sex` char(32) NOT NULL,
`class_id` int(11) NOT NULL,
`phonenumber` enum('181') DEFAULT NULL,
PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

===============================================

mysql 增删查改的更多相关文章

  1. php mysql增删查改

    php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root');  //连接数据库代码 mysql_query("set na ...

  2. node.js+mysql增删查改

    数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...

  3. 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)

    1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...

  4. 后端Spring Boot+前端Android交互+MySQL增删查改

    2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...

  5. MySQL 增删查改 必知必会

    MySQL 数据库中的基础操作 3.表的修改 对表的表名.字段.字段类型.字段长度.约束等进行修改. 3.1 表的名称修改 -- 语法: ALTER TABLE 库名.表名 RENAME TO 新表名 ...

  6. python操作mysql增删查改

    # coding=utf-8 ''' python操作mysql,需安装MySQLdb驱动 安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-py ...

  7. 2016/3/13 MySQL 增删查改 CRUD 用代码实现

    用代码实现数据库数据输入 T-SQL语句 查询语句分几块 ①创建表 create table Car (Code varchar(50) primary key, #primary key 主键 定义 ...

  8. MySQL增删查改语句(入门)

    目录 create alter: insert delete update select 数据库定义语句: create:创建数据库及表对象 drop:删除数据库及表对象 alter:修改数据库及表对 ...

  9. mysql增删查改练习

    建表 班级表 create table class( cid int auto_increment unique, caption varchar(32) not null default '' )c ...

随机推荐

  1. [Selenium] Explicit wait 方法

    (1)  new WebDriverWait(driver, 10). until(ExpectedConditions.elementToBeClickable(locator)); (2)  ne ...

  2. [Selenium] The most commonly used CSSSelector

    CSSSelector Example Description element.element div.dropdown Select all  <div> elements whose ...

  3. explain之二:Explain 结果解读与实践,分析诊断工具之二

    MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...

  4. C# 利用Aspose.Slides.dll将本地ppt文档转化成pdf(完美破解版 无水印 无中文乱码)

    下载Aspose.Slides.dll   http://pan.baidu.com/s/1kVPjnzL 添加引用C#代码. using System; using System.Collectio ...

  5. k8s-存储卷2-configMap-Secret-十三

    一.StorageClass 在前一篇文章中,手动定义了pv,并让pvc关联至pv:现实中在pvc申请存储空间时,未必就有现成的pv符合pvc申请的需求,该怎么办呢? Kubernetes提供了描述存 ...

  6. 009--test命令和grep作业及Sed作业awk作业和循环结构

    一.test命令 -d :目录                                test  -d  /boot -s :文件长度 > 0.非空            test  - ...

  7. Ruby主要方法

         方法定义        def hello(name) ...  end                                                函数名 参数 作用 备 ...

  8. 洛谷 - P1002 - 过河卒 - 简单dp

    https://www.luogu.org/problemnew/show/P1002 方程很好想,题目也很暴力.感谢题目提示数据会很大. #include<bits/stdc++.h> ...

  9. CodeForces 13A【暴力】

    题意: 给你的一个十进制数n,计算对于2~n-1进制下的每个位相加和与数n-2的比值. 思路: n是1000,所以直接暴力一发? #include<cstdio> #include< ...

  10. hdoj5387【模拟】

    题意: 略: 思路: 把所有的角度按照分母的形式写,中间不要约,不要除...(然后我就wa了),本来是想保证结果的正确性,最后会造成约好以后分子很大..>360°: /* 这个案例不错,妈的,随 ...