查看資料庫 show databases; 創建資料庫 create database db1; 創建資料庫db1 刪除資料庫 drop database db1; 刪除資料庫db1 切換進入資料庫 use user 進入user資料庫 查看資料庫中的表 show tables; 創建表 創建表的方 ...
查看資料庫
show databases;
創建資料庫
create database db1; 創建資料庫db1
刪除資料庫
drop database db1; 刪除資料庫db1
切換進入資料庫
use user 進入user資料庫
查看資料庫中的表
show tables;
創建表
創建表的方法 (1) 直接創建 CREATE TABLE [IF NOT EXISTS] 'tbl_name' (col1 type1 修飾符, col2 type2 修飾符, ...) #欄位信息 col type1 PRIMARY KEY(col1,...) INDEX(col1, ...) UNIQUE KEY(col1, ...) #表選項: ENGINE [=] engine_name ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT} 註意: Storage Engine是指表類型,也即在表創建時指明其使用的存儲引擎 同一庫中不同表可以使用不同的存儲引擎 同一個庫中表建議要使用同一種存儲引擎類型
範例:創建表
CREATE TABLE student ( id int UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(20) NOT NULL, age tinyint UNSIGNED, gender ENUM('M','F') default 'M' )ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; #id欄位以10初始值
查看對應表結構
DESC student;
insert添加表數據
insert student (id,name,age,gender)values(10,'xiaoming',20,'M'); ⭐️id是AUTO_INCREMENT 可以不添加,也可以添加 數字不用'' 字元需要''
查看表內數據
select * from student; 查看所有欄位,在student表中 select 欄位 from 表名;
複製另外一個表的結構
create table emp like student; 創建一個表emp,表結構複製student表
update修改表數據
update emp set age=18 where id=1; 更新emp表id=1的age為18 update 資料庫 set 修改內容 where 過濾條件
delete刪除表數據
delete from emp where name='xiaoming';刪除表emp中name=‘xiaoming’的記錄
查詢:分組 group by
select gender,avg(age) from students group by gender; select 性別欄位,平均(年齡)from 表名 group by 性別欄位 按照性別分組,男女分別的平均年齡 ⭐️分組之後再過濾不能用where 用having
查詢:分組後排序order by
select classid,gender,avg(age) from students group by classid,gender order by classid;TRANSLATE with x English TRANSLATE with EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back
查詢classid,gender,avg(age) 從students表 分組classid,gender 根據classid排序