日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 第二天数据_MySQL入门第二天------数据库操作

發(fā)布時間:2024/8/23 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 第二天数据_MySQL入门第二天------数据库操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、基本命令

1、啟動服務(wù)器

cmd

net start [服務(wù)器名稱]

net start mysql57

2、停止服務(wù)器

cmd

net stop [服務(wù)器名稱]

net stop mysql57

3、鏈接數(shù)據(jù)庫

mysql -u 用戶名 -p 登錄密碼

mysql -u root -p

4、退出登錄

quit

exit

\q

5、查看版本(連接后執(zhí)行)

select version();

6、查看當(dāng)前時間(連接后執(zhí)行)

select now();

7、遠(yuǎn)程鏈接

mysql -h ip地址 -u 用戶名 -p密碼 -P服務(wù)器端口號(默認(rèn)3306)

二、數(shù)據(jù)庫操作

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

格式: create database 【數(shù)據(jù)庫名】 charset=utf8;

示例:create database sunck charset=utf8;

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

格式: drop database 【數(shù)據(jù)庫名】;

示例:drop database sunck;

3、切換數(shù)據(jù)庫

格式:use [數(shù)據(jù)庫名]

示例:use sunck;

4、查看當(dāng)前選擇的數(shù)據(jù)庫

select database();

三、表操作(切換到當(dāng)前數(shù)據(jù)庫后使用)

1、查看當(dāng)前數(shù)據(jù)庫中所有表

show tables;

2、創(chuàng)建表

格式:create table 表名(列及類型)

說明:auto_increment 表明自增長 primary key 表示主鍵

not null 表示不為空

示例:create table student(

id int auto_increment primary key,

name varchar(20) not null,

age int not null,

gender bit default 1,

address varchar(20),

isDelete bit default 0

);

3、刪除表

格式:drop table 【表名】;

示例:drop table student;

4、查看表結(jié)構(gòu)

格式:desc 【表名】;

示例:desc student;

5、查看建表語句

格式:show create table 【表名】;

示例:show create table student;

6、重命名表名

格式:rename table 元表名 to 新表名

示例:rename table car to newCar;

7、修改表

格式:alter table [表名] add|change|drop 列名 類型;

示例:alter table newCar add isDelete bit default 0;

四、數(shù)據(jù)操作

1、增

a、全列插入:

格式:insert into [表名] values(...)

說明:主鍵列是自動增長的,但是在全列插入時需要占位,通常使用0,

插入數(shù)據(jù)以后以實際數(shù)據(jù)為準(zhǔn)

示例:insert into student values(0,"韓梅梅",15,0,"北京",0);

b、缺省插入:

格式:insert into tablename(column1,column2,.....) values(value1,value2,.....);

示例:insert into student(name,age,address) values ("lilei",19,"上海");

c、同時插入多條數(shù)據(jù):

格式:insert into tablename values values(...),(...),...;

示例:insert into student values (0,"穆桂英",28,0,"河南",0),(0,"楊帥",20,1,"河北",0),(0,"劉秀",35,1,"石家莊",0);

2、刪

格式:delete from tablename;全部刪除,刪除整個表的數(shù)據(jù)

delete from tablename where 條件;

示例:delete from student where id=7;

注意:沒有條件是全部刪除,慎用。

3、改

格式:update tablename set 列1=值1,列2=值2,....

update tablename set 列1=值1,列2=值2,.... where 條件

示例:update student set age =16 where id=4;

注意:沒有條件是全部修改,慎用。

4、查

格式:select * from [tablename];

說明:查詢表中的全部數(shù)據(jù)

示例:select * from student;

五、查

1、基本語法

格式:select * from [tablename];

說明:查詢表中的全部數(shù)據(jù) a、from 關(guān)鍵字后面是表名,表示數(shù)據(jù)來源于這張表

b、select 后面寫表中的列名,如果是* 表示結(jié)果中集中顯示表中的所有列

c、在select后面的列名部分,可以使用as為列名起別名,這個別名顯示在結(jié)果集中

d、如果要查詢多個列,之間用逗號分隔

示例:select * from student;

select name,age from student;

select name as "姓名",age from student;

2、消除重復(fù)行

在selecthoumian 列前前面使用 distinct 可以消除重復(fù)的行

示例:select distinct name,age from student;

3、條件查詢

a、語法

select * from 表名 where 條件

b、比較運算符

操作符描述實例

=等號,檢測兩個值是否相等,如果相等返回true(A = B) 返回false。

<>, !=不等于,檢測兩個值是否相等,如果不相等返回true(A != B) 返回 true。

>大于號,檢測左邊的值是否大于右邊的值, 如果左邊的值大于右邊的值返回true(A > B) 返回false。

>=大于等于號,檢測左邊的值是否大于或等于右邊的值, 如果左邊的值大于或等于右邊的值返回true(A >= B) 返回false。

