常用基礎sql整理

2021-08-14 09:45:33 字數 2013 閱讀 9312

show databases;

顯示所有資料庫

create database abc;

建立資料庫abc

useabc;

使用資料庫abc

create table aa(id  int , name varchar(30),age int);

建立表aa

id,name,age

desc aa;

顯示表aa中的欄位名

drop table aa;

刪除表aa

insert intoaa values(1,'n1',11);

向表aa中插入一條資料

insert into aa(id) values(4);

向表aa中插入一條資料

delete from aa  where id=4 and name="n1";

刪除表aa中所有id為4,name為n1的資料

truncate table aa;

清空表aa中所有資料,比delete from aa;效率高。

delete不釋放空間,truncate會徹底釋放空間

update aa set age=55,name="xx"  where id=1;

修改表aa中的字段

select * from aa;

選擇aa中所有資料

select name  from aa;

選擇aa中所有name資料

select name,age from aa;

選擇aa中所有name和age資料

條件查詢

select * from aa where id=1;

選擇aa中所有id為1的資料

select  min(id),name,age from aa where id>=5;

選擇aa中id>=5的資料中id最小的資料

select * from aa  where id between 5 and 30;

查詢id在[5,30]範圍內的資料

select * from aa

where id in(5,10,15);

查詢id為括號內列舉值的資料

select distinct name from aa;

name欄位相同時排除

select * from aa order by id desc;

查詢的資料按照id從大到小排序(不寫desc預設從小到大)

排序時,null作為最大值

select * from aa

where name like 『+%+』;

查詢所有name以+開頭,+結尾的字段【%任意數量字元,_單個字元】

子查詢select name,age from

(select * from aa where id>15) as temp;

as temp作為()內查詢結果的別名,雖然沒有被使用但是必須

多表查詢

select aa.id,name from aa,cc

where aa.id=bb.id;

sql99寫法:

select aa.id name from

aa inner join cc on aa.id=cc.id;

從aa和bb兩張表中進行查詢,限制條件為兩個表的id相同

(顯示字段 時,如果該欄位兩個表都有,需要用aa.id這種語法區分)

select aa.id name from

aa left outer join cc on aa.id=bb.id;

在滿足aa.id=cc.id情況下,將aa裡的資料全部也取出來

select aa.id name from

aa right outer join ccon aa.id=cc.id;

在滿足aa.id=cc.id情況下,將cc裡的資料全部也取出來

select id from aa

union

select id from cc;

將兩個表的查詢結果合併起來

常用sql整理

1.資料庫備份mysqldump uroot p msmk sql msmk.sql 匯出資料 source sql msmk.sql 匯入資料2.資料庫操作create database msmk 建立資料庫 show databases 檢視全部資料庫修改資料庫編碼alter table vrs...

基礎SQL整理

一.mysql 表新增字段 1.alter table people add column name varchar 100 default null comment 姓名 修改表 people 增加字段 name 長度100 預設為null 備註 姓名 二.mysql 將一張表的結果插入到另一張表...

SQL基礎整理

用於訪問和處理資料庫的標準的計算機語言,結構性查詢語言 structured query language 可以訪問和處理資料庫,一種 ansi american national standards institute 美國國家標準化組織 標準的計算機語言。mysql 資料型別 在 mysql 中...