#建表时指定 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;