1. 程式人生 > >mysql總結原始碼——表結構和原始碼

mysql總結原始碼——表結構和原始碼

MySQL 最全總結--https://blog.csdn.net/feifeiyechuan/article/details/84145331

表結構:

/*
Navicat MySQL Data Transfer

Source Server         : MySql
Source Server Version : 50556
Source Host           : localhost:3306
Source Database       : students

Target Server Type    : MYSQL
Target Server Version : 50556
File Encoding         : 65001

Date: 2018-11-18 20:12:33
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for gaofei
-- ----------------------------
DROP TABLE IF EXISTS `gaofei`;
CREATE TABLE `gaofei` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of gaofei
-- ----------------------------

-- ----------------------------
-- Table structure for t_score
-- ----------------------------
DROP TABLE IF EXISTS `t_score`;
CREATE TABLE `t_score` (
  `scid` int(11) NOT NULL AUTO_INCREMENT,
  `sid` int(11) DEFAULT NULL,
  `yuwen` int(11) DEFAULT NULL,
  `shuxue` int(11) DEFAULT NULL,
  `yingyu` int(11) DEFAULT NULL,
  PRIMARY KEY (`scid`),
  KEY `stuscore_sid` (`sid`),
  CONSTRAINT `stuscore_sid` FOREIGN KEY (`sid`) REFERENCES `t_stu` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_score
-- ----------------------------
INSERT INTO `t_score` VALUES ('2', '1', '11', '100', '13');
INSERT INTO `t_score` VALUES ('5', '3', '11', '82', '73');
INSERT INTO `t_score` VALUES ('6', '4', '11', '92', '99');
INSERT INTO `t_score` VALUES ('9', '5', '11', '16', '63');
INSERT INTO `t_score` VALUES ('16', '5', '11', '16', '63');
INSERT INTO `t_score` VALUES ('17', '5', '11', '16', '63');
INSERT INTO `t_score` VALUES ('18', '5', '11', '16', '63');
INSERT INTO `t_score` VALUES ('19', '4', null, null, null);
INSERT INTO `t_score` VALUES ('20', null, null, '66', null);

-- ----------------------------
-- Table structure for t_stu
-- ----------------------------
DROP TABLE IF EXISTS `t_stu`;
CREATE TABLE `t_stu` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `sname` varchar(20) NOT NULL,
  `sage` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  UNIQUE KEY `sname` (`sname`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of t_stu
-- ----------------------------
INSERT INTO `t_stu` VALUES ('1', 'feifei', '90');
INSERT INTO `t_stu` VALUES ('3', 'zs', '99');
INSERT INTO `t_stu` VALUES ('4', 'zss', '99');
INSERT INTO `t_stu` VALUES ('5', 'l5', '99');
INSERT INTO `t_stu` VALUES ('6', 'donghao', '99');
INSERT INTO `t_stu` VALUES ('7', '_sfs%fs', '21');
INSERT INTO `t_stu` VALUES ('8', 'l_5', '21');
INSERT INTO `t_stu` VALUES ('9', 'AFADFA', '21');

-- ----------------------------
-- View structure for view_a
-- ----------------------------
DROP VIEW IF EXISTS `view_a`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_a` AS select `s`.`sid` AS `sid`,`s`.`sname` AS `sname`,`r`.`shuxue` AS `shuxue`,`r`.`yingyu` AS `yingyu` from (`t_stu` `s` join `t_score` `r`) where (`s`.`sid` = `r`.`sid`) ;

-- ----------------------------
-- View structure for view_b
-- ----------------------------
DROP VIEW IF EXISTS `view_b`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `view_b` AS select `t_stu`.`sid` AS `sid`,`t_stu`.`sname` AS `sname`,`t_stu`.`sage` AS `sage` from `t_stu` WITH CASCADED CHECK OPTION ;

原始碼:

create database students

show databases;


-- 刪除表格
create table gaofei(id int primary key auto_increment); -- 測試用

drop table gaofei; -- 刪除表格,刪除所有資訊

truncate table gaofei; -- 刪除表格所有資料,保留表結構

delete from gaofei; -- 刪除表格所有資料,保留表結構
delete from gaofei where id=1; -- 刪除指定條件的表格資料


use students;

create table t_stu(
	sid int primary key auto_increment,
	sname varchar(20) unique not null,
	sage int not null	
)auto_increment = 001;


create table t_score(
	scid int primary key auto_increment,
	sid int not null,
	yuwen int,
	shuxue int,
	yingyu INT
)auto_increment=1;

alter table t_score add constraint stuscore_sid foreign key(sid) references t_stu(sid)

insert into t_stu values(sid,'_sfs%fs',21);

insert into t_stu values(null,'l_5',21);

insert into t_stu values(null,'AFADFA',21);

insert into t_stu(sname,sage) values('w5',21)


insert into t_score values(scid,1,11,12,13);
insert into t_score values(scid,5,11,16,63);
insert into t_score values(scid,3,11,82,73);
insert into t_score values(scid,4,null,null,null);




select * from t_score; -- 110測試

select * from t_stu;  -- 110測試


delete from t_score where scid=1;  -- 刪除給定條件的記錄

-- 修改
update t_stu set sage=90;  -- 修改所有
update t_stu set sage = 99,sname='donghao' where sid > 5  -- 修改給定條件的記錄

-- 查看錶結構
desc t_score;
desc t_stu;


-- 比較運算子 > < >= <= = (!= <> )兩個不等於一樣
select * from t_score;

select * from t_score where yingyu > 60; 
select * from t_score where shuxue >= 60;
select * from t_score where shuxue < 50;
select * from t_score where shuxue <= 50;
select * from t_score where shuxue = 50;
select * from t_score where shuxue != 50;
select * from t_score where shuxue <> 50;

-- 邏輯運算子 and:並且 or:或者 not:
select * from t_score where yingyu > 60 and shuxue > 60;

select * from t_score where yingyu > 60 or shuxue >90;

select * from t_score where not yingyu = 73;  -- 可以轉換為!=的情況
select * from t_score where not (yingyu > 60 and shuxue > 60);

-- 模糊查詢  like
-- 萬用字元:
--  _ :一個字元位
--  % :匹配0個或多個字元
-- escape :轉義字元,匹配上面兩個使用

select * from t_stu where sname like '_aofe_';
select * from t_stu where sname like binary '%a%'; -- 區分大小寫的比較字串用binary

select * from t_stu where sname like '%g%'; -- 匹配包含a
select * from t_stu where sname like 'a%';  -- 匹配以a開頭

select * from t_stu where sname like '/_%/%%' escape '/'; -- 將/作為轉義字元


-- 正則匹配:rlike 和 regexp 
-- . :匹配任意單個字元
-- + :匹配一個或者多個字元
-- * :匹配0個或者多個字元
-- []:匹配任意一個[]內的字元
-- ^ :匹配開頭
-- $ :匹配結尾
-- {n}:匹配字元n次
--  []:匹配範圍內的一個字元
-- [^]:匹配不在範圍內的一個字元
-- a|b :匹配a或者b

select * from t_stu where sname rlike '^[gaofei]';

select * from t_stu where sname rlike 'a+';

-- select * from t_stu where sname REGEXP '\\w+';   -- 貌似沒有這種
select * from t_stu where sname binary REGEXP 'a+'; -- 區分大小寫的比較字串用binary



-- 上面兩種方式like 和relike、regexp的異同點:
-- 相同點:
-- 三個都不區分大小寫,如果需要區分,在where 後新增關鍵欄位binary

-- 異同點:
-- 除了使用運算子不同之外,兩個重要的差異: 
-- - SQL 的 LIKE 模式只用於匹配整個字串;正則表示式 REGEXP 可以匹配字串的任何部分。 
-- - LIKE 運算子是多位元組安全的。REGEXP 運算子只能正確地處理單位元組字符集,並不會考慮排序規則。


-- 排序 order BY
-- 升序 asc  (預設)
-- 降序 desc 
select * from t_stu order by sid; 
select * from t_stu order by sid desc;


-- distinct :查詢不重複的記錄
-- 注意:(1)distinct這個關鍵字來過濾掉多餘的重複記錄只保留一條,
-- 	但往往只用它來返回不重複記錄的條數,而不是用它來返回不重記錄的所有值。
-- 	其原因是distinct只能返回它的目標欄位,而無法返回其它欄位,
-- 			(2)distinct 必須放在欄位的最前面,否則報錯

select distinct sage from t_stu;
select distinct sage,sid from t_stu;  -- 同時按照(sage + sid)去重,只有一個不一樣的記錄就是不同的記錄



-- null 和 空字串的用法
-- null : 表示空值  is null    is not NULL
-- 空字串:有值 
-- --注意:當比較的欄位的值為null時,用is null查詢,如果有值的話,就用比較運算子等去查詢
select * from t_score  where shuxue is NULL;

select * from t_score where shuxue = '90';

select * from t_score where shuxue != 0 and shuxue is not null;

select * from t_score;  -- 110測試


-- 欄位 between A and B  查詢滿足條件範圍的記錄
select * from t_score where shuxue between 50 and 90; 

select * from t_score where shuxue >= 50 and shuxue <= 90;

-- 這兩個語句一樣的效果


-- in :包含哪些值,和邏輯or類似
select * from t_score where shuxue in (90,82,30);

select * from t_score where shuxue = 90 or shuxue = 82 or shuxue = 30;

-- 這兩個語句一樣的效果


-- count() 計數
-- min() 最小值
-- max() 最大值
-- avg() 平均數
-- sum() 求和
-- --注意聚合函式不能用於where子句中
select count(1) from t_stu;	-- 統計記錄總數
select count(*) from t_stu; -- 統計記錄總數
select count(sname) from t_stu;  -- 統計該欄位非空數目 

select min(sid) from t_stu;
select max(sid) from t_stu;
select avg(sid) from t_stu;
select sum(sid) from t_stu;
 

select * from t_stu limit 1,4; -- 查詢2-5條記錄,從下表為1的記錄查詢5條記錄

select * from t_stu limit  (n-1)*m,m; -- 每頁m條資料,那麼第n頁的第一條資料為:(n-1)*m,m


分組查詢

-- group by 分組彙總,根據 by 指定的規則對資料進行分組
-- 注意:group by 子句中,必須要指定需要分組的列,而且必須是出現在
-- select 之後除了聚合函式之外所有的列。
-- 
-- having : 把分組之後的資料進行篩選和過濾,類似於where的作用
-- 注意:聚合函式必須要寫在having後面,絕對不能寫在where後面!!
-- 
-- 
-- where: 在分組之前,把不符合條件的資料過濾掉,where條件中絕對不能寫聚合函式!!!


-- 條件查詢的順序:
where ------>  group by  -----> having

select sname,count(1) from t_stu GROUP BY sname;

select count(1) from t_stu;


-- !!!select *,count(1) from t_score where count(1)=1 GROUP BY shuxue;  -- 錯誤語法,where後面不能有聚合函式


-- rollup
	-- 在group by分組欄位的基礎上再進行統計資料。
select *,count(1) from t_score GROUP BY shuxue with ROLLUP;
-- coalesce
	-- 如果需要修改rollup新生成的統計資料的名稱用coalesce
	-- 參考:https://www.cnblogs.com/phpper/p/9384614.html

select coalesce(shuxue,'總計') as '數學',count(1) from t_score GROUP BY shuxue with rollup;


-- 18.子查詢
/*什麼是子查詢?
	  1、當一個查詢語句巢狀在另外一個查詢語句中稱之為子查詢。
    2、子查詢的結果作為外查詢的條件
	  在使用子查詢時,當子查詢的返回值只有
		一個時用=,當子查詢的返回值有多個時用in
    子查詢能使用的操作符:
       in  
       not in 
       =  !=
		   >  <  <>
		返回1條或者多條資料都可以使用in
*/
select * from t_stu where sid = (select sid from t_score where shuxue = 92);

