SQL簡單使用 基礎篇

2021-08-31 18:40:08 字數 2869 閱讀 4843

搭建好mysql資料庫後,就可以練習sql語句,達到熟練運算元據庫的目的。sql語句主要分為增刪改查。多練習就可以熟能生巧了

先建立乙個資料表便於做增刪改查的練習,這裡就直接將建立表的語句寫出來先執行。後面在學習如何建立資料表

set names utf8;

set foreign_key_checks = 0;

-- ----------------------------

-- table structure for `websites`

-- ----------------------------

drop table if exists `websites`;

create table `websites` (

`id` int(11) not null auto_increment,

`name` char(20) not null default '' comment '站點名稱',

`url` varchar(255) not null default '',

`alexa` int(11) not null default '0' comment 'alexa 排名',

`country` char(10) not null default '' comment '國家',

primary key (`id`)

) engine=innodb auto_increment=6 default charset=utf8;

-- ----------------------------

-- records of `websites`

-- ----------------------------

begin;

insert into `websites` values ('1', 'google', '', '1', 'usa'), ('2', '**', '', '13', 'cn'), ('3', '菜鳥教程', '', '4689', 'cn'), ('4', '微博', '', '20', 'cn'), ('5', 'facebook', '', '3', 'usa');

commit;

set foreign_key_checks = 1;

1.select 用於查詢資料

示例:select * from websites; 查詢建立的websites表資料。 注意後面的分號,分號表示一段sql命令的結束。

select * from websites where name='**'; 查詢websites表中的『**』的資料, 號表示查詢這個websites表的所有資料。這裡號可以寫成表的某一列。

select name from websites; 或 select name from websites where country='cn';

2.select disinct 用於返回唯一不同的值。

示例:select distinct country from websites; 查詢websites表中country唯一不同的值。

3.where 子句用於提取那些指定標準的資料

運算子、<、=、!=、<>、>=、<= 大於/小於/等於/不等於/大於等於/小於等於

between 某個範圍內

in 指定針對某個列的多個可能值

like 搜尋某種模式

4.and & or

and 如果第乙個條件成立和第二個條件都成立,則and顯示一條記錄,

or 如果第乙個條件和第二個條件有乙個條件成立,則顯示一條一條記錄;

示例:select * from websites where country='cn' and alexa > 50;

select * from websites where country='cn' or alexa > 50;

結合運用

select * from websites where alexa >15 and ( country='cn' or country='us' );

5.order by 對於查詢的數值進行排序,order by 公升序, 加上desc為降序

示例:select * from websites order by alexa; 以alexa列的數值進行公升序排序

select * from websites order by country,alexa desc; 以country和alexa的數值進行降序排序(其中由於country在前,所以country優先順序高於alexa)

6.insert into 向表中插入新資料。

示例:7.update 用於更新表中已存在的記錄

示例:update websites set alexa ='5000' , country ='usa' where name ='菜鳥教程';

注意如果不加where則會將websites整張表的資料都改了。所以在實際生產環境中管理員都會對update使用進行限制,如果使用update時沒有加上where則會報錯

delete、drop、truncate 刪除資料 (其中drop與truncate刪除資料不可恢復,所以在使用前切記慎重,做好資料備份準備)

delete 用於刪除表中資料(可以刪除整張表的資料或者表中某一段資料,)

delete from websites where name='菜鳥教程';

delete table websites;

或 delete * from websites;

drop tables websites; 刪除websites資料表

drop database mysql; 刪除mysql資料庫

truncate tables websites刪除表資料,表的框架還保留,但資料不可恢復,這是與delete的區別點

SQL 筆記(基礎篇)

查詢資料庫表名為 websites 擁有字段 id name url alexa country 1.select 查詢 select 語句用於查詢資料庫中選取資料。語法 select 欄位名,欄位名 from 表名。注意 如果 select 後面跟的是 號,那麼欄位名則不用填寫,select 查詢...

SQL之基礎篇

說明儲存空間 bitbit資料型別是整型,其值只能是0 1或空值。這種資料型別用於儲存只有兩種可能值的資料,如yes 或no true 或false on 或off.很省空間的一種資料型別,如果能夠滿足需求應該盡量多用。1位元組tinyint tinyint 資料型別能儲存從0到255 之間的整數。...

SQL注入專題 基礎篇

隨著b s模式應用開發的發展,使用這種模式編寫應用程式的程式設計師也越來越多。但是由於這個行業的入門門檻不高,程式設計師的水平及經驗也參差不齊,相當大一部分程式設計師在編寫 的時候,沒有對使用者輸入資料的合法性進行判斷,使應用程式存在安全隱患。使用者可以提交一段資料庫查詢 根據程式返回的結果,獲得某...