设计数据库的3种完整性
1,实体完整性,不含重复记录,PK
2,引用完整性,FK
stu.deptno --> dept.deptno
foreign key(deptno) references dept(deptno)
3,自定义完整性
如:年龄大于等于0小于等于120
load data,关闭引用,索引,提高速度
show variables like '%foreign%'
---foreign_key_checks ON
set foreign_key_checks = off 关闭外建check检查
perror 查看MySQL错误
which perror
perror 39
mysql -e "create database ds"
mysql ds -e "select * from stu"
information_schema数据库
columns表
select table_schema, table_name , column_name from columns where table_schema='ds'
mysql数据库
user表
select user, password, host from user;
SQL语句
DDL,数据定义语言,create alter drop
DML,数据操作语言,insert delete update select
DCL,数据控制语言,grant deny revoke
完全限定
select user, password, host from mysql.user;
根据已有表创建
create table t1 like dept; 包含主建和索引
create table t2 select * from detp ; 同时导入了数据
改引擎
alter table stu engine = myisam;
alter table stu charset = gbk;
该表名
alter table t2 rename table3;
给已有表增加字段
alter table table3 add deptLeader varchar(32) not null;
删除字段
alter table table3 drop deptLeader;
修改字段
alter table table3 modify deptName varchar(64) default 'cc' not null;
增删主键
alter table table3 add primary key(deptID)
alter table drop primary key;
增删外键
alter table stu add foreign key(deptno) references dept(deptno);
alter table stu drop foreign key stu_ibfk_1; 需先知道外建的名字
改字段名
alter table table3 change deptName deptMz varchar(32) default 'jsj' not null;
同时添加多条记录
insert into dept(deptID) values(6),(7),(8),(9);
删除表中的数据,同时将auto_increment复原
truncate table stu;
update可以操作多张表
update dept d, stu s set s.age=s.age*1.1 where d.deptID=s.deptID and d.deptID = 'dba';
值的存储 @开头定义全局变量
select deptID into @a from dept where deptName = 'dba';
update stu set age = age*1.1 where deptID = @a;
delete不能操作多张表,可以用子查询或者全局变量
delete from stu where deptID = (select deptID from dept where deptName = 'dba');
select deptID into @a from dept where deptName = 'dba';
delete from stu where stu.deptID=@a;
select
select * from stu where deptNo is null;
select distinct deptNo from stu where deptNo is not null;
select * from stu order by score desc limit 3,3; 从第4条开始取3条
flush privileges; 刷新权限表
新建用户
create user testuser1@10.0.0.252 identified by 'aixocm';
select查询
多表查询
select stuName , courseName, score from student, course, sc
where student.stuID=sc.stuID && courseID=sc.courseID;
内连接 inner join, left join, right join
select stuName, deptName from stu inner join dept on stu.deptID=dept.deptID;
select stuName , courseName, score from student, course, sc
where student.stuID=sc.stuID && courseID=sc.courseID;
select stuName, courseName, score from student inner join sc on sc.stuID=student.stuID
inner join course on sc.courseID=course.courseID;
左外连接,会把左边表中的数据都显示出来,即使它并没有在右边表中出现
select stuName , courseName, score from student left join course on sc.stuentID=student.stuID
inner join course on sc.courseID=course.courseID;
子查询
select * from stu where deptID = (select deptID from dept where deptID='计算机与通信学院');
select deptID into @a from dept where deptID='计算机与通信学院';
select * from stu where deptID=@a;
记录联合
union
添加用户并赋权限 *.* 数据库.表
grant all on *.* to tom@'%' identified by 'aixocm';
“%”指所有的外部主机。
show grants for tom@'%';
回收权限
revoke insert on ds.* from tom@'%';
revoke all on *.* from tom@'%';
从外部连接MySQL
mysql -utom -paixocm -h10.0.0.252 -P3307
添加,删除用户
create user tom@'%';
drop user tom@'%';
阅读(1681) | 评论(0) | 转发(0) |