select * from t_stu where sid in (select sid from t_score where shuxue = 12);

select * from t_stu where sid in (select sid from t_score where shuxue in (92,12));

select * from t_stu where sid not in (select sid from t_score where shuxue in (92,12));


巢狀子查詢
/*
		子查詢包含一個子查詢,
    巢狀子查詢包含多個子查詢。
*/

select * from t_stu where sid in (select sid from t_score where shuxue in (select shuxue from t_score where yingyu=13))



-- 子查詢和delete,update,子查詢可以它們的條件表示式提供結果集
delete from t_score where sid in (select sid from t_stu where sname='gaofei');

update t_stu set sname='feifei' where sid in (select sid from t_score where shuxue = 12);


exists 關鍵字 :
		用於檢查一個子查詢是否至少會返回一行資料(即檢測行的存在),返回值為true或false。

select * from t_stu where not exists (select * from t_score where shuxue = 100); -- 不存在數學=100資料的時候,顯示所有的學生資訊

-- 比較~:
select * from t_stu where sid not in (select sid from t_score where  shuxue = 100);



/*連結查詢主要有:等值連線、內連線、左外連線、右外連線、完全連線*/
-- 等值連線
(
/*
		總結:等值查詢技巧
		通常把要查詢的列放在select 後面,列中間用逗號分開,把涉及到的
		表放在from後面用逗號分開並把每個表都取別名,接著把幾個表的主外來鍵
		關係放在where後面,然後用=號連線,還可以加上其他條件。
    最後如果還可以根據需要加上分組語句。
*/
select s.sid ,s.sname from t_stu s,t_score r where s.sid = r.sid and r.shuxue = 12;
select * from t_stu s,t_score r where s.sid = r.sid and r.shuxue = 12;

-- 好程式碼:
select e1.* from emp e1,(select d.dname '部門名稱',avg(e.epay) 'avg' ,e.did from dept d,emp e where d.did=e.did group by d.dname) e2 
where e1.did=e2.did and e1.epay>e2.avg;   # 這裡建立了一個虛擬表e2,然後將他們等值連線關聯起來,e1表的工資大於其所在部門的平均工資,因為部門已經得到了關聯。
)

