SQL基礎語句

2021-06-29 04:06:06 字數 3327 閱讀 5315

一.資料庫查詢語句:select

1. 查詢所有資料:

select * from 表名;

select * from exam_books;

2.按照一定的條件查詢:

select * from 表名 where 條件;

select * from exam_books where id<20;

3.範圍條件查詢:

select * from 表名 where 字段 between  值1  and 值2 ;

select * from exam_books where id<20 and id>10;

select * from exam_books where id between 10 and 20;

select * from exam_books where addtime between '2011-03-17 00:00:00' and '2011-03-18 00:00:00';

4.模糊查詢:

select * from 表名 where 字段 like '%條件%';

select * from exam_books where bookname like '%馬克思%';

select * from exam_books where bookname like '馬__';

(查詢書名中第乙個字是「馬」,後面有兩個字)

%和_叫做萬用字元

5.復合查詢:

select * from 表名 where 條件 and 另乙個條件;

select * from exam_questions  where coursecode = '03706' and chapterid = 2;

6.查詢個數:

select count(*) from 表名 where 條件

select count(*) as 別名 from exam_questions  where coursecode = '03706' and chapterid = 2;

7.查詢結果按順序排列:

正序、倒序(asc/desc)

select * from 表名 where 條件 order by 字段 desc/asc;

select * from exam_questions where coursecode = '03706' order by chapterid;

8、按照limit查詢某個範圍內的資料:

select  * from exam_questions order by id asc  limit  0, 15;(代表查詢出的第一頁的資訊)

select  * from exam_questions  order by id asc  limit 15, 15;(代表查詢出的第二頁的資訊)

【備註:】limit後面的兩個數字中:第乙個代表偏移量,第二個代表每頁展示的數量

偏移量 = (當前頁碼數-1) * 每頁顯示條數

select 字段 as  別名  from 表名 where...   order by ...  limit offset , count;

9、sql常用函式:

count()

length()

min()

max()

二. 刪除資料:delete

delete from 表名 where 條件;

delete from exam_questions where id<2000;

delete from exam_questions where coursecode='00041' and chapterid=1;

delete    from    表名    where子句

三.插入新資料:insert

insert into 表名(字段) values(值); 

insert into exam_weburl(webname , weburl , info , bigtypeid) values('人人網', 'renren.com' , '這個**不錯' , 3);

四.更新資料:update

update 表名 set 欄位1=值1, 欄位2=值2 where 條件;

update exam_weburl set info='這個**不太好' where id=73;

update exam_weburl set webname='人人2', weburl='www.renren.com' where id=73;

五、建立表結構的語句:

create table  表名 (_id integer primary key autoincrement , 欄位2, 欄位3...)

例如:create table tb_newwords (

_id integer primary key autoincrement

, words  , detail );

六、更新表結構的語句:

1、如需在表中新增列,請使用下列語法:

alter table table_name

add column_name datatype

2、要刪除表中的列,請使用下列語法:

alter table table_name 

drop column column_name

3、要改變表中列的資料型別,請使用下列語法:

alter table table_name

alter column column_name datatype

【備註:】

ddl (data define language)  

資料定義語句

create,drop,alter

dml (data manipulate language) 

資料操縱語句

insert,delete,update,select  (crud)

dcl  

資料控制語句

grant, revoke.

tcl(transaction controll language)  

事務控制語句. 

學習sql

的主要重心應該放在

dml語句上,而

dml語句中的重點應該是在

select

語句上。

crud:是指在做計算處理時的增加(create)、查詢(retrieve)(重新得到資料)、更新(update)和刪除(delete)幾個單詞的首字母簡寫。主要被用在描述資料庫的基本操作。

SQL基礎語句

1.1.1dml 資料操作語言 1.1.2 ddl 資料定義語言 select update delete insert 1.2.1 select語法a.查詢所有 select from 表名 b.查詢列 select 列名 from 表名 注意 查詢列名時,列名用逗號隔開,最後的列名不要加逗號1....

基礎sql語句

從資料庫中刪除資料 delete 插入資料 insert into 建立新資料庫 create database 修改資料庫 alter database 建立新錶 create table 變更資料庫表 alter table 刪除表 drop table 建立索引 create index 刪除...

SQL基礎語句

select 語句用於從資料庫中選取資料。結果被儲存在乙個結果表中,稱為結果集。select column name,column name from table name 與select from table name 在表中,乙個列可能會包含多個重複值,有時您也許希望僅僅列出不同 distinc...