SQL語句
SQL主要操作有增刪改查(curd)遮咖,其中查詢的頻率要高于其它操作,因為一般來說造虏,進行其它操作之前御吞,你需要明確表中有哪些字段踢械,要修改哪些值,要刪除哪條記錄魄藕。
查看表結(jié)構(gòu)
查看數(shù)據(jù)
-- select * from 表名内列; 查看表內(nèi)所有數(shù)據(jù)
select * from article;
添加數(shù)據(jù)
-- insert into 表名(字段...) values(值...) 一一對應(yīng)
insert into article(title,content_file_path) values("Python學習","/article/details/80244167");
修改數(shù)據(jù)
-- update 表名 set 列名=值 where 條件
update article set title ="Python學習2",content_file_path = "/article/details/80244167" where id =2;
刪除數(shù)據(jù)
-- delete from 表名 where 條件
delete from article where id = 3 ;
上面簡單操作了一遍,下面詳細演示并說明一下背率。
建表語句
-- 創(chuàng)建數(shù)據(jù)表article话瞧,
-- article 有id 無符號int類型 自動增長的主鍵,
-- title varchar類型長度100 非空
-- content_file_path varchar類型長度100 非空
-- content_num 無符號int類型 非空 默認值為0
-- is_delect tinyint類型 非空 默認值為0
create table article(
id int unsigned auto_increment primary key comment "文章id",
title varchar(100) not null COMMENT "文章標題",
content_file_path varchar(100) not null comment "內(nèi)容文件路徑",
content_num int unsigned not null default 0 comment "內(nèi)容字數(shù)",
is_delect tinyint not null default 0 comment "文章是否刪除"
) comment "文章表";
添加數(shù)據(jù)
全部添加
-- 給表里的所有字段添加值
insert into article(id,title,content_file_path,content_num,is_delect)
values(5,"Python學習","/article/details/80244167","2000",0)
insert into article values(7,"Python學習","/article/details/80244167","2000",0)
部分添加
-- 給表里的部分字段添加值
insert into article(title,content_file_path) values("Java學習","/article/details/80244167");
insert into article(content_file_path,title) values("Android學習","/article/details/80244167");
一次性添加多條數(shù)據(jù)
insert into article(title,content_file_path)
values("Python學習1","/article/details/80244167"),
("Python學習2","/article/details/80244167"),
("Python學習3","/article/details/80244167"),
("Python學習4","/article/details/80244167");
修改數(shù)據(jù)
修改一個字段的值
update article set content_num=520 where id=5; --修改id 為5 的記錄的content_num 的值為520
update article set content_num=1314 where id=7; --修改id 為7 的記錄的content_num 的值為1314
修改多個字段的值
--修改id 為5 的記錄的content_num 的值為520寝姿,title為大話西游
update article set title="大話西游",content_num=1206 where id=5;
update article set title="愛你一萬年",content_num=820 where id=7;
修改某個字段的全部值
update article set title="YanglingWang",content_num=5201314 ;
刪除數(shù)據(jù)
刪除數(shù)據(jù)
delete from article where id=9;
刪除兩張表的記錄[1]
delete from article1,article2 USING article1, article2 where article1.id=1 and article2.id=1;
偽刪除(通過邏輯控制交排,不顯示數(shù)據(jù))
update article set is_delect=1
到此結(jié)?DragonFangQy 2018.5.10