-- 內連線:關鍵字inner join...on,只有左,右2個表相匹配的行才能在結果中顯示,作用和等值連線一樣。
select * from t_stu s inner join t_score r on s.sid = r.sid;


-- 左外連線:關鍵字left join,左邊的表的行全部顯示,左邊連線右邊的值,右邊的表如果沒有匹配的值,顯示為null
select * from t_stu s left join t_score r on s.sid = r.sid;

-- 右外連線:關鍵字right join, 右邊的表的行全部顯示,右邊連線左邊的,左邊的表如果沒有匹配的值,顯示為null
select * from t_stu s right join t_score r on s.sid = r.sid;

-- 完全連線:關鍵字union, 用於合併兩個或多個select語句的結果集。 左,右2個表的行都全部顯示,如果沒有匹配的值,顯示為null
-- 合併的select語句的列數量、型別、順序必須完全一樣
select * from t_stu where sid in (1,2)
union
select * from t_stu where sid in (5,6)




-- 日期函式
select now(); -- 獲取當前的日期時間
select curdate();	-- 獲取當前的日期
select curtime();	-- 獲取當前的時間

/*
	select date_add(指定日期,interval 年月日間隔數 年月日季選擇);
	返回指定日期加上一個時間間隔後的日期
  
  select date_sub(指定時間,interval 年月日間隔數 年月日季選擇);
  函式從日期減去指定的時間間隔。
*/
select DATE_ADD(CURDATE(),INTERVAL 1 month); -- 下個月的今天
select DATE_ADD(CURDATE(),INTERVAL 1 quarter); -- 下個季度的今天
select date_add(curdate(),interval 1 year); -- 明年的今天
select date_add(curdate(),interval 1 day); -- 明天

