安裝好數(shù)據(jù)庫以后... ...
創(chuàng)建庫
create database 數(shù)據(jù)庫名字;
//創(chuàng)建名字為XXX的庫 create database XXX;
??下面框?qū)儆跀U展
{{ /~知識拓展:創(chuàng)建庫時指定字符集(utf-8, gb2312)??}}
# utf-8
CREATE DATABASE 數(shù)據(jù)庫名稱 DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
# gbk
CREATE DATABASE 數(shù)據(jù)庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;
進(jìn)入創(chuàng)建好的庫
use XXX;
//use空格用戶名分號,進(jìn)入此庫~。~!
查看當(dāng)前庫名稱 / 信息
第一種: select database();
//簡單明了 只能看在哪個庫
第二種: status;
//詳細(xì)復(fù)雜 格式路徑系統(tǒng)啥都有
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名稱;
// 例如刪除teacher庫 drop database teacher;
創(chuàng)建表
create table 表名(
列名 類型 是否可以為空,
列名 類型 是否可以為空
);
// 舉例
ysql> create table teacher(
-> id int auto_increment primary key,
-> name varchar(10),
-> age int null, //可以為空的
-> phone char(11) not null default '' //不能為空的 ~~那個詞忘了
-> );
Query OK, 0 rows affected (0.02 sec)
是否可空,null表示空,非字符串
not null - 不可空
null - 可空(非主鍵的默認(rèn)值)
自增,如果為某列設(shè)置自增列,插入數(shù)據(jù)時無需設(shè)置此列的值,默認(rèn)將自增(表中只能有一個自增列)
create table tb1(
id int auto_increment primary key, //id一般為自增
age int not null
)
主鍵,一種特殊的唯一索引,不允許有空值,如果主鍵使用單個列,則它的值必須唯一,如果是多列,則其組合必須唯一。
create table tb1(
id int not null auto_increment primary key,
age int null
)
或
create table tb1(
id int not null,
age int not null,
primary key(id,age)
)
表內(nèi)填入內(nèi)容
增加數(shù)據(jù)
mysql> insert into class ( class_name, create_date ) values ( '云5', '2018-05-16' );
mysql> insert into class ( class_name, create_date) values ( '云6', '2018.06.16' );
mysql> insert into class ( class_name, create_date ) values ( '云07', '26' ),('云計算1808','20180816');
insert into teacher (name, class_id) values ('楊哥', 1);
insert into teacher (name, class_id) values ('強哥', 3);
insert into teacher (name, class_id) values ('磊哥', 02);
修改表結(jié)構(gòu)
添加列:
alter table 表名 add 列名 類型
/* 示例:
alter table stadent add gender Enum("男", "女");
alter table stadent add hobby set("girl","car","yacht");
update student set hobby = "girl,car";
*/
刪除列:
alter table 表名 drop column 列名
修改列:
-- 修改類型
alter table 表名 modify column 列名 類型;
--修改列名和類型
alter table 表名 change 原列名 新列名 類型;
添加主鍵:
alter table 表名 add primary key(列名;
alter table students add id int not null auto_increment, add primary key (id);
刪除主鍵:
alter table 表名 drop primary key;
- 刪除主鍵屬性,保留原值和列
alter table 表名 modify 列名 int, drop primary key;
查看數(shù)據(jù)庫編碼:
SHOW CREATE DATABASE db_name;
查看表編碼:
SHOW CREATE TABLE tbl_name;
查看字段編碼:
SHOW FULL COLUMNS FROM tbl_name;
單表查詢
基礎(chǔ)查詢
select * from 表
select * from 表 where id > 2
select id,name,age as gg from 表 where id > 2
高級查詢
a、條件
select * from 表 where id > 1 and name != '王麻子' and age = 18;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,22,33)
select * from 表 where id not in (11,22,33)
select * from 表 where id in (select id from 表)
b、通配符
select * from 表 where name like 'sha%' - sha開頭的所有(多個字符串)
select * from 表 where name like 'shar_' - sha開頭的所有(一個字符)
c、限制
select * from 表 limit 5; - 獲取前 5 行
select * from 表 limit 0,2; - 從第 1 行開始, 取出 2 行, 包含第 1 行
select * from 表 limit 2 offset 0 - 從第 1 行開始, 取出 2 行, 包含第 1 行
d、排序
select * from 表 order by 列 asc - 根據(jù) “列” 從小到大排列
select * from 表 order by 列 desc - 根據(jù) “列” 從大到小排列
select * from 表 order by 列1 desc,列2 asc - 根據(jù) “列1” 從大到小排列,如果相同則按列2從小到大排序
e、分組
select age from 表 group by age
select age,id from 表 group by age,id
select age,id from 表 where id > 10 group by age,id order id desc
select age,id,count(*),sum(age),max(age),min(age) from 表 group by age,id
select age from 表 group by age having max(id) > 10
特別的:group by 必須在where之后,order by之前
f、嵌套查詢
select * from (select name from t1 where age>18 and age < 25 order by id desc limit 2 ) as tt order by id;
常用類型
char / varchar
char (m)
char數(shù)據(jù)類型用于表示固定長度的字符串,可以包含最多達(dá)255個字符。
其中m代表字符串的長度。
PS: 即使數(shù)據(jù)小于m長度,也會占用m長度
varchar(m)
varchars數(shù)據(jù)類型用于變長的字符串,可以包含最多達(dá)255個字符。
其中m代表該數(shù)據(jù)類型所允許保存的字符串的最大長度,
只要長度小于該最大值的字符串都可以被保存在該數(shù)據(jù)類型中。
PS:雖然varchar使用起來較為靈活,但是從整個系統(tǒng)的性能角度來說,
char數(shù)據(jù)類型的處理速度更快,有時甚至可以超出varchar處理速度的50%。
因此,用戶在設(shè)計數(shù)據(jù)庫時應(yīng)當(dāng)綜合考慮各方面的因素,以求達(dá)到最佳的平衡