Mysql数据库的基本操作 创建表
• 查看数据库 show databases;
• 创建数据库 create database 数据库名
• 删除数据库 drop database 数据库名
• 创建表 use 数据库名
Create table 表名( );
• mysql 中的完整性约束条件
• primary key 主键 foreign key 外键 not null 非空 unique 唯一的 auto_increment 自动增加 default 指定默认值
• 单字段主键
Create tableexample (stu_id int primary key);
• 多字段主键
Create tableexample1(stu_id int,
Stu_id int,
Course_id int
Primary key(stu_id,Course_id));
• 设置表的外键
Constraint 外键别名 froeign key (属性1 。。。。。。。)references 表名( );
如:
Create tableexample2(id int primary key ,
Stu_id int,
Course_id int
Constraint c_fxforeign key (stu_id,Course_id) references examlp1(stu_id, Course_id ));//c_fx是外键别名
Mysql数据库基本操作 修改表
• 修改表名:
Alter table systemrename user;
• 修改字段数据类型
Alter table systemmodify 属性名 数据类型
• 修改字段名
Alter table systemchange 旧属性名 新属性名 新数据类型
• 增加字段
Alter table systemadd 属性名 数据类型 完整性约束条件 [ 指定字段添加位置],(默认是添加到尾部,加入frist关键字添加到头部),
在表的指定位置之后添加字段
Alter table systemadd address varchar(30) not null after phone;
上述语句表示在phone字段的尾部添加一个名为address的字段
• 删除字段
Alter table systemdrop 属性名
• 修改字段的排列位置
Alter table systemmodify 属性名1 数据类型 first|after 属性名2
如:修改字段到第一个位置
Alter table systemmodify studentname varchar(30) first;
修改字段到指定位置
Alter table systemmodify studentname varcharL(30) after add;
上述语句表示将字段studnetname 的位置移动至add字段的后面
数据库的索引
索引是一种特殊的数据结构,可用来快速的来查询数据库表中的特定记录,Msql中所有的数据类型都可以被索引。
索引是创建在表上的,是对数据库表中一列或多列的值进行排序的结构,索引可以提高查询的速度。
创建索引
1. 创建表时可以直接创建索引,
Create table 表名(属性名数据类型 [完整约束条件]
………………………………………………
[unique|fulltext|spatial|index|key][别名] (属性名1 [长度][asc|desc]));
其中 unique表示索引为唯一索引 ,fulltext 表示全文索引 ,spatial 表示为空间索引,index和key 表示指定字段索引,asc表示升序排列,desc表示降序排列。
2. 创建普通索引:
Createtable index(id int ,
Name varchar(20),
Sex bool,
Index(id));
上述语句表示在表id字段上创建索引
查看表的另一种方法:
Show create table (表名)\G
Describe (表名);
3.创建唯一索引
Createtable index2(id int unique,
Name varchar(20),
Unique index index2_id(idasc));
上述语句代表示在表中id字段上建立名为index2_id的唯一索引,并以升序的方式进行排列,
4.创建全文索引
注:全文索引只能创建在 char varchar 或 text 类型的字段上,并且只有MyISAm存储引擎支持全文索引.
其语法为 :
Create table index3(id int ,namevarchar(20),
Fulltext indexindex3_name(name))engine=MyiSAM;
上述语句表示在表中name字段上创建了全文索引.
5.创建单列索引
Createtable index4(id int,subject varchar(30),index index4_st(subject(10)));
上述语句表示了在字段subject上创建了名为index4_st的单列索引,
Index4_st中索引的长度为10这样做的原因是加快查询速度.(对字符类型有用)。
5.创建多列索引
Createtable index4(id int,name varchar(20),sex char(4),index index5_ns(name,sex));
上述语句表示了在字段name,sex创建了名为index4_ns的多列索引
多列索引只有在查询条件中使用了这些字段,索引才会被使用
6.空间索引(在默认存储引擎上不可使用).
7.在已经存在的表上创建索引
在已经存在的表上创建索引的基本语法是:
Create[unique|fulltext|spatal]index 索引名 on 表名(属性名 [(长度)] [asc|desc]);
1. 创建普通索引
Create indexstudent_id on student(id ASC);
上述语句表示在student 的表的id字段中创建了名为student_id的普通索引
2. 创建唯一索引
Create nuiqueindex student_id on student(id desc);
上述语句表示在student的表的id 字段中创建了名为student_id的唯一索引
使用alter table 语句创建索引
其基本形式为:
Altertable 表名 add [unique|fulltext|spatal|] index 索引名(属性名 [长度] [asc desc]);
删除索引
索引的删除的基语法是:
Drop index 索引名 on 表名。
数据库视图
视图是从一个表或多个表中导出来的表,是一种虚拟存在的表,视图就像一个窗口,通过这个窗口可以看到系统专门提供的数据.
1. 创建视图
创建视图的语法为 : create [algorithm={undefined|merge|temptable}]
View 视图名 [(属性清单)] as select 语句 [with[cascaded|local] check option];
其中 algorithm表示视图所选择的算法,视图名表示要创建的视图名称,属性清单为可选参数指定了视图中各个属性的名词,默认情况下与select语句中查询的属性相同,select 语句参数是一个完整的查询语句,表示从表中查出满足条件的记录,将这些记录导入视图中:with check option 表示更新视图时要保证该视图在权限范围内。
(undefined,merge,temptable)表示不同的算法。
2. 在单表上创建视图
Create view department_view1 as select *from department;
上述代码完成了在department(部门)表上创建的简单视图名为:department_view1视图
如图示:
结果显示表 index1_view1 的属性与index1表的结果一样,因为上述语句没有指定视图的属性列表,所以视图的属性名与select语句查询的属性一样。
3. 创建一个视图名为:index1_view2的视图
语法为:
Create view index1_view2(a,id,name)as select a,id,name from(index1);
如图示:
此语句加入属性清单,实现了对视图的筛选.
4. 在多表上创建视图
在多表上创建视图同样也是使用create view 语句
例:
Create view algorithm=merge viewworker_view1(name,address,sex) as select name,address,sex from deprartment,worker where worker.d_id=department.d_id with local check option;
查看视图
1.Describ 语句查询视图的基本信息
其基本语法为describe 视图名;
3. 使用 showtable status 语句查看视图的基本信息;
其基本语法为 show table statuslike ‘视图名’;
4. 使用showcreate view 语句查看视图的详细信息
其基本语法为 show createview worker_view\G
5. 在views表中查看视图的详细信息
在Mysql中所有的视图都存放在information_schema 数据库的views 表中 查询表views可以看到数据库中所有视图的基本信息。
修改视图
修改视图使用 create or replace view 语句修改视图 (replace更换)
该语句表示当视图不存在是就创建视图,视图存在时就将视图进行更改。
Create or replace view 的语法为: createor replace [algorithm ={undefined|merge|temptable}]
View视图名 [(属性清单)]] ac select 语句 [with local check option];这里个视图的创建是一样的。
Altre 语句修改视图
Alter 语句修改视图的语法为 :alter[algorithm={undefined|merge|temptable}] view 视图名
[(属性清单)] asselect 语句 [with local check option];
更新视图
更新视图指的是通过视图来(insert ,udpdate ,delete)表中的数据
删除视图
删除视图是指删除表中已经存在的视图,删除视图时只删除视图的定义,不会删除数据。
Mysql 中使用 drop语句来删除视图
器基本语法为:drop view if exists 视图列表
触发器(trigger)
触发器(trigger)是由事件来触发某个操作,这些事件包括 insert 语句 ,udpdate 语句和 delete语句当数据可执行这些事件时,就和激活触发器执行相应的操作。
创建触发器
1.Mysql 中创建只有一执行语句的触发器的基本形式为:
Create trigger 触发器名before|after 触发事件 on 表名 for each row 执行语句。
其中 before|after 指定了触发器的执行时间触发事件包括 (inster, udpdate,delete)
下面创建一insert触发器名为 dept_trg1;
Create trigger dept_trg1 before insert ondepartment for each row insert into trigger_time values(now());
触发器创建后当向department表插入数据时数据库系统会在insert语句之前向trigger_time表中插入当前时间
2. 创建执行多个语句的触发器
其基本语法为: create trigger 触发器名 before|after 触发事件 on 表名 for each row begin
执行语句表 end
查看触发器
查看触发器使用:show trigger 语句查看触发器
删除触发器
Drop trigger 触发器名
SQL 查询语句
1. 查询数据
基本查询语句 mysql 中的select 基本用法为:
Select 属性表
From 表名和视图列表
[where 条件表达式]
[group by 属性名1 [having 条件表达式2]]
[orderby 属性名2 [asc,desc]]