select DATE_sub(CURDATE(),INTERVAL 1 month); -- 上個月的今天
select DATE_sub(CURDATE(),INTERVAL 1 quarter); -- 上個季度的今天
select date_sub(curdate(),interval 1 year); -- 去年的今天
select date_sub(curdate(),interval 1 day); -- 昨天

select date_add(curdate(),interval 10 day); -- 10天后
select date_add('2018-1-1',interval 356 day); -- 2018-1-1 356天后是哪一天



datediff()

/*
語法格式:datediff(結束時間,起始時間)
返回起始時間和結束時間之間的天數。
*/
select DATEDIFF('2019-1-4',curdate())
select datediff(curdate(),'2019-1-5')

-- !!!select datediff(CURTIME(),'00:29:10') -- 101錯誤,應該不常用,只用日期就行了
-- 這個具體的時間後邊查查 110 查查為什麼時間不行


date() 
 /*
 語法:date(日期時間表達式)
 提取日期或時間日期表示式中的日期部分。 
*/
select date(curdate());
select date(now());
select date(curtime());  -- 為Null,因為沒有date日期
select date('2018-11-18 00:34:45');


dayofweek(date)
     /*返回date是星期幾(1=星期天,2=星期一,……7=星期六) */
select dayofweek(curdate());

select concat('今天是星期:',dayofweek(now())-1) 'dayofweek';


dayofmonth(date) 
    /*返回date是一月中的第幾天(在1到31範圍內)*/
select DAYOFMONTH(now());
select DAYOFMONTH(CURTIME()); -- 沒日期結果為Null

dayofyear(date) 
    /*返回date是一年中的第幾天(在1到366範圍內)*/
select dayofyear(now());


month(date) 
    /*返回date中的月份數值*/ 
day(date)
     /*返回date中的天數數值*/ 
year(date)
     /*返回date的年份(範圍在1000到9999)*/ 
quarter(date) 
    /*返回date是一年的第幾個季度 */

select month(now());
select day(now());
select year(now());
select quarter(now());

