您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)mysql基本操作有那些的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
文章目錄
structured Query Language:結(jié)構(gòu)化查詢(xún)語(yǔ)言
1) DDL(Data Definition Language)數(shù)據(jù)定義語(yǔ)言
用來(lái)定義數(shù)據(jù)庫(kù)對(duì)象:數(shù)據(jù)庫(kù),表,列等。關(guān)鍵字:create, drop,alter 等
2) DML(Data Manipulation Language)數(shù)據(jù)操作語(yǔ)言
用來(lái)對(duì)數(shù)據(jù)庫(kù)中表的數(shù)據(jù)進(jìn)行增刪改。關(guān)鍵字:insert, delete, update 等
3) DQL(Data Query Language)數(shù)據(jù)查詢(xún)語(yǔ)言
用來(lái)查詢(xún)數(shù)據(jù)庫(kù)中表的記錄(數(shù)據(jù))。關(guān)鍵字:select, where 等
4) DCL(Data Control Language)數(shù)據(jù)控制語(yǔ)言(了解)
用來(lái)定義數(shù)據(jù)庫(kù)的訪問(wèn)權(quán)限和安全級(jí)別,及創(chuàng)建用戶。關(guān)鍵字:GRANT, REVOKE 等
#Createcreate database hzyc;create database if not exists hzyc98 character set gbk;#Retrieveshow databases;show create database hzyc98;#Updatealter database hzyc98 character set gbk;#Deletedrop database hzyc98;drop database if exists hzyc98; #查看當(dāng)前使用的數(shù)據(jù)庫(kù)select database();show tables;use hzyc98
表名/表頭為:zoomlist
#查show tables; -- show tables_in_hzyc98desc zoomlist;#增create table zoomlist ( Name varchar(30), Age int, ID int, Height double(5,1))#刪drop table if exists zoomlist;#改alter table zoomlist rename to newzoomlist;alter table zoomlist character set gbk;alter table zoomlist add Name varchar(20);#加列alter table zoomlist change Age newAge int;alter table zoomlist modify Age char(8);alter table zoomlist drop Name;/*設(shè)置類(lèi)型:*/ - int、double(5,1)、varchar(20) - date #yyyy-MM-dd - datetime #yyyy-MM-dd HH:mm:ss - timestamp#時(shí)間戳 yyyy-MM-dd HH:mm:ss
#除了數(shù)字,其他都需要引號(hào)來(lái)賦值insert into zoomlist (Name, Age, ID, Height) value('美洲豹',5,'20201207',3.2);insert into zoomlist ('美洲豹',5,'20201207',3.2);#刪除delete from zoomlist where [條件];delete from zoomlist;TRUNCATE TABLE zoomlist;#修改update zoomlist set Name = '大笨象' Age = 12 where address = '深圳';update zoomlist set address = '深圳';
#查詢(xún)#盡量不要用 * 先desc一下表里面有啥,然后在決定展示什么東西。SELECT * FROM zoomlist; SELECT Name,Age FROM zoomlist; --只顯示某個(gè)列,方便查看!SELECT DISTINCT Name FROM zoomlist; --去除結(jié)果中[完全重復(fù)]的SELECT Name,score1,score2,scroe1+scroe2 FROM zoomlist;--as:自定義名字展示,也可以不寫(xiě)asSELECT Name,scroe1+IFNULL(scroe2,0) 總分 FROM zoomlist; --ifnull遇到?jīng)]有值的直接給賦值為0SELECT Name,score1,score2,scroe1+IFNULL(scroe2,0) AS 總分 --顯示表頭FROM zoomlist,peoplelist; --從zoomlist、peoplelist里面獲取
* > 、< 、<= 、>= 、= 、!=、<>--不等號(hào)* and、or、not --關(guān)鍵字比&&、||、!好用推薦* BETWEEN...AND --范圍內(nèi)都符合就行* IN( 集合) --特定值的范圍* LIKE:模糊查詢(xún)(1)_:單個(gè)任意字符;(2)%:多個(gè)任意字符* IS NULL例子:select Name, Age from Student where age between 12 and 20;select Name, Age from Student where age in (12,14,16,18);select Name, Age from Student where name like '%牛%'; --查名字里面包含了牛的學(xué)生select Name, Age from Student where name is not null; -- 查詢(xún)學(xué)生:名字空的不查
select * from employee order by age;select * from employee order by age asc; --升序select * from employee order by age desc; --降序select * from employee order by age desc height desc; --第一個(gè)一樣的時(shí)候,才會(huì)用第二個(gè)方法排序(age降序,身高降序)
排除了null數(shù)據(jù),并且有null的數(shù)據(jù)就不參與計(jì)算,不會(huì)報(bào)錯(cuò)!
select count(*) from student;select count(ifnull(age,20)) from student; select count(age) from student;--如果沒(méi)有就不記錄select count(id) from student; --我們一般選用主鍵來(lái)統(tǒng)計(jì)個(gè)數(shù)select max(age) from student;select min(age) from student;select sum(age) from student;select avg(age) from student;
group by 之后就是兩個(gè)不同的組別了,他們不能再去查看一個(gè)獨(dú)立的個(gè)體了。
select sex,count(name) from employee group by sex having count(name)<6;select sex,count(name) from employee where name = '張四' group by sex ;
limit是一個(gè)MySQL的方言,用于分頁(yè)
SELECT * FROM student LIMIT 0,5; -- 第1頁(yè),從0索引開(kāi)始,讀5個(gè)數(shù)據(jù)SELECT * FROM student LIMIT 7,10; -- 第2頁(yè),從7索引開(kāi)始(第8個(gè)數(shù)據(jù)),讀10個(gè)數(shù)據(jù)
-- 建表時(shí)添加非空約束: create table employee( name char(30), sex char(8) not null ) alter table employee modify sex char(8) not null; --添加非空約束 alter table employee modify sex char(8); --破除非空約束
只可以有一個(gè)null值,不能再多了;
刪除約束只可以用drop index來(lái)刪除unique約束
-- 建表時(shí)添加唯一約束: create table employee( name char(30), sex char(8), score int unique --分?jǐn)?shù)要唯一 ) --添加唯一約束alter table employee modify name char(8) unique; --破除唯一約束-- alter table employee modify sex char(8); 不可用--破除name身上的unique約束用drop index除去索引alter table employee drop index name;
一個(gè)表只有一個(gè)primary key,非空且唯一
做記錄的唯一標(biāo)識(shí),相當(dāng)于index
-- 建表時(shí)添加主鍵約束: create table employee( id int primary key, --給id加上主鍵約束 name char(30), ) --添加唯一約束alter table employee modify id int primary key; --破除唯一約束-- alter table employee modify id int; 不可用!--破除id身上的primary key約束只能用drop primary keyalter table employee drop primary key;
只對(duì)數(shù)值有用,而且一般可以放給主鍵做自動(dòng)增長(zhǎng)
-- 建表時(shí)添加auto_increment: create table employee( id int auto_increment, --給id加上auto_increment name char(30), ) --添加auto_increment,自動(dòng)從1開(kāi)始alter table employee modify id int auto_increment;--設(shè)置初值alter table employee auto_increment = 100; --破除auto_incrementalter table employee modify id int;
感謝各位的閱讀!關(guān)于mysql基本操作有那些就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。