<=小于等于號,檢測左邊的值是否小于于或等于右邊的值, 如果左邊的值小于或等于右邊的值返回true(A <= B) 返回 true。

c、邏輯運算符

and

or

not

d、模糊查詢

like

% 表示任意多個任意字符

_ 表示一個任意字符

select * from student where name like "劉%";

select * from student where name like "劉_";

e、范圍查詢

in 表示在一個非連續(xù)性的范圍內(nèi)

between... and ... 表示在一個連續(xù)范圍內(nèi)

需求:查詢編號為8,10,12的學(xué)生

示例:select * from student where id in (8,10,12);

需求:查詢編號為6-8的學(xué)生

示例:select * from student where id between 6 and 8;

f、空判斷

insert into student(name,age) values ("daojun",69);

注意:null 與"" 是不同

判斷空: is null

判斷非空: is not null

需求:查詢沒有地址的同學(xué)

示例: select * from student where address is null;

g、優(yōu)先級

小括號, not 比較運算符,邏輯運算符

and 比or 優(yōu)先級高。

4、聚合

為了快速得到統(tǒng)計數(shù)據(jù),提供五個聚合函數(shù)

a、 count(*) 表示計算總行數(shù),括號中可以寫* 和列名

b、 max(列) 表示求此列的最大值

c、 min(列) 表示求此列的最小值

d、 sum(列) 表示求此列的和

e、 avg(列) 表示求此列的平均值

需求:查詢學(xué)生總數(shù)

示例: select count(*) from student;

需求:查詢女生的編號最大值

示例: select max(id) from student where gender=0;

需求:查詢女生的編號最小值

示例: select min(id) from student where gender=0;

需求:查詢女生的年齡和

示例: select sum(age) from student where gender=0;

需求:查詢學(xué)生年齡平均值

示例: select avg(age) from student;

5、分組

按照字段分組,表示此字段相同的數(shù)據(jù)會被放到一個集合中。分組后,只能查詢出相同的數(shù)據(jù)列。

對于有差異的數(shù)據(jù)列,無法顯示在結(jié)果集中

可以對分組后的數(shù)據(jù)進(jìn)行統(tǒng)計,做聚合運算。

語法:select 列1,列2,聚合... from 表名 group by 列1,列2,列3,.... having 列1,列2,.....,聚合...

需求:查詢女生和男生總數(shù)

示例:select gender,count(*) from student group by gender;

select name,gender,count(*) from student group by gender,age;

需求:分組后的數(shù)據(jù)篩選

示例: select gender,count(*) from student group by gender having gender;

select gender,count(*) from student group by gender having gender;

where 與 having的區(qū)別

where 對from 后面的制定的表進(jìn)行篩選,屬于對原始數(shù)據(jù)的篩選

having 是對結(jié)果集 group by 的結(jié)果進(jìn)行篩選

6、排序

語法: select * from 表名 order by 列1

asc|desc, 列2 asc|desc,....

說明:

a、將數(shù)據(jù)按照列1進(jìn)行排序,如果某些列1的值相同,則按照列2進(jìn)行排序

b、默認(rèn)按照從小到大的順序排序

c、asc升序

d、desc降序

需求:按照年齡排序

示例: select * from student order by age;

需求:按照沒有刪除的數(shù)據(jù)的年齡排序

示例: select * from student where isDelete=0 order by age desc,id desc;

7、分頁

語法:select * from 表名 limit start,count;

說明: start索引從0開始

示例:select * from student limit 0,3;

select * from student limit 3,3;

select * from student where gender=1 limit 0,3;

六、關(guān)聯(lián)

外鍵去關(guān)聯(lián)其他表。

建表語句:

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

create table students(id int auto_increment primary key, name varchar(20) not null,gender bit default 1,classid int not null,foreign key(classid) references class(id));

插入數(shù)據(jù):

insert into class values(0,"python01",45),(0,"python02",20),(0,"python03",30),(0,"python04",34),(0,"python05",28);

insert into students values(0,"tom",1,1);

insert into students values(0,"lilei",1,1);

insert into students values(0,"jack",1,2);

insert into students values(0,"韓梅梅",0,1);

關(guān)聯(lián)查詢

select students.name,class.name from class inner join students on class.id=students.classid;

select students.name,class.name from class left join students on class.id=students.classid;

select students.name,class.name from class right join students on class.id=students.classid;

分類:

1、表A inner join 表B

表示 表A與表B的行會出現(xiàn)在結(jié)果集中

2、表A left join 表B

表A與表B的行會出現(xiàn)在結(jié)果集中,外加表A中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)使用null填充

3、表A reft join 表B

表A與表B的行會出現(xiàn)在結(jié)果集中,外加表B中獨有的數(shù)據(jù),未對應(yīng)的數(shù)據(jù)使用null填充

總結(jié)

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

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