日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

mysql使用数据库_MySQL数据库的常用操作

發(fā)布時間:2025/3/20 63 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql使用数据库_MySQL数据库的常用操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

學(xué)習(xí)python少不了和數(shù)據(jù)庫打交道,常見的數(shù)據(jù)庫有:MySQL、SQLite、MongoDB、Redis等,這里主要介紹一下MySQL數(shù)據(jù)庫的基本操作。

數(shù)據(jù)庫聽起來感覺好高大上,當(dāng)你接觸之后有種恍然大悟的感覺,不就是把我們的數(shù)據(jù)存到一個表格中嗎。可以簡單的這么理解,大家都知道表格都是由表名、表頭、數(shù)據(jù)等幾部分組成的,數(shù)據(jù)庫跟這類似,只不過叫法不一樣,這里叫數(shù)據(jù)庫名、表名和字段。數(shù)據(jù)庫就簡單介紹這么多,說的不一定完全正確,下面就說一下怎么操作數(shù)據(jù)庫。

一、MySQL字段類型

常用的有:數(shù)值(int,?float)、字符串(varchar, text)、日期(date, datetime)

二、MySQL約束

主鍵:primary?key

外鍵:foreign?key

默認(rèn)值:default

唯一:unique

非空:not null

三、物理刪除和邏輯刪除

在使用數(shù)據(jù)庫的時候,一定會遇到刪除數(shù)據(jù)庫中的內(nèi)容,這個時候一定要小心操作,不然就會被老板直接開除,網(wǎng)絡(luò)上不是有個很火的命令操作叫"程序員如何執(zhí)行一條命令讓老板開除",所以操作數(shù)據(jù)庫時一定要小心謹(jǐn)慎。

說一下什么是物理刪除和邏輯刪除,物理刪除就是把一條數(shù)據(jù)從數(shù)據(jù)庫中永遠(yuǎn)刪除,就是再也找不到也不能恢復(fù);邏輯刪除就是給你設(shè)計的表再增加一列(比如:isDelete,設(shè)默認(rèn)值為0),當(dāng)你想刪除數(shù)據(jù)時將這個字段的值設(shè)為1,當(dāng)你再操作數(shù)據(jù)時把isDelete值為1的篩選掉就可以了,這樣數(shù)據(jù)也不會丟失。對于重要的數(shù)據(jù)一定要設(shè)置這個字段,對于不重要的數(shù)據(jù)自己想怎么操作就怎么操作,大不了全部刪除再重新建數(shù)據(jù)庫。

四、數(shù)據(jù)庫基本操作(增刪改查)

1、創(chuàng)建數(shù)據(jù)庫

create database [if not exists] db_name [character set utf8];

注意:[if?not?exists]:最好加上,這樣可以避免麻煩,每操作一次數(shù)據(jù)庫就多一次風(fēng)險

[character?set utf8]就是設(shè)置編碼格式,也可以不指定

2、查看數(shù)據(jù)

show databases;

3、查看數(shù)據(jù)庫創(chuàng)建方式

show create database db_name;

4、修改數(shù)據(jù)庫

alter database db_name [character set xxx];

5、刪除數(shù)據(jù)庫

drop database [if exists] db_name;

注意:?[if?exists]:如果你不確定數(shù)據(jù)庫是否存在最好加上,存在就刪除,不存在也不會報錯

6、使用數(shù)據(jù)庫

use db_name;

7、查看當(dāng)前使用數(shù)據(jù)庫

select database();

五、數(shù)據(jù)庫管理

1、通過grant命令,給test數(shù)據(jù)庫,添加用戶***,密碼123456

grant select,insert,update,delete,create,drop on?test?to '***'@'localhost' identified by '123456';

2、修改用戶密碼

grant select,insert,update,delete,create,drop on?test?to '***'@'localhost' identified?by 'asdfasdf';

3、顯示用戶

select user from mysql.user;

4、刪除用戶

drop user '***'@'localhost';

六、數(shù)據(jù)表的基本操作(增刪改查)

1、查看數(shù)據(jù)表

show tables;

2、創(chuàng)建數(shù)據(jù)表

create table?student(

id int primary key auto_increment,

name varchar(20) not null,

age int not null,

gender?tinyint(1) default 1,

birthday date,

hobby?varchar(20)

);

3、查看數(shù)據(jù)表結(jié)構(gòu)

desc student;

4、查看創(chuàng)建數(shù)據(jù)表語句

show create table student;

5、增加數(shù)據(jù)(列,字段)

alter table?student?add address varchar(30);

6、增加多個數(shù)據(jù)

alter table?student?add address varchar(30),

add age int not null,

add height int not null;

7、修改一列列名

alter table?student change address addr varchar(20);

8、修改一列類型

alter table?student modify age tinyint default 20;

9、刪除一列

alter table student drop height;

10、修改表名

rename table student to stu;

11、修改表所用的字符集

