Mysql基本命令
發(fā)布作者:微思網(wǎng)絡(luò) 發(fā)布時間:2023-07-03 瀏覽量:0次
查看數(shù)據(jù)庫
show databases;
創(chuàng)建數(shù)據(jù)庫
create database db1; 創(chuàng)建數(shù)據(jù)庫db1
刪除數(shù)據(jù)庫
drop database db1; 刪除數(shù)據(jù)庫db1
切換進(jìn)入數(shù)據(jù)庫
use user 進(jìn)入user數(shù)據(jù)庫
查看數(shù)據(jù)庫中的表
show tables;
創(chuàng)建表
創(chuàng)建表的方法
(1) 直接創(chuàng)建
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是指表類型,也即在表創(chuàng)建時指明其使用的存儲引擎
同一庫中不同表可以使用不同的存儲引擎
同一個庫中表建議要使用同一種存儲引擎類型
范例:創(chuàng)建表
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初始值
查看對應(yīng)表結(jié)構(gòu)
DESC student;
insert添加表數(shù)據(jù)
insert student (id,name,age,gender)values(10,'xiaoming',20,'M');
??id是AUTO_INCREMENT 可以不添加,也可以添加
數(shù)字不用''
字符需要''
查看表內(nèi)數(shù)據(jù)
select * from student; 查看所有字段,在student表中
select 字段 from 表名;
復(fù)制另外一個表的結(jié)構(gòu)
create table emp like student; 創(chuàng)建一個表emp,表結(jié)構(gòu)復(fù)制student表
update修改表數(shù)據(jù)
update emp set age=18 where id=1; 更新emp表id=1的age為18
update 數(shù)據(jù)庫 set 修改內(nèi)容 where 過濾條件
delete刪除表數(shù)據(jù)
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;
查詢classid,gender,avg(age) 從students表 分組classid,gender 根據(jù)classid排序