MySql語句的基本使用(一)

2021-08-26 18:13:31 字數 4561 閱讀 4414

登入資料庫

在cmd命令視窗裡面輸入

mysql -uroot -p ;
然後輸入密碼即可

退出資料庫

exit ;-- (常用)

quit ; -- (不常用)

解決mysql亂碼問題

建立資料庫的操作

create database 資料庫名;
判斷是否存在該資料庫,如果不存在則建立,然後指定對應的編碼(為了防止中文亂碼問題)
create database if not exists 資料庫名稱  character set 具體的編碼;
顯示所有的資料庫
show databases;
修改資料庫的字符集
alter database 資料庫名稱 character set 具體的編碼;
刪除資料庫
drop  database 資料庫名稱;
使用資料庫(在資料庫裡建立表之前需要使用資料庫)
use 資料庫名稱;
查詢正在使用的資料庫
select database();
建立表的基本格式
create table 表名(列表1,型別1,列表2,型別2,...,列表n,型別n);
刪除表的操作
drop table 表名;
修改表名稱
alter table 表名 rename to 新的表名;
修改列名稱
alter table 表名 change 舊的列名稱 新的列名稱  新列資料型別;
修改列的資料型別
alter table 表名 modify 要修改的列名  新的資料型別;
增加列,刪除列
alter table 表名 add 增加的列名 資料型別;

alter table 表名 drop 刪除的列名;

新增表中的資料(如果省略表名後面的小括號,則後面values新增的值必須是所有列的資料)
insert into 表名稱 (列名1,列名2,列名3) values (值1,值2,值3);
刪除整個表的操作
delete from 表名稱;    -- 刪除整個表(逐行刪除) 效率低

truncate table 表名稱; -- 刪除整個表(先刪表,再建表) 效率高 【推薦使用】

修改表資料(如果不加後面的條件,則將該列所有的值都改為新的資料)
update 表名稱 set 列名1=資料1,列名2=資料2 [where 條件];
查詢表
select *from 表名; -- 查詢表所有的資訊

select 列名1,列名2 from 表名 ; -- 查詢指定列的資訊

select 列名1 as 新列名1,列名2 as 新列明2 from 表名稱; -- 將查詢的列名命名為別的名稱顯示出來(as可省略)

條件查詢(與或非)
-- 查詢年齡大於20小於30的

select *from 表名 where age>20 && age<30;

select *from 表名 where age>20 and age<30;

select *from 表名 where age between 20 and 30;

-- 查詢年齡等於20或30的

select *from 表名 where age>20 || age<30;

select *from 表名 where age>20 or age<30;

select *from 表名 where age in(20,30);

-- 查詢年齡不是20的

select *from 表名 where age!=20;

select *from 表名 where age is not 20;

佔位符(_ %)
-- _下劃線表示乙個字元,%表示多個字元

select * from 表名 where name like '李%'; -- 查詢姓李的 ,名字為多個字

select * from 表名 where name like '李_'; -- 查詢姓李的 ,名字為兩個字

排序
select *from 表名 order by age; -- 按年齡排序  預設公升序

select *from 表名 order by age desc; -- 按年齡降序排序

-- 建立表

create table student(

id int,

name varchar(20),

chinese float,

english float,

math float

); desc student; -- 檢視表結構

select *from student; -- 查詢表資訊

-- 新增資料

insert into student(id,name,chinese,english,math) values(1,'張小明',89,78,90);

insert into student(id,name,chinese,english,math) values(2,'李進',67,53,95);

insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);

insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);

insert into student(id,name,chinese,english,math) values(5,'李來財',82,84,67);

insert into student(id,name,chinese,english,math) values(6,'張進寶',55,85,45);

insert into student(id,name,chinese,english,math) values(7,'黃蓉',75,65,30);

select *from student;-- 1.查詢表中所有學生的資訊。

select name,english from student;-- 2.查詢表中所有學生的姓名和對應的英語成績。

alter table student add 總分 varchar(20); -- 新增總分一欄

update student set 總分=english+chinese+math; -- 吧總分的資料設定上去

select name,總分 from student; -- 3.統計每個學生的總分。

alter table student add 特長分 int; -- 先在表結構上加一列特長分

update student set 特長分=10; -- 將所有的特長分設定為10

update student set 總分=總分+特長分 -- 4.在所有學生總分數上加10分特長分。

select name,chinese '語文成績' ,english '英語成績' ,math '數學成績' from student;-- 5.使用別名表示學生分數。

select * from student where name='李一'; -- 6.查詢姓名為李一的學生成績

select * from student where english>90;-- 7.查詢英語成績大於90分的同學

select name,總分 from student where 總分》200;-- 8.查詢總分大於200分的所有同學

select *from student where english between 80 and 90; -- 9.查詢英語分數在 80-90之間的同學。

select *from student where math in(89,90,91); -- 10.查詢數學分數為89,90,91的同學。

select name,english from student where name like '李%';-- 11.查詢所有姓李的學生英語成績。

select *from student where math>80 and chinese >80 ;-- 12.查詢數學分》80並且語文分》80的同學。

select * from student where english>80 or 總分》200;-- 13.查詢英語》80或者總分》200的同學

select *from student order by math desc; -- 14.查詢所有學生的成績,按數學成績降序輸出。

select *from student order by 總分 desc;-- 15.對每個同學的總分排序後輸出,然後再按從高到低的順序輸出

select *from student where name like '李%' order by 總分 desc;-- 16.對姓李的學生總成績公升序輸出

mysql語句使用 MySQL 基本語句的使用

1.create 建立命令 建立新資料庫 mydb create database mydb 選擇資料庫 mydb use mydb 在當前資料庫中建立資料表 table1 create table if not exists table1 fileid int not null auto incr...

一 MySql基本語句(一)

資料庫 庫的操作 1 查詢庫 show databases 2 建立庫 create database 庫名 character set 編碼格式 utf8 3 檢視資料庫建立語句 show create database 庫名 4 刪除資料庫 drop database 資料庫名 5 修改資料庫 ...

mysql基本語句 mysql基本語句

mysql關係型資料庫rds中的老大哥,增刪改查是mysql入門的基礎 增刪改查語句 增刪改查的語句命令為 增 insert 刪 delete 改 update 查 select或者show 庫操作建立資料庫 create database shujukuba 建立帶字符集的資料庫 create d...