MySQL常用語句簡介
發表時間:2023-07-20 來源:明輝站整理相關軟件相關文章人氣:
[摘要]本篇文章主要介紹數據庫、數據表、數據類型、字符串以及時間與日期等常用語句,感興趣的小伙伴參考下。數據庫(Database) 數據表 table 列 column 行 row 冗余 主鍵 primar...
本篇文章主要介紹數據庫、數據表、數據類型、字符串以及時間與日期等常用語句,感興趣的小伙伴參考下。
數據庫(Database)
數據表 table
列 column
行 row
冗余
主鍵 primary key
外鍵 foreign key
復合鍵
索引
參照完整性
MySQL 數據類型
三類:數值、日期/時間和字符串(字符)
數值
TINYINT 1 字節(0,255)
SMALLINT 2 字節(0,65 535)
MEDIUMINT 3 字節
INT或INTEGER 4 字節 BIGINT 8 字節
FLOAT 4 字節 DOUBLE 8 字節 DECIMAL
日期時間
DATE 日期值
TIME 時間值或持續時間
YEAR 年份值
DATETIME 混合日期和時間值
TIMESTAMP 時間戳
字符串
CHAR 0-255字節 、VARCHAR 0-65535 字節
BINARY、VARBINARY、BLOB、TEXT、ENUM和SET
事務是必須滿足4個條件(ACID):
Atomicity(原子性)、Consistency(穩定性)、Isolation(隔離性)、Durability(可靠性)
1、事務的原子性:一組事務,要么成功;要么撤回。
2、穩定性 : 有非法數據(外鍵約束之類),事務撤回。
3、隔離性:事務獨立運行。一個事務處理后的結果,影響了其他事務,那么其他事務會撤回。
事務的100%隔離,需要犧牲速度。
4、可靠性:軟、硬件崩潰后,InnoDB數據表驅動會利用日志文件重構修改。
可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit選項 決定什么時候吧事務保存到日志里。
命令如下:
mysql> -uroot -p123456 登陸
mysql> grant all on test.* to 'pengshiyu'@'localhost'
-> identified by '123456'; 創建用戶
mysql> quit 退出
mysql> show databases; 查看數據庫
mysql> create database test; 創建數據庫
mysql> create database test charset utf8; 指定字符集支持中文
mysql> show create database test; 查看數據庫信息
mysql> drop database test; 刪除數據庫
mysql> use test; 進入數據庫
mysql> create table student(
-> id int auto_increment,
-> name char(32) not null,
-> age int not null,
-> register_data date not null,
-> primary key (id)
-> ); 創建表
mysql> show tables; 查看表
mysql> desc student; 查看表結構
mysql> describe student; 查看表結構
mysql> show columns from student; 查看表結構
mysql> insert into student(name, age, register_data)
-> values('tom', 27, '2018-06-25'); 增加記錄
mysql> select * from student; 查詢數據
mysql> select * from student\G 按行輸出
mysql> select * from student limit 3; 限制查詢數量
mysql> select * from student limit 3 offset 5; 丟棄前5條數
mysql> select * from student where id > 3; 條件查詢
mysql> select * from student where register_data like "2018-06%"; 模糊查詢
mysql> update student set name = 'cxx' where id = 10; 修改
mysql> delete from student where id = 10; 刪除
mysql> select * from student order by age; 排序默認ascend
mysql> select * from student order by age desc; 降序descend
mysql> select age,count(*) as num from student group by age; 分組
mysql> select name, sum(age) from student group by name with rollup; 匯總
mysql> select coalesce(name,'sum'), sum(age) from student
-> group by name with rollup; 匯總取別名
mysql> alter table student add sex enum('M','F'); 增加字段
mysql> alter table student drop sex; 刪除字段
mysql> alter table student modify sex enum('M','F') not null; 修改字段類型
mysql> alter table student modify sex
-> enum('M','F') not null default 'M'; 設置默認值
mysql> alter table student change sex gender
-> enum('M','F') not null default 'M'; 修改字段名稱
mysql> create table study_record(
-> id int not null primary key auto_increment,
-> day int not null,
-> stu_id int not null,
-> constraint fk_student_key foreign key (stu_id) references student(id)
-> );命名外鍵約束
創建表
mysql> create table A(a int not null);
mysql> create table B(b int not null);
插入數據
mysql> insert into A(a) values (1);
mysql> insert into A(a) values (2);
mysql> insert into A(a) values (3);
mysql> insert into A(a) values (4);
mysql> insert into B(b) values (3);
mysql> insert into B(b) values (4);
mysql> insert into B(b) values (5);
mysql> insert into B(b) values (6);
mysql> insert into B(b) values (7);
交集 內連接
mysql> select * from A inner join B on A.a = B.b;
mysql> select a.*, b.* from A inner join B on A.a = B.b;
差集
mysql> select * from A left join B on A.a =B.b; 左外連接
mysql> select * from A right join B on A.a =B.b; 右外連接
并集
mysql> select * from a left join b on a.a=b.b union
-> select * from a right join b on a.a = b.b; 全連接
mysql> begin; 開始事務
mysql> rollback; 回滾事務
mysql> commit; 提交事務
mysql> show index from student; 查看索引
mysql> create index name_index on student(name(10)); 創建索引
mysql> drop index name_index on student;刪除索引
相關推薦:
MySQL 語句大全:創建、授權、查詢、修改
30 個優化 MySQL 語句的技巧
PHP中常用的幾個 mysql 語句_PHP教程
以上就是MySQL常用語句簡介的詳細內容,更多請關注php中文網其它相關文章!
學習教程快速掌握從入門到精通的SQL知識。