week(date,first)
 /*返回date是一年的第幾周(first預設值0,first取值1表示週一是周的開始,0從週日開始)*/
select week(now(),1);

DATE_FORMAT(date,format) 
   /*根據format字串格式化date值*/
select date_format(now(),'%y=%m=%d=%h=%m=%s');



extract() 
   /*函式用於返回日期/時間的各個部分,比如年、季、月、日、小時、分鐘等等。*/

select extract(year from now());
select extract(month from now());
select extract(day from now());
select extract(quarter from now());
select extract(hour from now());
select extract(minute from now());
select extract(second from now());


timestampdiff() : 計算兩個日期的時間差函式
/*select timestampdiff(年月日季選擇,起始時間,結束時間);*/
select timestampdiff(day,'2017-4-9','2017-8-9');
select datediff('2017-8-9','2017-4-9');

select timestampdiff(year,'2017-4-9','2018-8-9');


last_day() 函式:返回月份中的最後一天
select last_day('2017-7-8');
select last_day(now());

-- 今天是星期幾
select dayofweek(curdate() - 1);
select dayofweek(curdate()) - 1;  -- 最好用第一種方法,這種方法週日顯示的是0
-- 今天是第幾季度
select quarter(curdate());
-- 本月一共有多少天
select day(last_day(curdate()));
-- 本月的第一天是星期幾
select dayofweek(date_sub(curdate(),interval day(curdate())-1 day)) - 1;
-- 本週的週一是幾號
select date_sub(curdate(),interval dayofweek(curdate()-1)-1 day);  


字元函式
concat() 
/*   concat(str1,str2,…)  concat函式可以連線一個或者多個字串
     concat_ws(x,s1,s2,...) 同concat(s1,s2,...)函式,但是每個字串直接要加上x 
*/
select concat('a','b','c');
select concat_ws('*','a','b','c');  -- 添加了分隔符


left(str, length)
/*
     從左開始擷取字串  
     說明:left(被擷取欄位,擷取長度)
*/
select left('abc',2);

right (str , length)
/*
     從右開始擷取字串  
     說明:left(被擷取欄位,擷取長度)
*/
select right('abc',2);


substring
擷取字串 
/*
     substring(str, pos) 從pos位置開始擷取到末尾
     substring(str, pos, length) 從pos位置開始擷取length位字串
     說明:substring(被擷取欄位,從第幾位開始擷取) 
     substring(被擷取欄位,從第幾位開始擷取,擷取長度)
*/
select substring('abcdefg',2);
select substring('abcdefg',2,2);

char_length(s)  
/*
返回字串s的字元數
*/
select char_length('abcdefg');

insert(s1,x,len,s2) 
/*
將字串s2替換s1的x位置開始長度為len的字串
*/
select insert('abcdefg',2,2,'123'); -- 將2位置開始兩位字元替換為123

upper(s) 
/*
upper(s): 將字串s的所有字母變成大寫字母
*/
select upper('abcDEfgH');

lower(s) 
/*
lower(s): 將字串s的所有字母變成小寫字母
*/
select lower('abcDEfgH');

trim(s) : 
/*
去掉字串s開始和結尾處的空格
並不會去除末尾的換行符,所以如果想去除換行符那麼:
	解決方法:
						UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ''), CHAR(13), '');
						char(10):  換行符
						char(13):  回車符

	MySQL的trim函式沒辦法去掉回車和換行,只能去掉多餘的空格,可以用MySQL的replace函式,解決掉這個問題
	缺點:會把字串中間出現的換行也會替換掉

--100  疑問
	解決:
		UPDATE `tran` 
		SET `status` = '1' 
		WHERE trim( trim( 
		BOTH '\r\n' 
		FROM content ) ) = '我愛你'

		用了兩個trim,這樣的好處是不會替換內容中間的換行和回車,
		只會處理頭尾的空格換行回車,相當於php中trim函式的作用了。 

*/
select trim(' abc ');

reverse(s) : 
/*
將字串s的順序反過來
*/
select reverse('abcdefg');


數學函式

abs(1) -- 取絕對值
ceil(x) -- 向上取整
floor(x) -- 向下取整
rand() -- 返回0-1的隨機數,包含0不包含1
pi() -- 返回圓周率(3.141593)
round(x,y) -- 保留x小數點後y位的值,但截斷時要進行四捨五入
truncate(x,y) -- 返回數值x保留到小數點後y位的值(與round最大的區別是不會進行四捨五入)
pow(x,y) 或 power(x,y) -- 返回x的y次方
sqrt(x) -- 返回x的平方根
mod(x,y) -- 返回x除以y以後的餘數

