sql基本入門
一.資料庫查詢語句:select
查詢所有資料:
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 『%馬克思%』;
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後面的兩個數字中:第乙個代表偏移量,第二個代表每頁展示的數量
二. 刪除資料:delete
delete from 表名 where 條件;
delete from exam_questions where id<2000;
delete from exam_questions where coursecode=』00041』 and chapterid=1;
三.插入新資料: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;
五、更新表結構的語句:
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
SQL入門學習 1 基本概念
1.1.1 資料庫 儲存有組織的資料的容器。不要與dbms的概念混淆,dbms是指資料庫管理系統,而一般的我們是通過dbms來訪問資料庫的。1.1.2 表 某種特定型別資料的結構化清單 同一資料庫中不能兩次使用相同的表名 模式 schema 關於資料庫和表達布局及特性資訊。1.1.3 列與資料型別 ...
動態SQL入門
動態sql語句的理解 1.動態sql的分類 根據要處理的sql語句的作用不同,可以使用三種不同型別的動態sql方法 1 使用execute immediate語句可以處理包括ddl create alter和drop dcl grant revoke dml insert update delete...
sql注入入門
參考文章 正常的語句 select from users where username marcofly and password md5 test 異常操作 在使用者名稱輸入框中輸入 or 1 1 密碼隨便輸入,這時候的合成後的sql查詢語句為 select from users where us...