Mysql使用
操作环境
Ubuntu 24.04
Mysql 数据库安装
WSL中安装
以root用户登录并修改root用户密码
执行以下语句,强制以root用户登录
修改密码:基本操作
创建数据库
查询当前所有数据库的名称
创建数据表
创建样例
create table instructor(
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key(ID),
foreign key(dept_name) references department
on delete cascade | set null | restrict | set default
on update cascade | set null | restrict | set default,
);
插入数据
查询数据
查询样例
select '437' as FOO
/* Results is a table with one column and a single row with value '437'*/
select 'A' from instructor
/*Result is a table with one column and N rows(number of tuples in the instructors table), each row with value 'A'*/
select ID,name, salary/12 from instructor
/* would return a relation that is the same as the instructor relation, except thaht the value of the attribute salary is divided by 12.*/
select distinct dept_name from instructor
/* find the department name of all insturctors, and remove duplicates*/
select all dept_name from intructor
/* The keyword all specifies that duplicates should not be removed */
删除字段
建立索引
删除索引
建立视图
删除视图
删除数据
进阶操作
集合运算
(select course_id from section where sem='Fall' and year = 2017) union (select course_id from section where sem='Spring' and year = 2018)
/* Find courses that ran in Fall 2017 or in Spring 2017*/
(select course_id from section where sem ='Fall' and year = 2017) intersect (select course_id from section where sem='Spring' and year =2018)
/*Find courses that ran in Fall 2017 and in Spring 2018*/
(select course_id from section where sem = 'Fall' and year = 2017) except (select course_id from section where sem='Spring' and year = 2018)
/* Find courses that ran in Fall 2017 but not in Spring 2018*/
union (all)
select distinct course_id from section where semester = 'Fall' and year = 2017 and course_id in\not in(select course_id from section where semester= 'Spring' and year = 2018);
select name from instructor where salary > some/all (select salary from instructor where dept_name = 'Biology')
exits 判断集合是否有值
unique 判断集合的值是否唯一
字符串运算
- percent (%). The % character matches any substring.
- underscore (_). The _ character matches any character.
select name from instructor where name like '%dar%'
/* Find the names of all instructors whoes name includes the substring 'dar' */
like '100 \%' escape '\'
/*Match the string '100%'*/
'Intro%' /* matches any string beginning with 'Intro' */
'___' /*matches any string of exactly three characters.*/
'___%' /* matches any string of at least three characters*/
重命名
select distinct T.name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.'
分类COUNT
跨表查询
排序
添加约束
添加主键
添加外键(级联删除和级联更新)
ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES referenced_table_name(referenced_column_name) ON DELETE CASCADE ON UPDATE CASCADE;
添加内容约束
添加触发器
定义结束符,用于命令行的程序书写
定义触发器
CREATE TRIGGER trigger_name BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
DECLARE var_name TYPE;
IF condition THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'message';
END IF;
END$$
恢复分号结束符
权限控制
创建用户
给予权限
撤销权限
内置函数
- avg\min\max\sum\count
- group by