mysql數據庫的增刪改查_MySQL數據庫之基礎增刪改查操作
作者: ? ? ? ?守望幸福
最后修改日期:2014-7-27
所操作的MySQL數據庫名:firstdb
所用的兩個表結構如下:
student表
number name socre born_date
1 ? ? ?張三 ?96 ?2000-01-01
2 ? ? ?李四 ? 98 ?1999-4-23
... ? ?... ? ?.. ? ........
customer表
id ? ?firstname ? surname
1 ? ? ?張 ? ? ? ? ? ?三
2 ? ? ?李 ? ? ? ? ? ?四
3 ? ? ?王 ? ? ? ? ? ?五
4 ? ? ?麻 ? ? ? ? ? ?六
... ? ?... ? ? ? ? .....
以上表為例進行如下增刪改查操作
增加操作
增加一個數據庫: ? ? ? ? ? ? ? ? ? ? create database firstdb;
增加一個表結構: ? ? ? ? ? ? ? ? ? ? create table student(number int,name varchar(20),score float,born_date date)0;
增加一行數據: ? ? ? ? ? ? ? ? ? ? ? insert into student(number,name,score,born_date) values(5,'zhang',100,'1994-11-11');
增加多行用多個(,,,,),之間用逗號隔開(student中的參數可以省略)。
增加一列數據: ? ? ? ? ? ? ? ? ? ? ? alter table student add birthday DATE;
查詢操作
簡單基礎查詢
:
顯示所有數據庫: ? ? ? ? ? ? ? ? ? ? ? ? ? ?show databases;
顯示指定數據庫的所有表: ? ? ? ? ? ? ? ? ? ?show tables;(注:使用前需要指定使用哪個數據庫,use databasename;)
顯示指定的數據庫: ? ? ? ? ? ? ? ? ? ? ? ? ?show create database databasename;(注:databasename為數據庫名)
顯示指定的表: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?show create table tablename; ? ? ? (注:tablename為表名)
顯示表結構: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?describe tablename;
按成績從小到大排列顯示: ? ? ? ? ? ? ? ? ? ? select * from student order by score;
按成績反序排列顯示: ? ? ? ? ? ? ? ? ? ? ? ? select * from student order by score desc;(注desc 為反序,如果不指定默認值為asc)
限制輸出一行: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select * from student order by score desc limit 1;
第一個參數為偏移量,
第二個參數為距離偏移量顯示的行數 ? ? ? ? ? ? select * from student order by score desc limit 0,5;
根據姓名查詢數量: ? ? ? ? ? ? ? ? ? ? ? ? ?select count(name) from student;
根據姓名查詢數量,區別重名: ? ? ? ? ? ? ? ? select count(distinct name) from student;
根據姓名顯示表中的詳細信息,區別重名 ? ? ? ? select distinct name from student;
區別所有元素顯示表中的詳細信息: ? ? ? ? ? ? select distinct * from student;
顯示成績最大值: ? ? ? ? ? ? ? ? ? ? ? ? ? ?select max(score) ?from student;
顯示成績最小值: ? ? ? ? ? ? ? ? ? ? ? ? ? ?select min(score) from student;
顯示成績平均數: ? ? ? ? ? ? ? ? ? ? ? ? ? ?select avg(score) from student;
顯示成績總和: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select sum(score) from student;
數據庫也可用於普通數值計算: ? ? ? ? ? ? ? ? select 2*2;
顯示的所有信息中成績+10: ? ? ? ? ? ? ? ? ? ?select number,name, score+10 from student;
顯示當前時間,日期: ? ? ? ? ? ? ? ? ? ? ? ? select now(),current_date();
顯示born_date的年份: ? ? ? ? ? ? ? ? ? ? ? select year(born_date) from student;
顯示born_date的月份: ? ? ? ? ? ? ? ? ? ? ? select month(born_date) from student;
顯示born_date的日期: ? ? ? ? ? ? ? ? ? ? ? select dayofmonth(born_date) from student;
高級查詢:
變量別名顯示: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select name,month(born_date) as month,dayofmonth(born_date) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? as day from student;
使用concat()函數連接列顯示: ? ? ? ? ? ? ? ?select concat(number,name) as name from student; ?(注:concat(字符串1,字符串2,...)
顯示一年中的第幾天: ? ? ? ? ? ? ? ? ? ? ? ?select dayofyear(born_date) from student;(注:也可以計算具體數值如:select dayofyear('1994-11-11');)
同時操作兩個表: ? ? ? ? ? ? ? ? ? ? ? ? ? ?select id ,name,score,firstname from student,customer where id=number;
顯示兩個表中姓名后綴相同的數據的所有屬性: ? ?select * from student,customer where right(student.name,1)=customer.surname;
group by從句: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select number,sum(score) as sum from student group by number;
刪除操作
刪除整個數據庫: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop database firstdb;(注:drop database 數據庫名;)
刪除整個表: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?drop table student;
刪除表中的number為1的一行: ? ? ? ? ? ? ? ? ? delete from student where number= 1;
刪除表中的一列: ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?alter table student drop born_date;
更改操作
改變表中的數據: ? ? ? ? ? ? ? ? ? ? ? ? ? update student set born_date='1994-11-11' where number=1;
改變表中數據變量名,允許新舊名重復: ? ? ? ?alter table student change born_date birthday;
改變表中變量數據類型: ? ? ? ? ? ? ? ? ? ? alter table student modify born_date date;
為表重命名,兩種方法: ? ? ? ? ? ? ? ? ? ? alter table student rename newname;
alter table student rename to newname;
總結
以上是生活随笔為你收集整理的mysql數據庫的增刪改查_MySQL數據庫之基礎增刪改查操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zabbix mysql模板_zabbi
- 下一篇: python错误代码40035_Pyth