select sqrt(4);
select mod(5.5,3);
select mod(5,2);
select pow(2,4);
select power(2,4);


索引(東西還挺多,目前不用看,一般用於提高檢索效能)
/*
  建立索引
  語法:create index 索引名稱 on 表名(欄位名);
        alter table 表名 add index 索引名稱(欄位名);
*/
-- 案例
create index suoyin on t_stu(sid);

/*刪除索引 
  語法: drop index 索引名稱 on 表名;
  */
drop index suoyin on t_stu;
/*檢視索引*/
show index from tblname;

檢視
(
/*
什麼是檢視 
檢視是從一個或多個表中匯出來的表,是一種虛擬存在的表。
檢視就像一個視窗,通過這個視窗可以看到系統專門提供的資料。
這樣,使用者可以不用看到整個資料庫中的資料,而之關心對自己有用的資料。
資料庫中只存放了檢視的定義,而沒有存放檢視中的資料,這些資料存放在原來的表中。
使用檢視查詢資料時,資料庫系統會從原來的表中取出對應的資料。
檢視中的資料依賴於原來表中的資料,一旦表中資料發生改變,顯示在檢視中的資料也會發生改變。
檢視的作用 :
1.使操作簡單化,可以對經常使用的查詢定義一個檢視,使使用者不必為同樣的查詢操作指定條件
2.增加資料的安全性,通過檢視,使用者只能查詢和修改指定的資料。
3.提高表的邏輯獨立性,檢視可以遮蔽原有表結構變化帶來的影響。
建立檢視時不能使用臨時表。
即使表被刪除,檢視定義仍將保留。
定義檢視的查詢不能包含 以下語句:
order by
compute 子句
compute by 子句
into 關鍵字 
*/

/*
  建立檢視
  語法:create view 檢視名稱 as 查詢語句
*/
create view view_a as select s.sid,sname,shuxue,yingyu from t_stu s,t_score r where s.sid=r.sid;


/*查詢檢視
  語法:select * from 檢視名稱;
*/
select * from view_a;

/*刪除檢視 
  語法:drop view 檢視名稱;*/
drop view view_a;

/*向檢視中新增資料
  語法:insert into 檢視名稱(欄位1,欄位2,...) values (值1,值2,....);
  不能新增資料到多個表,因為新增語句涉及到了多個基表
	新增資料的時候,如果涉及的基表除了插入欄位外還有非空沒有預設值欄位,那麼也不會成功
*/
insert into view_a(shuxue) values(66);
SELECT * FROM T_SCORE;

/*向檢視中修改資料
  可以修改單表資料,不能修改多表資料
  update 檢視名稱 set 欄位1=新值1 where 欄位2=值2;
*/
update view_a set shuxue = 100 where sid = 1;
select * from view_a;

/*
  with check option 的使用
  語法:create view 檢視名稱 as 查詢語句  with check option;
  --在建立檢視時,可以使用with check option選項,
  --其作用是限定向檢視新增或修改資料時,新增或修改的資料必須遵照建立檢視時where之後的條件
*/
create view view_b as select s.sid,s.sname,r.shuxue,r.yingyu from t_stu s,t_score r with check option;
drop view view_b;

-- 修改表結構
create table test(
	id int primary key auto_increment,
	name varchar(20)
)auto_increment=1
drop table test;

修改表結構 :

  alter table 舊錶名 rename 新表名;
  //修改 資料表名
alter table test rename test1;


  alter table 表名 modify  欄位名 新資料型別;
  //只修改資料型別
alter table test1 modify name char(20);

  alter table 表名 change 舊欄位名  新欄位名 新的資料型別;
  //只修改欄位名
  //修改欄位名和資料型別
alter table test1 change name names varchar(50);

  alter table 表名 modify 欄位 資料型別 after 已存在的欄位/first
  //欄位修改位置
alter table test1 modify name varchar(50) after id;

增加:
  alter table 表名 add  新欄位名 新資料型別 (約束條件‘如:not null’)(位置‘first’);
  //增加欄位
alter table test1 add age int(20);

  alter table 表名 add  新欄位名 新資料型別  (約束條件) after 已存在的欄位;
  //將新加的欄位放在某個已存在的欄位後
alter table test1 add places VARCHAR(20) after id;

刪除:
  alter table 表名 drop 欄位名;
  //刪除已存在的欄位名
alter table test1 drop places;