alter table student character set utf8;

12、刪除表

drop table student;

create table users(id int not null,

name varchar(10),

age int,

height int

);

13、添加主鍵

alter table users add primary key (id);

14、刪除主鍵

alter table users drop primary key;

15、添加唯一索引

alter table users add unique (name);

16、添加唯一索引設(shè)置索引名

alter table users add unique key user_name(name);

17、添加聯(lián)合索引

alter table users add unique index name_age(name,age);

18、刪除索引

alter table users drop index name;

create table student( id int primary key auto_increment,

name varchar(20),

birthday varchar(20),

age int

);

19、插入一條數(shù)據(jù)

insert into student(name,birthday,age) values('學(xué)生1','2001-1-1',11);

20、插入多條數(shù)據(jù)

insert into student(name,birthday,age) values('學(xué)生2','2001-1-2',11),

('學(xué)生3','2001-1-3',11),

('學(xué)生4','2001-1-4',11);

21、修改數(shù)據(jù)

update student set birthday='2001-1-10' where id=1;

22、數(shù)據(jù)表中的數(shù)據(jù)可以直接運算

update student set age = age + 5;

23、刪除數(shù)據(jù)

delete from student where id = 1;

注意:刪除數(shù)據(jù)時一定要加條件限制,不然整張數(shù)據(jù)表都被刪除了

24、刪除表中的所有數(shù)據(jù)

delete from student;

七、查詢數(shù)據(jù)

create table grade( id int primary key auto_increment,

name varchar(20),

js double,

java double,

python double );

insert into grade (name,js,java,python) values ('Tom',68,89,87),('Jim',70,91,92),('Jake',71,73,74),('Mike',80,84,85),('Jame',85,88,83);

1、select查詢

查詢所有數(shù)據(jù):?select * from grade;

查詢姓名和js成績:?select name,js from grade;

過濾表中重復(fù)數(shù)據(jù):?select distinct js from grade;

給所有js成績+5,并使用as別名:?select name,js+5?as 'js成績' from grade;

2、where過濾查詢

查詢姓名XX的學(xué)生信息:?select * from grade where name='Tom';

查詢總分大于250分的所有學(xué)生:?select name,js+java+python as '總成績' from grade where js+java+python>250;

查詢js分?jǐn)?shù)和java分?jǐn)?shù)大于90的學(xué)生:?select * from grade where js>90 and java>90;

查詢java分?jǐn)?shù)80~90的學(xué)生:?select * from grade where java between 80 and 90;

查詢java分?jǐn)?shù)在80或90的學(xué)生:?select * from grade where java in (80,90);

/* 模糊查詢,%表示多個字符,_表示一個字符 */

查詢姓名j開頭的學(xué)生:?select * from grade where name like 'j%';

八、order by?排序

/* asc:升序,默認(rèn)值 desc:降序 */

根據(jù)js分?jǐn)?shù)升序排序:?select * from grade order by js;

更加總分?jǐn)?shù)從高到低排序:select name,(ifnull(js,0)+ifnull(java,0)+ifnull(python,0)) as '總分?jǐn)?shù)' from grade order by '總分?jǐn)?shù)' desc;

九、group by分組查詢

create table product_tab( id int primary key auto_increment,

product_name varchar(20),

price float(6,2),

product_date date,

class varchar(20) );

insert into product_tab (product_name,price,product_date,class) values ('蘋果',10,'20180812','水果'),

('香蕉',20,'20180826','水果'),

("水壺",120,'20170612',"電器"),

("被罩",70,'20170612',"家具"),

("音響",420,'20171012',"電器"),

("電視",2000,'20170912',"電器"),

("床單",55,'20171112',"家具"),

("草莓",34,'20170512',"水果");

按位置分組:?select * from product_tab group by 5;

按產(chǎn)品類別分類并顯示平均價格:?select class,avg(price) from product_tab group by class;

按產(chǎn)品分類顯示每一種商品價格總和超過200的商品:?select class,sum(price) from product_tab group by class having sum(price)>200;

分組顯示所有產(chǎn)品:group_concat select id,group_concat(product_name) from product_tab group by class;

十、where和having之間的區(qū)別

首先,明確一點就是能使用where的地方都能使用having;where只能用于分組之前數(shù)據(jù)的篩選;having只能用于分組之后數(shù)據(jù)的篩選,

而且having中可以使用聚合函數(shù)。

十一、聚合函數(shù)

COUNT(列名):統(tǒng)計行的個數(shù)

統(tǒng)計學(xué)生個數(shù):?select count(id) from grade;

SUM(列名):統(tǒng)計總量

統(tǒng)計所有js總成績:?select sum(js) as 'js總成績' from grade;

AVG(列名):平均數(shù)

統(tǒng)計所有js平均分:?select avg(js) as 'js平均分' from grade;

MAX,MIN (最高,最低)

select max(js) as 'js最高分' from grade;

