1. 程式人生 > 資料庫 >MySql語法基礎

MySql語法基礎

進入資料庫系統 cd E:\mysql-8.0.18-winx64\bin;

啟動mysql服務 net start mysql

登入 mysql -u root -p

修改密碼 ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';

查詢所有資料庫 mysql> show databases;

建立資料庫 mysql> create database xxxx

檢視資料庫 mysql> show create database xxxx;

刪除資料庫 mysql> drop database xxxx;

修改資料庫 mysql> alter database xxxx defalut character set utf8;

檢視所有表 mysql> show tables;

進入表 mysql> use xxxx;進入指定資料庫

建立表 mysql> create table student(
-> sid int,
-> sname varchar(20),
-> sage int
-> );

查看錶結構 mysql> desc student;

刪除表 mysql> drop table student;

修改表
新增欄位 mysql> alter table student add column sgender varchar(2);
刪除欄位 mysql> alter table student drop column sgender;
修改欄位型別 mysql> alter table student modify column remark varchar(100);
修改欄位名稱 mysql> alter table student change column sgender gender varchar(2);
修改表名稱 mysql> alter table student rename to teacher;

插入全部欄位 INSERT INTO student VALUES(1,'張三','男',20);
插入部分欄位 INSERT INTO student(id,NAME) VALUE(3,'王五');

修改所有資料 update student set gender='女'; //修改整個表的所有欄位的資料值
帶條件的修改 update student set gender='女' where id=1;
修改多個數據 update student set gender='女',age=30 where id=1

刪除所有的資料 delete from student;
另一種方式的刪除 truncate table student; //刪除整張表
delete from: ①可以帶條件刪除 ②只能刪除表的資料,不能刪除表的約束
③使用delete from刪除的資料可以回滾(事務)
truncate table: ①不能帶條件刪除 ②既可以刪除表的資料,也可以刪除表的約束
③使用truncate table刪除的資料不可以回滾
帶條件的刪除 delete from student where id=2;

查詢所有資料 select*from student;
查詢指定列 select id,name from student;
查詢時指定別名(as) //在多表查詢時經常使用表的別名(查詢出來的欄位資料會顯示為別名)
select id as '編號’,name as '姓名' from student as s;
查詢時新增常量列 //在查詢表時新增一個班級列,欄位為’java就業班’,別名’年級’
select id,name,gender,age,'java就業班' as '年級' from student;
查詢時合併列 //只能合併數值了型別的欄位,通常用於統計2列數值型別欄位的資料和
select id,(math+english) from student;
查詢時去除重複 //例如查詢性別(不去重的話會顯示重複)
select distinct gender form student;
select distinct(gender)form student;

條件查詢(where) 邏輯條件 and(與) or(或)
比較條件 > < >= <= == <>(不等於) between and(>=且<=)
判空條件(null 空字串) is null / is not null / ==’’ / <>’’
模糊條件 like //通常使用以下替換標記:
– % 表示任意個字元
– _ 表示替換一個字元

	  select * from student where id=2 and name='李四';
	  select * from student where id=2 or name='張三';
	  select * from student where math>70;
	  select * from student where math>=70 and math<=90;
	  select * from student where math betreen 70 and 90;//與上面一句相同
	  select * from student where address =='' or is null;
	  select * from student where address <>'' and is not null;	
	  select * from student where name like '張%';	
	  select * from student where name like '張_ _';(匹配幾個就用幾個)
	  select id,(math+english+chinese) as '總成績' from student;

聚合查詢 常用函式:sum() avg() max() min() count()

	select sum(math) as 'math的總成績' from student; //查詢所有學生數學成績和
    select avg(math) as 'math的平均分' from student; //查詢數學的平均分
    select max(math) as 'math的最高分' from student;
    select min(math) as 'math的最低分' from student;
    select count(*)  from student;   //統計欄位的最大數(包括空)	
    select count(id)  from student;  //統計id的數量(不包括空)

分頁查詢(limit 起始行,查詢幾行)//起始行從0開始
select * from student limit 0,2; //查詢第1,2條記錄

查詢排序(order by) order by 欄位 asc/desc 預設情況下按照插入記錄的順序排序

    select * from student order by id asc; //正序排序
    select * from student order by math asc,english desc;按數學正序,英語反序

分組查詢(group by) 分組之前用where關鍵字,分組之後用having關鍵字
//按照性別進行分組,並統計每個組的人數
select gender,count(*) from student group by gender;
//篩選出人數大於2的記錄
select gender,count(*) from student group by gender having count(*)>2;