1.表操作:
create table tableName
(id int(6) not null primary key auto_increatment,name varchar(10) not null,value double(10,2) not null ); # 創建表
show columns from tableName; # 查看表結構
drop table tableName; # 洗掉表
alter table tableName add/drop/modify columnName; #對表欄位進行增、刪、改型別
alter table t1 change c1 c2 varchar(8); # 將表中列名c1進行修改c2(c2的型別)
alter table t1 rename t2; # 將表t1改名為t2
2.資料表中資料記錄的增刪改:
insert into tableName values (1,'蘋果',12.22),(2,'香蕉',8.88); # 插入表中資料
delete from tableName where id=1; # 洗掉滿足條件的資料記錄
update tableName set c1 = 2 where id = 1; # 更新滿足條件的資料記錄中的欄位值
3.資料查詢:
show databases;
show tables;
select * from tableName where id > 10 limit 0,2; # 查詢滿足條件的資料記錄的前兩行
count:
select count(name) from tableName where 1;
distinct:
select distinct(name) from tableName where 1;
sum:
select sum(value) from tableName where 1;
limit:
in:
select * from tableName where id in (5,10);
like:
? 任何一個單一的字符
* 任意長度的字符
# 0~9之間的單一數字
[字串列] 在字串列里的任一值
[!字串列] 不在字串列里的任一值
4.SQL函式實作批量插入資料:
DROP PROCEDURE IF EXISTS proc_initData; #--如果存在此存盤程序則刪掉
CREATE PROCEDURE proc_initData()
BEGIN
DECLARE i INT DEFAULT 1;
WHILE i<=10000 DO
INSERT INTO data(user_id) VALUES(i); #插入1000條此資料
SET i = i+1;
END WHILE;
END;
CALL proc_initData();
5.補充:
補充1——內連接與外連接,都要求兩個表中的鍵相聯系:
內連接:可以查詢到雙方聯合的資料:
select t1.*,t2.* from table1,t1 inner join table2,t2 on t1.num = t2.num;
外連接:可以查詢到一邊表存在的符合條件的資料,左連接和右連接(left join,right join):
select t1.*,t2 from table1,t1 left jon table2,t2 on t1.name = t2.name where price>1000;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/117955.html
標籤:MySQL
上一篇:Mysql中的索引
下一篇:常用SQL陳述句分享