select min(js) as 'js最低分' from grade;

十二、SQL語句執(zhí)行順序

from----where----select----group by----having----order by

十三、limit和正則表達(dá)式

limit

查詢前3條數(shù)據(jù):?select * from grade limit 3;

跳過1條,查詢3條數(shù)據(jù):?select * from grade limit 1,3;

正則表達(dá)式

查詢j開頭的學(xué)生:?select * from grade where name regexp '^j';

查詢名字中m出現(xiàn)2次的學(xué)生:?select * from grade where name regexp 'm{2}';

十四、多表操作

1、外鍵約束

/* 一個班級對應(yīng)多個學(xué)生,一個學(xué)生只能對應(yīng)一個班級 */

主表:創(chuàng)建班級表 create table class( id int primary key auto_increment, name varchar(20), stu_nums int );

子表:創(chuàng)建學(xué)生表 create table student( id int primary key auto_increment, name varchar(20), class_id int, foreign key(class_id) references class(id) );

注意:作為外鍵一定要和關(guān)聯(lián)主鍵的數(shù)據(jù)類型保持一致

插入數(shù)據(jù):?insert into class (name,stu_nums) values ('班級一',10),('班級二',12),('班級三',13),('班級四',14),('班級五',15);

insert into student (name,class_id) values ('Tom',1),('Jim',1),('Jake',2),('Mike',3),('Jane',4);

增加外鍵:?alter table student add constraint student_fk_class foreign key(class_id) references class(id);

刪除外鍵:?alter table student drop foreign key student_ibfk_1;

2、INNODB支持的on語句

外鍵約束對子表的含義:如果在父表中找不到候選鍵,則不允許在子表進(jìn)行insert/update

外鍵約束對父表的含義:在父表上進(jìn)行update/delete以更新或刪除在子表中的一條或多條對應(yīng)數(shù)據(jù),

父表的行為取決于:在定義子表的外鍵指定的on update/on delete語句

ON DELETE CASCADE 級聯(lián)刪除:父表記錄被刪除,子表對應(yīng)的記錄自動被刪除

foreign key(class_id) references class(id) on delete cascade;

ON DELETE SET NULL 置空:父表update/delete記錄時,子表設(shè)為null

foreign key(class_id) references class(id) on set null;

RESTRICT:拒絕對父表進(jìn)行刪除操作

NO ACTION:在mysql中同RESTRICT,如果子表中有匹配的記錄,則不允許對父表對應(yīng)候選鍵

3、多表查詢

/* 創(chuàng)建2張表:員工表和部門表 并插入相關(guān)數(shù)據(jù) */

create table employee( emp_id int primary key auto_increment, emp_name varchar(20), age int, dept_id int );

create table department( dept_id int primary key auto_increment, dept_name varchar(100) );

insert into employee(emp_name,age,dept_id) values ('A',19,200), ('B',26,201), ('C',30,201), ('D',24,202), ('E',20,200), ('F',38,204);

insert into department values (200,'人事部'), (201,'技術(shù)部'), (202,'銷售部'), (203,'財政部');

笛卡爾積查詢:查詢結(jié)果是m*n

select * from employee,department;

內(nèi)連接:查詢兩張表中都有的關(guān)聯(lián)數(shù)據(jù)

select * from employee,department where employee.dept_id=department.dept_ id;

外連接:

左外連接:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒有的結(jié)果

select * from employee left join department on employee.dept_id=departmen t.dept_id;

右外連接:在內(nèi)連接的基礎(chǔ)上增加右邊有左邊沒有的結(jié)果

select * from employee right join department on employee.dept_id=departme nt.dept_id;

4、多條件查詢

查詢員工大于25歲的信息:

select employee.emp_name,department.dept_name

-> from employee,department

-> where employee.dept_id=department.dept_id and age>25;

以內(nèi)連接方式查詢employee和department表,并以age字段升序顯示:

select employee.emp_id,employee.emp_name,employee.age,department.dept_name

-> from employee,department

-> where employee.dept_id=department.dept_id

-> order by age asc;

子查詢:查詢employee表,dept_id在department表中的所有信息

select * from employee where dept_id in (select dept_id from department);

子查詢:

select * from employee ?where dept_id in (select dept_id from department where age>25);

使用EXISTS關(guān)鍵字

EXISTS關(guān)字鍵字表示存在。在使用EXISTS關(guān)鍵字時,內(nèi)層查詢語句不返回查詢的記錄。

而是返回一個真假值。Ture或False

當(dāng)返回Ture時,外層查詢語句將進(jìn)行查詢;當(dāng)返回值為False時,外層查詢語句不進(jìn)行查詢

select * from employee?where?EXISTS (SELECT dept_name from department where dept_id=205);

select * from employee?where?EXISTS (SELECT dept_name from department where dept_id=203);

總結(jié)

以上是生活随笔為你收集整理的mysql使用数据库_MySQL数据库的常用操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。