mysql相關(guān)知識

如何連接數(shù)據(jù)庫:

- command line client:

輸入初始password

然后就可以進(jìn)行連接

- workbanch:

cmd路徑下進(jìn)行連接(bin)

連接數(shù)據(jù)庫的語句

- 連接數(shù)據(jù)庫:(在cmd中)

mysql-uroot-p(密碼)

- 不顯示密碼:mysql-uroot-p

- 退出數(shù)據(jù)庫:exit;quit

- 查看數(shù)據(jù)庫的類型:select version()

- 顯示當(dāng)前時間:select now()

- 查看當(dāng)前使用的數(shù)據(jù)庫:select database()

- 查看所有數(shù)據(jù)庫:show databases()

- ==創(chuàng)建數(shù)據(jù)庫==

```

:create database test_02

```

charset=utf8;

- 查看創(chuàng)建數(shù)據(jù)庫的語句:show create database

- ==使用創(chuàng)建的數(shù)據(jù)庫:==

```

use database XXX

```

- 展示數(shù)據(jù)庫:

```

show database

```

- 刪除數(shù)據(jù)庫:

```

drop database+數(shù)據(jù)庫名

?數(shù)據(jù)表的操作

navicat注冊機可以使用

右鍵創(chuàng)建new database,charset=utf8

- 查看當(dāng)前數(shù)據(jù)庫中的數(shù)據(jù)表:

show tables

- ==創(chuàng)建表:create table== 數(shù)據(jù)表名字(字段,類型,約束)

- int unsinged auto_increasment not? null primary key default

- 創(chuàng)建classes表(id,name):

```

create table classes(id int unsigned primary key not null auto_increment,name varchar(20) not null);

```

- 創(chuàng)建student表:

```

create table students(id int unsigned primary key not null auto_increment ,

name varchar(20) not null,

age int unsigned,

high decimal(5,2),

gender enum('男','女','中性','保密')default '保密',

cls_id int unsigned);

```

- 查看創(chuàng)建的表:show create table students

- ==查看表的結(jié)構(gòu):desc students;==

?- 修改表:alter

==修改表都是用alter==

```

alter table students add pet varchar(20) default 'dog';

```

修改表的字段,最后要加類型及約束(不重名名版)約束寫不寫都行

```

alter table students modify pet varchar(20) default 'cat';

```

修改表字段,重命名版本

```

alter table students change pet chongwu varchar(20);

```

刪除列

```

alter table students drop pet

```

==刪除表和數(shù)據(jù)庫:==

```

drop database XXX;

drop table students

``

對數(shù)據(jù)的操作-增刪改查

增加:==全列插入==

```

insert into classes values(1,"zhangsan");

```

向?qū)W生表插入一個信息

```

insert into students values(1,'list',18,178,'男','001')

```

==學(xué)生表信息,auto_increment:默認(rèn)自增,主鍵部分添加 0, null, default==

- ==枚舉類型:enum('男','女','中性','保密')==

```

insert into students values(null,'zhaoqi',19,168,2,'002')

```

==zhaoqi的性別就是女,因為枚舉類型中女是第二位。==

部分插入

```

insert into students (name) value('老李')

```

多行插入:

```

insert into students values(0,'老劉',45,178,1,'003'),(0,'老張',30,168,2,'002')

```

**修改 update set**

```

update students set gender='中性'

```

按照條件修改

```

update students set gender='女' where

id=2;

```

修改多個值(好多個cls_id為3的)

```

update students set gender='女' where cls_id=003;

```

**查詢:select**(略)

**刪除 delete from**

```

delete * from students where cls_id=1;

```

以上為物理刪除

- ==邏輯刪除:用一個字段表示這條信息是否已經(jīng)不能再使用了。==

- 給students表添加一個is_delete字段bit類型

```

alter table students add is_delete bit default 0;

```

==group_concat==

```

select gender,group_concat(name) as names from students group by gender.

```

==查詢出同種性別中的姓名。==

==with rollup 匯總的作用==

```

select gender, count(1) from students group by gender with rollup;

```

最終除了各個性別的數(shù)量,還有多了一行總數(shù)。

?Mysql 查詢2

- 分頁查詢:limit

```

select * from students limit 0,4;

```

選取id從1-4

```

select * from students limit 1,4

```

選取id從2-4

- 內(nèi)連接

- 左連接

- 子鏈接

從student表中查詢年齡22~26歲的學(xué)生信息

```

select

? id,

? name,

? gender,

? date_format(now(),'%Y')-birth as age,

? department,

? address

from student

where year(now())-birth>=22 and year(now())-birth

```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容