#建表時指定 create table t2(id int(10) not null,name varchar(5)); #已存在的表增長約束 alter table t2 modify name varchar(5) not null; #能夠設置默認值,即爲非空 alter table t2 constraint test_id default (‘xxx’) for stuname; #取消 alter table t2 modify name varchar(5);
#建立的時候制定 create table t3(userid int(10) unique,name varchar(10)); #已存在的表增長 alter table t3 add constraint t3_id unique(id) #關鍵字增長惟一約束 alter table test4 add unique(id,name,age)
#建表時添加 create table test1(user_id int(10) primary key,name varchar(10)); #刪除約束 alter table test1 drop primary key; #以多列組合創立主鍵 create table t4 (id int,name varchar(255),primary key(id,name)); #已存在的表增長主鍵 alter table stu add constraint id primary key (id)
#建立表的時候 CREATE TABLE `students` ( `StuID` int(10) unsigned NOT NULL AUTO_INCREMENT, `Name` varchar(50) NOT NULL, PRIMARY KEY (`StuID`), KEY `hello_fk` (`Name`), CONSTRAINT `hello_fk` FOREIGN KEY (`Name`) REFERENCES `classes` (`Name`) ) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8 #已存在的表增長 alter table students add constraint mage_stu_class_fk foreign key(classid) references classes(classid);
#upper轉換爲大寫輸出 MariaDB [hellodb]> select upper(name) from teachers; #lower轉換爲小寫輸出 MariaDB [hellodb]> select lower(name) from teachers; #insert替換函數(列名,從第幾位開始,替換幾位,替換的內容) MariaDB [hellodb]> select name,insert(phone,4,4,'****') phone from students; #substr從第5位開始取,日後一共取3位 MariaDB [hellodb]> select substr(name,5,3) phone from students; #length顯示字段的長度 MariaDB [hellodb]> select length(name) phone from students; #CONCAT(s1,s2...sn) 字符串 s1,s2 等多個字符串合併爲一個字符串 MariaDB [hellodb]> SELECT CONCAT("hello ", "mariadb ", "mysql ", "orcale") AS ConcatenatedString; #FIELD(s,s1,s2...) 返回第一個字符串 s 在字符串列表(s1,s2...)中的位置 MariaDB [hellodb]> SELECT FIELD("c", "a", "b", "c", "d", "e"); #LEFT(s,n) 返回字符串 s 的前 n 個字符 MariaDB [hellodb]> SELECT LEFT('abcde',2); #REPEAT(s,n) 將字符串 s 重複 n 次 MariaDB [hellodb]> SELECT REPEAT('mariadb ',3); #SUBSTRING(s, start, length) 從字符串 s 的 start 位置截取長度爲 length 的子字符串 MariaDB [hellodb]> SELECT SUBSTRING("mariadb", 2, 3) AS ExtractString;
#顯示當前的時間 MariaDB [hellodb]> select now(); #DATEDIFF(d1,d2) 計算日期 d1->d2 之間相隔的天數 MariaDB [hellodb]> SELECT DATEDIFF('2001-03-01','2001-02-02'); #DATE_FORMAT(d,f) 按表達式 f的要求顯示日期 d MariaDB [hellodb]> SELECT DATE_FORMAT('2011-11-11 11:11:11','%Y-%m-%d %r'); #DAY(d) 返回日期值 d 的日期部分 MariaDB [hellodb]> SELECT DAY("2017-06-15"); #DAYNAME(d) 返回日期 d 是星期幾 MariaDB [hellodb]> SELECT DAYNAME('2011-11-11 11:11:11'); #DAYOFMONTH(d) 計算日期 d 是本月的第幾天 MariaDB [hellodb]> SELECT DAYOFMONTH('2011-11-11 11:11:11'); #WEEK(d) 計算日期 d 是本年的第幾個星期 MariaDB [hellodb]> SELECT WEEK('2011-11-11 11:11:11');
#取絕對值 MariaDB [hellodb]> select abs(-20); #取模 MariaDB [hellodb]> select mod(11,3); #取不小於X的最小整數 MariaDB [hellodb]> select ceil(9.2); #取不大於X的最大整數 MariaDB [hellodb]> select floor(3.6); #n DIV m 整除,n 爲被除數,m 爲除數 MariaDB [hellodb]> SELECT 10 DIV 5; #GREATEST(expr1, expr2, expr3, ...) 返回列表中的最大值 MariaDB [hellodb]> SELECT GREATEST(3, 12, 34, 8, 25); MariaDB [hellodb]> SELECT GREATEST("mysql", "mariadb", "linux");
#查看全部的函數數列 MariaDB [hellodb]> show function status\G; #建立無參的函數(在centos7支持,6不支持) MariaDB [test]> CREATE FUNCTION simple() RETURNS VARCHAR(20) RETURN "Hello World!"; #調用函數 MariaDB [test]> select simple(); #查看指定自定義函數的定義 MariaDB [mysql]> show create function simple\G; #建立帶參的函數 MariaDB [test]> delimiter // #爲了方便書寫把結束符從新定義 MariaDB [test]> create function addtwo(x int unsigned,y int unsigned) returns int begin declare a,b int unsigned; set a=x,b=y; return a+b; end// #刪除自定義函數 MariaDB [test]> drop function simpleFun;
#建立視圖 MariaDB [hellodb]> create view viewname as select * from teachers; #查詢指定視圖 MariaDB [hellodb]> select * from viewname; #查看全部的視圖信息 select * from information_schema.views\G; #查看視圖的結構 desc viewname #刪除視圖 MariaDB [hellodb]> drop view viewname;
不支持作視圖的語句mysql
使用視圖的好處linux
使用索引的優勢:sql
使用索引的缺點:數據庫
索引的使用原則:centos
#建立索引 MariaDB [m33student]> create index age_index on student(phone); #查看索引 MariaDB [m33student]> show indexes from student\G; #建立惟一索引 create table ti( id int not null, name char(30) not null, unique index uniqidx(id) );#對id字段使用了索引,而且索引名字爲UniqIdx #建立普通索引 create table book( bookid int not null, bookname varchar(255) not null, authors varchar(255) not null, info varchar(255) null , comment varchar(255) null, year YEAR not null, index(year) #對屬性年建立索引 ); #修改表結構(添加索引) MariaDB [hellodb]> alter table teachers add index index_name(name); #刪除索引 MariaDB [hellodb]> drop index [indexn_ame] on teachers; #顯示索引信息 MariaDB [hellodb]> show index from teachers\G;
觸發器是一個特殊的存儲過程,不一樣的是存儲過程要用來調用,而觸發器不須要調用,也不須要手工啓動,只要當一個預約義的事件發生的時候,就會被MYSQL自動調用。
簡單格式:安全
create trigger trigger_name trigger_time trigger_event on table_name trigger_stmt for each row(行級監視,mysql固定寫法,oracle不一樣) begin sql語句集........(觸發器執行動做,分號結尾) end;
trigger_name:觸發器的名稱
trigger_time:{ BEFORE | AFTER },表示在事件以前或以後觸發
trigger event::{ INSERT |UPDATE | DELETE },觸發的具體事件
trigger_stmt:觸發器程序體,能夠是一句SQL語句,或者用 BEGIN 和 END 包含的多條語句。
實驗:建立一個觸發器,制定一張表,對錶增長記錄的時候count加1,則反之oracle
#建立student_info表 MariaDB [hellodb]> create table student_info(stu_id int(11) primary key auto_increment,stu_name varchar(255) default null); #建立一張計數表 MariaDB [hellodb]> create table student_count( student_count int(11) default 0); #使計數表置爲0 MariaDB [hellodb]> insert into student_count values (0); #建立觸發器,給student_info表增長記錄,計數表加一 MariaDB [hellodb]> create trigger trigger_student_count_insert after insert on student_info for each row update student_count set student_count=student_count+1; #建立觸發器,給student_info表刪除記錄,計數表減一 MariaDB [hellodb]> create trigger trigger_student_count_delete after delete on student_info for each row update student_count set student_count=student_count-1; #開始測試,添加記錄 MariaDB [hellodb]> insert into student_info (stu_id,stu_name) values (123,'abc'); #會看到計數表爲1 MariaDB [hellodb]> select * from student_count; #開始測試,刪除記錄 MariaDB [hellodb]> delete from student_info where stu_id=123; #會看到計數表爲0 MariaDB [hellodb]> select * from student_count;
MariaDB [hellodb]> create user name@host identified by 'passwd'; #建立所有權限的用戶 MariaDB [hellodb]> grant all on *.* to name@host identified by 'passwd';
#第一種直接利用password函數修改 MariaDB [hellodb]>set password for name@'host'=password("newpasswd"); #第二種更新用戶表 MariaDB [hellodb]> update mysql.user password=password("passwd") where host ='localhost' #第三種關閉MySQL密碼驗證功能,而後第一種第二種 /etc/my.cnf 關閉密碼認證 skip_grant_tables
#給用戶受權使用庫,表,視圖 MariaDB [hellodb]> grant all on database.(database|table|view) to name@host ; #給用戶受權使用命令select,insert MariaDB [hellodb]> grant select,insert on database.(database|table|view) to name@host; #給用戶受權使用表或視圖裏的屬性 MariaDB [hellodb]> grant select(屬性) on database.(database|table|view) to name@host;
MariaDB [hellodb]>show grant for name@host;
MariaDB [hellodb]>revoke commod|all on database.(database|table|view) from name@host;