sql数据库基本操作

-- 创建student数据库
create database if not exists student;
use student;
show tables;
-- 创建学生信息表
create table if not exists `stuinfo`(`sid` int(11) unsigned primary key auto_increment,
`sname` varchar(20) not null,
`age` int(10),`sex` enum('男','女') default '男',`birth` datetime not null ,`email` varchar(20) not null,`address` varchar(20),`tel` int(11)
);
-- 创建课程表
create table if not exists course(
cid int(11) unsigned primary key auto_increment,
cname varchar(20) not null
);
-- 创建成绩表
create table if not exists sorce(
sid int(11) unsigned not null,
cid int(11) unsigned not null,
sorce int(3) not null,
primary key(sid,cid),
foreign key(sid) references stuinfo(sid) on delete cascade on update cascade,
foreign key(cid) references course(cid) on delete cascade on update cascade
)engine=innodb default charset=utf8;
-- 删除表
drop table stuinfo;
drop table course;
drop table sorce;
-- 删库
drop database student;
-- 修改表的结构
-- 查看表的结构
desc stuinfo
-- 增加一个字段
alter table stuinfo add beizhu varchar(50) comment '备注';
-- 删除一个字段
alter table stuinfo drop beizhu;
-- 修改一个字段
alter table stuinfo change beizhu bz varchar(50);
-- 修改字段属性
alter table stuinfo modify bz varchar(100);
alter table stuinfo modify tel varchar(11);
-- 增加记录
-- 增加stuinfo(学生信息)
insert into stuinfo(sname,age,sex,birth,email,address,tel,bz) values('zhangsan',20,'男','1999-1-1','123.com','上海','13211111110','nisp一级');
insert into stuinfo(sname,age,sex,birth,email,address,tel,bz) values('lisi',20,'男','1999-1-1','123.com','上海','13211111110','nisp一级');
-- 增加课程信息
insert into course(cname) values('nisp二级'),('cisp'),('cisp-pte');
-- 增加成绩信息
insert into sorce values(1,1,60),(1,2,80),(1,3,90),(2,1,50),(2,2,60),(2,3,70);
-- 删除一个学生记录
delete from stuinfo where sid=4;

-- 修改一个学生记录
update stuinfo set sname='张三' where sname='zhangsan';


-- 查询所有张字开头的
select sname from stuinfo where sname like '张%';
-- 查询年龄在20以上学生的信息
select sname,age from stuinfo where age>=20;
-- 查询家住在上海或者北京的学生的信息
select * from stuinfo where address='上海' or address='北京';
update stuinfo set address='北京' where sname='张三' and address='上海';
-- 没有留下邮箱的学生的信息(邮箱字段为空)
select * from stuinfo where email is null;
select * from stuinfo;
select * from sorce;
select * from course;


-- 查询所有用户
use mysql;
select user,host from user;
-- 增加用户
create user xinjian@'%' identified by '123';
-- 删除用户
drop user xinjian@'%';
-- 用户授权
grant all privileges on student.* to xinjian@'%' identified by '123' with grant option;
-- 查看用户权限赋予情况
show grants for xinjian@'%';
-- 回收用户的权限
revoke all privileges on student.* from xinjian@'%';

-- 服务器版本
select version();
-- 当前数据库库名
select database();
-- 当前用户名
select user();
-- 服务器状态
show status;
-- 服务器配置变量
show variables;
-- 查看数据文件存放位置
show global variables like '%datadir%';
-- 查询数据库的路径
select @@datadir;
-- 查询MySQL的安装路径
select @@basedir;
-- union查询详解,union查询默认去重如果不想去重就用union all
use student
create table temp1(uid int primary key auto_increment,uname varchar(20) not null);
create table temp2(eid int primary key auto_increment,age int not null );
insert into temp1(uname) values ('zs'),('ls'),('ww');
insert into temp2(age) values (11),(22),(33);
select * from temp2;
select * from temp1;
-- union联合查询
select * from temp1 union select * from temp2 ; -- 属性是第一个的属性
select * from temp1 union select 1; -- union联合注入可以报字段

-- 分组查询展示group_concat()
select * from stuinfo
select * from sorce;
select sid,group_concat(sname),group_concat(age),group_concat(tel) from stuinfo group by sid;
select sid,group_concat(cid),group_concat(sorce order by sorce desc separator' ') from sorce group by sid; 

-- 常用字符串函数
select lower('aSDasf')
select upper('adfa')
select concat('adf','13')
select concat_ws(';','asdf','234')
select substr('hello',2,2)
select length('士大夫')-- 返回字符串的存储长度
select length('afda')
select char_length('sff')-- 返回字符串的字符个数
select char_length('阿斯顿')
-- 查看是否允许将数据库导入导出,为null时是不允许
show variables like 'secure_file_priv'

在cmd命令行下导库

mysqldump -u root -p student>student.sql    导出mysql库
mysql -u root -p123.com tt<student.sql  导入库 


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值