1. 程式人生 > >資料庫儲存過程,Mysql檢視,Mysql語句

資料庫儲存過程,Mysql檢視,Mysql語句

相關連結: https://www.cnblogs.com/chenpi/p/5133648.html

 

SQL,結構化儲存語言,有自己的語法規則。儲存過程使得,一系列sql語句可以通過函式呼叫的形式進行使用。

sql語句是執行時編譯執行的,而儲存過程可以預編譯,效能快一些。

mysql 變量表示形式,@varName

use life;
set @param = "ddd";
set @param2 = 1;
select @param, @param2;

 

mysql的儲存過程:

mysql 定義儲存過程的時候,如果過程體是很多條語句,需要使用begin end,但是在begin end中間寫;會報錯,說begin沒有配套的end。解決方式是DELIMITER // 。。。DELIMITER ;  

mysql的DELIMITER 表示直譯器在遇到什麼符號的時候可以開始執行。

drop procedure if exists selectById;
DELIMITER // 
create procedure selectById( a int)
begin
  select * from family where _id =a;
end//
DELIMITER ; 

set @a = 1;
call selectById(@a);

 

 

 

sql檢視:select的結果的結構叫做檢視,檢視和基本表有區別。檢視通過基本表或檢視運算得到,它引用基本表的資料,不儲存具體的資料。

檢視方便操作,減少複雜的查詢語句。

對檢視進行的增刪改操作要符合基本表的約束,例如:使用insert、delete操作檢視來操作基本表,需要保證未出現在檢視中的列都允許空。(!delete 也需要)

 

 

 

 

 

 

mysql部分資料型別 tinyint 1位元組,int 4位元組,bigint 8位元組,float 4位元組,double 8位元組。

char(n) 定長字串,varchar(n)不定長字串,text 長文字資料( 5.0.3以上版本 varchar長度可以很大, 可使用varchar 儲存長文字,節省空間  )

 

建立資料庫,建立表

drop database if exists life;
create database life;
use life;

drop table if exists family;
create table family (
fullName varchar(8),
info varchar(300)
);
alter table family add column _id int not null primary key auto_increment;


insert into family (fullName, info) values("father","love food");
insert into family (fullName, info) values("mother", "love flowers");
insert into family (fullName, info) values("brother", "sunny boy");
insert into family (fullName, info) values("sister", "shy girl");

drop table if exists worklife;
create table worklife(
_id int not null primary key auto_increment,
_name varchar(40),
detail varchar(800)
);

insert into worklife (_name, detail) values ("first_c", "say goodbye to school");
insert into worklife (_name, detail) values ("second_c", "say goodbye to first leader");
insert into worklife (_name, detail) values ("known", "spring is coming");

 

修改表結構:

alter table t_name add column col_name col_type;

alter table t_name drop column col_name ;

alter table t_name add primary key (col_name);

alter table t_name drop primary key (col_name);

增刪改查

insert into t_name values (all col_values)

insert into t_name (interested col_names) values (col_values)

select * from t_name where condition_words;

update t_name set field1 = field1+3

select t_col_name as v_col_name from t_name where

select avg(t_col_name) as v_col_name from t_name where

求和函式sum,求平均數avg 最大值max,最小值min

delete from t_name where