作为一个程序员那么对数据库的应用那就是必不可少的、如下就是我对MSQL数据库的一些总结希望能够帮到大家
数据库(Database):是一个储存是数据的仓库,代码分服务端与客户端,底层使用Socket进行通讯,客户端与服务端可能不在一台主机上。(背)
常用数据库:MySQL、oracle、db2、access、mangodb(背)
操作偶数据库语言(SQL读音同circle、结构化查询语言)(背)
DDL 数据定义语言(背)
DML 数据操作语言(背)
DQL 数据查询语言(背)
TCL 事物控制语言(背)
数据库的 常用操作:CRUD、增删改查
C create 增
R retrieve 查
U update 改
D delete 删
表的命名:习惯使用复数
在安装数据库的电脑上运行Command Line Client
Mysql>show databases;查看所有的数据库
Use mysql;进入mysql这个数据库
Show tables; 查看所有的表
Exit;退出
Mysql 默认端口号:3306
Oracle 默认端口号:1521
R-查询
所用语言:DQL(Data Query Language)-数据库查询语言
------------------------------Show
Show databases——show tables
------------------------------Select
select name,stock——select name as 书名,stock as “库 存” ——
select 2+1——select avg(price)——select sum(price)——
select max(price)——select count(distinct(id))——去重计数
------------------------------Where
Or——and——(=,<,>,<>,)——stock is null//stock not null——
Name like ‘_好%’——
------------------------------order
By price asc\desc——by atock desc,pricec asc
-------------------------------limit
4,2
--------------------------------group||having
Select count(id),publishedby Group by publishedby having publishedby=’人民出版社’
--------------------------------子查询
Select avg(b.price) b是子表别名
From(
Select price from books
Where id>3
)b b是子表别名 括号里面是子表
--------------------------------多表查询
SQL86标准
Select u.name account,c.name name,c.tel tle
From consignees c,users u
Where c.userid=u.id
SQL99标准
Select *
From users u join consignees c join连接查询 查询完前面再查后面
On (u.id=c.userid) On满足括号里面的条件
Select *
From users u left out join consignees c left out(左)/right out(右)外连接 显示不满足条件的信息 out可以省略
On (u.id=c.userid)
===================================================
DML-数据操作语言
C-增加数据
---------------inser into
students values (1,’tome’,12) ——students(id,name) values(1,’tom’)
students(id,name) value (2,’aa’),(3,’bb’)
U-修改数据
----------------------update||set
Update students set name=’cc’,url=’’ where id=4
D-删除数据
Delete from students
Where id=1
DDL-数据定义语言
------------------------------新建表
Create table teachers(
Id int(10) primary key,
Name varchar(100)
)
Create table 表名(列名 列类型 (长度))
---------------primary key主键
Id int(10) primary key auto_increment主键自增
---------------unique 唯一的约束
------------check() 检查约束------------------有问题
Gender int(1) check(gender in(0,1,2))
Salary double(6,2) check(salary>=2000 and salary <=8000)
----------------查看表列类型
Desc teachers
---------------------重命名表名-rename table,to
Rename table teachers2 to teachers3
---------------------重命名列名-alter table,change
Alter table teachers3
Change birth birthday date
--------------------增加列3
Alter table teachers3
Add age int(10)
----------------修改列
Alter table teachers3
Modify teachername vachar(200)
------------------删除列
Alter table teachers3
Drop column age
---------------删除表
1hers3
----------------清空表
Truncate table students
TCL-事务控制语言
什么是事务?
一组实现特定功能的DML 语句
特点:要么都成功,要么都失败
-------------事务的四个基本属性
ACID
原子性:事务是最小单位不可拆的
隔离性:在执行事务时其他语句不能来操作——事务的隔离级别
一致性:数据保持一致
持久性:能永久保存数据
----------------------开启事务
Start transaction;
---------------------进行事务操作
Update teachers set balance=blance-100 where id=2;
Update teachers set balance=blance+100 where id=1;
--------------------提交事务
Commit;
----------------事务的回滚
Rollback;只能回滚DML
------------------
Save
数据库函数
-------------------—数学函数
----round()—截取小数位且四舍五入
Select round(12.35,1)
---truncate()---截取数字
Select truncate(balance,1)
---pow()—开方
Select pow(2,3)—2的3次方
---sqrt—开平方根
Select sqrt(6)
-----------------------字符串函数
---trim()---祛除空格
Trim(‘ avd ’)
---replace()---替换
Select
---substring()---截取字符
Select substring(‘adc’,2,3) 第几个开始 取几个
---------------------------日期
----now()—当前日期
Select now() –看当前的 日期
-----curdate()当前日期
---curtime()当前时间
---日期装换
Str_to_date(abc,format)把abc字符串按照format给定的格式转换为日期类型
Date_format(date,format)把date这个日期按照format给定的格式转换为字符串类型
Select date_format(curdate(),’%y年%m月%d日’) 查看年月日
-------------------------------其他函数
Md5(abc) 吧abc字符串进行md5加密
Version() 当前数据库的版本
LAST_INSERT_ID() 查看最后一个插入的数据的主键
本文全面解析MSQL数据库操作,涵盖数据库基础知识、常用数据库介绍、SQL语言分类及CRUD操作详解,深入探讨数据定义、操作、查询及事务控制语言,并提供实用的数据库函数示例。
195

被折叠的 条评论
为什么被折叠?



