当前位置:网站首页>MySQL讲解(二)
MySQL讲解(二)
2022-06-26 12:40:00 【C和弦~】
目录
一、SQL语句概述
1、SQL语言
- Structured Query Language 的缩写,即结构化查询语言
- 关系型数据库的标准语言
- 用于维护管理数据库
- 包括数据查询、数据更新、访问控制、对象管理等功能
2.SQL分类
DDL 数据定义语言,用于创建数据库对象,如库、表、索引等
DML 数据操纵语言,用于对表中的数据进行管理
DQL 数据查询语言,用于从数据表中查找符合条件的数据记录
DCL 数据控制语言,用于设置或者更改数据库用户或角色权限二.查看数据库结构
1.访问库
mysql -u root -p
密码
或者
mysql -u root -p 密码
2.查看数据库信息
show databases;
以英文的;结尾
3.查看表
use 库名 #先进入存放表的数据库
show tables; #显示当前库中所有的表

4.显示表的结构
describe 数据库.表名
或
desc 数据库.表名 
三.数据类型
类型 说明
int 整型(定义整数类型数据)
float 单精度浮点,4字节32位,准确到小数点后六位
double 双精度浮点,8字节64位
char 固定长度的字符类型,定义字符类数据
varchar 可变长度的字符类型
text 文本
image 图片
decimal (5,2) 5个有效长度数字,小数点后面有2位,指定长度数组
四.DDL
1.创建数据库
create database 数据库名;
2.创建表
create table 表名 (字段1 数据类型,字段2 数据类型 .... [primary key(主键名)]); 
field 字段
type 类型
not null 不允许为空
default 默认为空
primary key 主键(一般选择没有重复且不为空的字段)3.删除表
use 库名
drop table 表名;
或
drop table 库名.表明;
4.删除库
drop database 数据库名;

五.DML
1.插入新数据
insert into 表名(字段1,字段2[,...]) values(字段1的值,字段2的值,...);
字段中,可使用 password('密码') ,能够使用加密型密码

2.修改、更新原有数据
update 表名 set 字段名1=字段值1[,字段名2=字段值2] [where 条件表达式];

注:在更改数据时,需要谨慎,建议使用主键作为条件表达式选项,因为主键是唯一的。
3.删除数据
delete from 表名 [where 条件表达式];

五.DQL
1.查询数据
select 字段名1,字段名2[,...] from 表名 [where 条件表达式];
select * from 表名; #显示全部
select 字段1,字段2 from 表名; #显示字段1和字段2
select 字段1 from 表名\G; #以列表形式竖向显示
select * from 表名 info limit 2; #只显示头2行
select * from 表名 info limit 2,3; #显示第2行后的前3行



六.修改表明或表结构
1.修改表名
alter table 旧表名 rename 新表名;

2.扩展表结构
alter table 表名 add 扩展项名 数据类型;

3、修改字段(列)名,添加唯一键
alter table 表名 change 旧列名 新列名 数据类型 [unique key];
#change 可修改字段名、数据类型、约束等所有项
唯一键:唯一,但可以为空(空值只能出现一次)
主键包含唯一键的部分属性
唯一键不能完全作为主键

4、删除字段、主键
alter table 表名 drop 字段名;

总结
- 我们日常对数据库会进行“增、删、改、查”
- 建立主键是一定要使用唯一的
- where选项建议使用主键,因为唯一,不容易出错
- sql语句不区分大小写
- sql语句需要很高的精准,错一个字母都会导致语句执行失败
- 对数据库的操作一定要谨慎
边栏推荐
- Bridge mode
- National standard gb28181 protocol easygbs video platform TCP active mode streaming exception repair
- scrapy——爬取漫画自定义存储路径下载到本地
- Electron official docs series: Best Practices
- Map value
- zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
- Software testing - concept
- Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte
- Uva5009 error curves three points
- Fire warning is completed within 10 seconds, and Baidu AI Cloud helps Kunming Guandu build a new benchmark of smart city
猜你喜欢

C# const详解:C#常量的定义和使用

Processing random generation line animation

Biff TwinCAT can quickly detect the physical connection and EtherCAT network through emergency scan

First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology

10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆

P2393 yyy loves Maths II

Bridge mode

Group counting practice experiment 9 -- using cmstudio to design microprogram instructions based on segment model machine (2)

8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)

倍福PLC通过程序获取系统时间、本地时间、当前时区以及系统时间时区转换
随机推荐
Adapter mode
PostGIS geographic function
倍福TwinCAT3 NCI在NC轴界面中的基本配置和测试
UVa11582 [快速幂]Colossal Fibonacci Numbers!
Word document export (using fixed template)
mariadb学习笔记
D - skiing
J - Wooden Sticks poj 1065
利用scrapy爬取句子迷网站优美句子存储到本地(喜欢摘抄的人有福了!)
5月产品升级观察站
Arcpy - - utilisation de la fonction insertlayer (): ajout de calques dans un document de carte
Electron official docs series: Get Started
Uva11582 [fast power]colossal Fibonacci numbers!
Is it safe for the head teacher to open a stock account and open an account for financial management?
First pass! Baidu AI Cloud Xiling platform has obtained the authoritative certification of digital human ability evaluation from the Institute of information technology
Deep parsing MySQL binlog
Script - crawl the customized storage path of the cartoon and download it to the local
Guacamole installation
Bridge mode
10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