MySQL 基礎語句

1、增長數據:數據庫

insert 語句能夠用來將一行或多行數據插到數據表中,使用的通常形式以下:函數

insert into 表名(字段列表)values(值列表)spa

insert[into]表名[(列名1,列名2,列名3,列名4,...)]values(值1,值2,值3,值4,...)blog

insert into students values(NULL, "張三", "男", 20, "18889009876");字符串

有時咱們只須要插入部分數據, 或者不按照列的順序進行插入, 可使用這樣的形式進行插入:table

insert into students(name,sex,age)values("李四","女",21);date

1.添加數據:select

insert into stu(name,sex,age) values('張學友','男',18);
insert into stu(name,sex,age) values('張娜拉','女',73);
insert into stu(name,sex,age) values('張家輝','男',23);
insert into stu(name,sex,age) values('張匯美','女',85);
insert into stu(name,sex,age) values('張鐵林','男',35);

 

2.查詢數據數據類型

2.一、查詢全部學生im

select * from stu;

2.二、查詢年齡大於80歲的女生

select * from stu where age >80 and sex = "女";

where 關鍵詞用於指定查詢條件, 用法形式爲: select 列名稱 from 表名稱 where 條件;

以查詢全部性別爲女的信息爲例, 輸入查詢語句: select * from students where sex="女";

eg:查詢年齡在21歲以上的全部人信息:select * from students where age > 21;

查詢名字中帶‘王’字的全部人信息:select * from students where name like "%王%";

查詢id 小於5而且年齡大於20的全部人信息:select * from students where id <5 and age>20;

2.三、聚合函數:

 

得到學生總人數:select count(*) from students

得到學平生均分:select avg(mark)from students

得到最高成績:select max(mark)from  students

得到最低成績:select min(mark) from students

得到學生總成績:select sum(mark) from students

3.刪除數據

delete from 表名[刪除條件]

刪除表中全部數據:selete from students;

刪除id 爲10的行:delete from students where id=10;

刪除全部年齡小於88歲的數據:delete from students where age<88;

4.修改數據

 4.一、將編號爲1 的學生年齡加1歲

updata stu set age=age+1 where id =1;

4.二、將80歲以上的女學生年齡修改爲90歲並將姓名添加「老人」

#CONCAT(str1,str2,...)鏈接字符串

update stu set age = 90,name = CONCAT(name,'(老人)')where age >=80 and sex='女';

4.三、將編號4的學生名字修改成‘張美麗’

update stu set name = '張美麗' where id =4;

4.四、刪除數據

刪除年齡大於70歲的學生

delete from stu where age >70;

4.五、刪除全部學生

delete from stu;

5.更新數據

 

update 語句可用來修改表中的數據, 基本的使用形式爲:

 

update 表名稱 set 列名稱=新值 where 更新條件;

 

Update 表名 set 字段=值 列表 更新條件

 

將id爲5的手機號改成默認的"-": update students set tel=default where id=5;

 

將全部人的年齡增長1: update students set age=age+1;

 

將手機號爲 13723887766 的姓名改成 "張果", 年齡改成 19: update students set name="張果", age=19 where tel="13723887766";

6.修改表

alter table 語句用於建立後對錶的修改,基本用法以下:

6.一、添加列

基本形式:alter table 表名add 列名 列數據類型[after 插入位置]

eg: 在表的最後追加列 address: alter table students add address char(60);

在名爲 age 的列後插入列 birthday: alter table students add birthday date after age;

6.二、刪除列

刪除 age 列:alter table students drop age;

6.三、重命名

基本形式:alter table 表名 rename 新表名;

eg: 重命名 students 表爲temp: alter table students rename temp;

6.四、刪除表

基本形式:drop table 表名;

eg:刪除students表:drop table students;

6.五、刪除數據庫

基本形式: drop database 數據庫名;

eg:刪除lcoa數據庫:drop database lcoa;

相關文章
相關標籤/搜索