当前位置:网站首页>初识数据库
初识数据库
2022-07-06 05:37:00 【ythswzgxlp】
命令符操作:
进入数据库:mysql -h 主机名 -P 端口号 -u 用户名 -p密码(并不是说都要写,但是避免出现错误最好都写,一般建议密码下一行输入保证安全)
查看数据库:show databases 数据库名;
查看表:show table 表名;
使用数据库:use 数据库名;
DQL:
查询语句的执行顺序以及一个查询语句包括的关键字:
连接类型分类:内连接(inner join),外连接,交叉连接
内连接分类:等值连接,非等值连接,自连接
外连接分类:左外连接(left outer),右外连接(right outer),全外连接
左外连接,left join左边的为主表;右外连接,right join右边的为主表
内连接和外连接的区别:内连接是两张表的交集部分;外连接则是主表加上连接部分
DDL:
创建:create 修改:alter 删除:drop
库的管理:
创建:create database 【if not exists(判断数据库是否已经存在)】 库名;
修改(一般是字符集的修改):alter database 库名 character set 字符集格式;
删除:drop database 【if exists(判断数据库是否已经存在)】 库名;
表的管理:
创建:create table 【if not exists】 表名(列名 列的类型 【(长度) 约束(可选)】,(列名 列的类型 【(长度) 约束(可选)】,... ,(列名 列的类型 【(长度) 约束(可选)】)
修改:alter table 表名 add | drop | modify | change column 列名 【列名 列类型 约束】
删除:drop table 【if exists】 表名;
复制:
仅仅复制表的结构,不会复制表内的内容:create table 要复制的表名 like 被复制的表名
复制表的结构和数据:create table 新表 select * from 被复制的表名
只复制部分数据:create table 新表 select ...(筛选条件和要复制的列等) from 被复制的表名
仅仅复制某些字段的结构:create table 新表 select ... where 0(使用0表示false,就不要数据了)
DML:
插入:insert into 表名 set 列名1=值1,列名2=值2,........;
修改:update 表名 set 列1=值1,列2=值2,.... where 筛选条件;
删除:方法一:truncate table 表名;
方法二:delete from 表名 where 筛选条件
常见约束:
分类(六大约束):
not null:非空约束,用于保证该字段的直不能为空
default 默认值:默认,用于保证该字段有默认值
primary key:用于保证该字段的值具有唯一性,并且非空
unioue:唯一,用于保证该字段的值具有唯一性,可以为空
check(检查条件):检查约束(mysql中不支持,但是不报错,只是没效果)
foreign key (从表列名)references 主表名(主表列名):主表的关联列必须是一个有唯一性的列(一般是主键或唯一,也可以是外键,但是存在重复不推荐)
数据库的三大范式:
第一范式:第一范式(1NF):属性不可分割,即每个属性都是不可分割的原子项。不允许属性还能再分割
第二范式:第二范式(2NF):满足第一范式;且不存在部分依赖,即非主属性必须完全依赖于主属性。例:主键与非主键之间有依赖。但是非主键之间不依赖
第三范式:第三范式(3NF):满足第二范式;且不存在传递依赖,即非主属性不能与非主属性之间有依赖关系,非主属性必须直接依赖于主属性,不能间接依赖主属性。
非主键必须直接依赖主键,非主键不能间接依赖主键
隔离级别分类:
read uncommitted:会出现脏读,不可重复读,幻读
read committed:避免脏读,会出现不可重复读,幻读
repeatable read:避免脏读,不可重复读,幻读
serializable:避免出现脏读,不可重复读,幻读,但是性能低
事务的ACID属性:
1.原子性(Atomicity):原子性是指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
2.一致性(Consistency):事务必须使数据从一个一致性状态变换到另外一个一致性状态。
3.隔离性(Isolation):事务的隔离性是指一个事务的执行不能被其他事务干扰,既一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
4.持久性(Durability):持久性是指一个事务一旦被提交,它对数据库数据的改变就是永久性的,接下来的其他操作和数据库故障不应该对其有任何影响。
边栏推荐
- [QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- ArcGIS application foundation 4 thematic map making
- Redis消息队列
- First acquaintance with CDN
- [detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
- Promotion hung up! The leader said it wasn't my poor skills
- In 2022, we must enter the big factory as soon as possible
- B站刘二大人-线性回归及梯度下降
- 【torch】|torch.nn.utils.clip_grad_norm_
猜你喜欢
Unity Vector3. Use and calculation principle of reflect
B站刘二大人-数据集及数据加载 Lecture 8
04. Project blog log
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
PDK工艺库安装-CSMC
Promise summary
In 2022, we must enter the big factory as soon as possible
Pointer classic written test questions
What preparations should be made for website server migration?
巨杉数据库再次亮相金交会,共建数字经济新时代
随机推荐
Game push: image / table /cv/nlp, multi-threaded start!
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
Promotion hung up! The leader said it wasn't my poor skills
How to download GB files from Google cloud hard disk
UCF (summer team competition II)
【SQL server速成之路】——身份驗證及建立和管理用戶賬戶
【华为机试真题详解】统计射击比赛成绩
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Fluent implements a loadingbutton with loading animation
Check the useful photo lossless magnification software on Apple computer
[email protected]树莓派
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
[SQL Server fast track] - authentication and establishment and management of user accounts
Vulhub vulnerability recurrence 67_ Supervisor
SQLite queries the maximum value and returns the whole row of data
【SQL server速成之路】——身份验证及建立和管理用户账户
【华为机试真题详解】检查是否存在满足条件的数字组合
指针经典笔试题
Notes, continuation, escape and other symbols
2022 half year summary