MySQL基础之数据管理【1】
添加记录
insert [into] tbl_name[(col_name,...)] {value|values}(values...);
--不指定字段名称时需要按照建表时的字段顺序给每一个字段赋值
--插入多条数据用逗号隔开
insert tbl_name value(values...);
insert tbl_name set 字段名称=值,...;
insert tbl_name[(字段名称...)] select 字段名称,... from tbl_name [where 条件];
测试
create database if not exists king default character set 'utf8';
use king;
create table user(
id int unsigned auto_increment key comment '编号',
username varchar(20) not null unique comment '用户名',
age tinyint unsigned default 18 comment '年龄',
email varchar(50) not null default '325@qq.com' comment '邮箱'
)engine=innodb charset=utf8;
insert user value(1,'king',24,'12335@qq.com');
insert user value(null,'queen',20,'1235235@qq.com');
insert user(id,username) value(3,'wang');
create table if not exists user2(
name varchar(20) not null default 'aafd'
)engine=innodb charset=utf8;
insert user2 value('aaa'),
('bbb'),
('ccc');
修改记录
update tbl_name set 字段名称=值,字段名称=值,... [where 条件];
--如果不添加条件,整个表中的记录都会被更新
测试
update user set age=29 where id=1;
update user set username='黎明',age=1,email='111@qq.com' where id=3;
update user set age=age+10;
update user set age=age-5,email=default where id<=5;
删除记录
delete from tbl_name [where 条件]
--如果不添加条件,表中所有记录都会被删除
测试
delete from user where username='king';
delete from user where age>=23;
delete from user; --删除表中所有记录
alter table user auto_increment=1; --将auto_increment重置为1
truncate [table] tbl_name; --彻底将表清空
查询记录 select语句的基本形式
select select_expr,... from tbl_name [where 条件]
[group by {col_name|position} having 二次筛选]
[order by {col_name|position|expr} [asc|desc]]
[limit 限制结果集的显示条数];
select * from tbl_name; --查询表中所有记录 *所有字段
select 字段名称,... from tbl_name --指定字段的信息
select 字段名称 [as] 别名名称,... from db_name.tbl_name; --给字段起别名
select 字段名称,... from tbl_name [as] 别名; --给数据表起别名
select tbl_name.col_name,... from tbl_name; --表名.字段名
测试
create table user(
id int unsigned auto_increment key comment '编号',
username varchar(20) not null unique comment '姓名',
age tinyint unsigned not null default 18 comment '年龄',
sex enum('男','女','保密') not null default '保密' comment '性别',
addr varchar(20) not null default '北京',
married tinyint(1) not null default 0 comment '0代表未婚,1代表已婚',
salary float(8,2) not null default 0 comment '薪水'
)engine=innodb charset=utf8;
insert user values(1,'king',23,'男','上海',1,50000);
insert user(username,age,sex,addr,married,salary) values('queen',27,'女','上海',0,25000);
insert user set username='imooc',age=31,sex='女',addr='北京',salary=40000;
insert user values(null,'张三',38,'男','上海',0,15000),
(null,'张子枫',38,'男','上海',0,15000),
(null,'子怡',25,'女','北京',0,85000),
(null,'王菲',62,'女','广州',0,95000),
(null,'刘德华',14,'男','南京',0,115000),
(null,'吴亦凡',35,'男','上海',0,75000),
(null,'张阿文',14,'男','西安',0,65000),
(null,'经过历',25,'男','湖南',0,15000);
select * from user;
select username,addr,age from user;
select * from king.user; --不用打开数据库就可以查询指定的数据表
select id as 'id',username as '姓名' from user;
select id,username from user as u;
select user.id,user.username from user;
MySQL基础之数据管理【1】的更多相关文章
- MySQL基础之数据管理【4】
外键约束的使用(只有InnoDB存储引擎支持外键) create table news_cate( id tinyint unsigned auto_increment key comment '编号 ...
- MySQL基础之数据管理【3】
MySQL中的多表联查 --查询emp的id username age depName create table emp( id int unsigned auto_increment key, us ...
- MySQL基础之数据管理【5】
子查询的使用 select 字段名称 from tbl_name where col_name=(select col_name from tbl_name); --内层语句查询的结果可以作为外层语句 ...
- MySQL基础之数据管理【2】
where条件筛选记录 select id,username,age from uesr where id=5; alter table user add userDesc varchar(100); ...
- MySQL基础----py全栈
目录 MySQL基础----py全栈 一.引言 1.什么是数据? 2.什么是数据库(DB)? 3.什么是数据库管理系统(DBMS)? 4.什么是数据库系统? 5.数据库管理系统由来 6.什么是数据模型 ...
- Mysql基础代码(不断完善中)
Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...
- MYSQL基础操作
MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...
- 【夯实Mysql基础】记一次mysql语句的优化过程
1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同时使 ...
- MySQL基础(非常全)
MySQL基础 一.MySQL概述 1.什么是数据库 ? 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access ...
随机推荐
- Winform中设置ZedGraph鼠标悬浮显示举例最近曲线上的点的坐标值和X轴与Y轴的标题
场景 Winform中设置ZedGraph鼠标双击获取距离最近曲线上的点的坐标值: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/ ...
- Servlet小结(面试)
1.doGet()和doPost()区别/get和post请求方法区别: (1)在form表单中:method = “get/post”相对应doGet和doPost方法. (2)在http协议中: ...
- windows 应急流程及实战演练
前言 本文摘自信安之路公众号的文章. 当企业发生黑客入侵.系统崩溃或其它影响业务正常运行的安全事件时,急需第一时间进行处理,使企业的网络信息系统在最短时间内恢复正常工作,进一步查找入侵来源,还原入侵事 ...
- ios中设置UIButton圆角,添加边框
//例如: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(50, ...
- 7.智能快递柜(APP及微信公众号)
1.智能快递柜(开篇) 2.智能快递柜(终端篇) 3.智能快递柜(通信篇-HTTP) 4.智能快递柜(通信篇-SOCKET) 5.智能快递柜(通信篇-Server程序) 6.智能快递柜(平台篇) 7. ...
- 初识Kotlin之函数
本章通过介绍Kotlin的基本函数,默认参数函数,参数不定长函数,尾递归函数,高阶函数,Lamdba表达式.来对Kotlin函数做进一步了解.将上一篇的Kotlin变量的知识得以运用.Kotlin变量 ...
- Linux开发环境搭建三 使用mount -t cifs 挂载windows共享目录方法与问题解决
转载链接:https://blog.csdn.net/fuyuande/article/details/82915800 嵌入式开发通常是在linux环境下编译,windows下开发,这就需要在lin ...
- July 7th, 2019. Week 27th, Sunday
We laughed and kept syaing "see you soon", but inside we both knew we would never see each ...
- golang中的定向通道(Directional channels)
好像第一次看到这个知识点,作个记录. 注意通道在只能发射或只能接收信息时,<-这个符号放置的位置. package main import "fmt" import &quo ...
- 201871010114-李岩松《面向对象程序设计(java)》第十六周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...