事务
原子性 Atomicity
BUSINESS sql 语句1 sql 语句2 COMMIT
原子性:事务操作要么同时发生,要么同时失败,不存在中间情况
通过Undo Log回滚实现
一致性 Consistency
账户500元 -> 扣除1000元 -> 账户-500元 -- 非法操作
一致性:每个操作都必须是合法的,账户信息应该从一个有效状态到另一个有效状态。
隔离性 Isolation
商户1转账500元 -> 余额更新为500元 商户2转账500元 -> 余额更新为500元 -- 没有隔离性
隔离性:两个操作对同一个账户并发操作时,应该表现为不相互影响类似串行的操作。
持久性 Durability
转账500元到余额 --服务器宕机--> 余额0元
持久性:操作更新成功后,更新的结果应该永久地保留下来,不会因为宕机等问题而丢失。
使用
创建用户
-- 创建用户 CREATE USER 'devuser'@'192.168.1.%' IDENTIFIED BY 'Secure@123'; -- 授予权限 GRANT ALL PRIVILEGES ON appdb.* TO 'devuser'@'192.168.1.%'; -- 刷新权限 FLUSH PRIVILEGES; -- 撤销 REVOKE ALL PRIVILEGES ON mydb.* FROM 'newuser'@'%';
启动数据库
net start <mysql-service_name>
登录数据库
mysql -u <username> -p $ <password>
导入数据库
mysql> SET NAMES 'utf8mb4' ; mysql> SET character_set_server = 'utf8mb4' ; mysql> SOURCE / path/ to / database.sql;
数据库
CREATE DATABASE yourDatabase CHARACTER SET utf8mb4; show databases;use yourDatabase
表
CREATE TABLE yourTable ( id INT AUTO_INCREMENT PRIMARY KEY, name varchar (50 ) NOT NULL DEFAULT 'worker' , age INT , addr varchar (50 ) ); drop < table > ;show tables;alter table < table > add < col> char (20 )drop column < col> ;drop table < table > ;
查询
select * from < table > \Gselect < column1> , < column2> , < column3> from < table > ;select distinct < column > from < table > ;select < col> from < table > limit 5 ;
排序
ORDER BY
需要放在 WHERE
之后
select < col> from < table > order by < col> (asc ); select < col> from < table > order by < col1> desc , < col2> desc ;
筛选
select < col> from < table > where < col> = < value > ;select < col> from < table > where < col> between 0 and 10 ;select < col> from < table > where < col> is null ;
逻辑操作符
优先级: AND
> OR
select < col> from < table > where < cond1> or (< cond2> and < cond3> ); select < col> from < table > where < col> not in (< value1> , < value2> );
插入
insert into < table > (< col1> , < col2> ) values (< val1> , < val2> ); insert into yourTable (name, email, age) values ('eric' , 'example@email.com' , 19 );
更新
update < table > set < col1> = < val1> , < col2> = < val2> where < col3> = < val3> ;
删除
delete from < table > where < col> = < val> ;
联结
设计数据库时,应遵循将数据分解到不同的数据表这一原则。
然而,在使用数据时,常常需要将多个表的数据一起检索出来。
这时就需要用到联结。
select < col1> , < col2> from < table1> , < table2> where < table1> .< col> = < table2> .< col> ;select < col1> , < col2> from < table1> inner join < table2> on < table1> .< col> = < table2> .< col> ;
安装
压缩包安装
初始化
mysqld --initialize --console > mysql_info
配置mysql.ini
[mysqld] port =3306 basedir =C:\\your\\path\\MySQLdatadir =C:\\your\\path\\MySQL\\Datamax_connections =200 max_connect_errors =10 character-set-server =utf8default-storage-engine =INNODBdefault_authentication_plugin =mysql_native_password[mysql] default-character-set =utf8[client] port =3306 default-character-set =utf8
安装服务
mysqld --install MySQL8 --defaults-file="C:\your\path\MySQL\mysql.ini" mysqld install
修改密码
mysqladmin -u root -p password (YourPassword) --port 3306