• SQL语句-中级


     一、Mysql软件使用

    1.启动/停止Mysql服务器

    任务管理器

    cmd命令:以管理员的身份打开cmd命令行

    1. net start mysql80//开启
    2. net stop mysql80//停止

    2.连接与断开Mysql服务器

    注意要在bin目录下执行:-u用户名root,-p密码

    mysql -u root -p

    可能出现的问题:

    配置环境变量-右键此电脑——>点击属性——>高级设置——>系统属性——>用户变量新建——>复制Mysql的bin目录地址即可

     另一个是我遇到的,推荐这个博主文章:关于Can't connect to MySQL server on 'localhost:3306' (10061)问题-http://t.csdnimg.cn/gjGFK

    退出:

    quit;

     二、数据库与表结构操作

    1.对数据库的操作

    数据库的创建:

    1. create database test;
    2. create schema test1;//与上一句都是创建数据库
    3. create database test2
    4. character set=gbk;//创建时可设置编码格式-汉字编码
    5. create database if not exists test;//创建时可先进行判断

    修改数据库:

    1. alter database 数据库名
    2. default character set gbk//修改字符集编码为gbk
    3. default collate gbk_chinese_ci;//修改字符集校对规则编码为gbk,字符集编码必须要一致

    删除数据库:

     drop database 数据库名;

    选择数据库:

    use 数据库名;

    2.数据类型

    数据库中的数据类型对应C语言中的数据类型

    1. tinyint---1个字节:-128~127unsigned-0~255
    2. smallint---2个字节:
    3. int---4个字节:
    4. bigint---8个字节:相当于long
    5. float(5,2)---4个字节:表示共5个数字长度,有2个小数位
    6. unsigned表示无符号类型,即正数
    7. char-定长字符类型,长度固定;varchar-变长字符串类型,按实际长度存储
    8. tinytext--存储短文本字符串
    9. date---年月日YYYY-MM-DD;time---时分秒HH:MM:SS
    10. datetime---date+time

    3.表结构操作

    1. alter table t_table1 add detail tinytext ;-- 添加新属性/字段
    2. alter table t_table1 modify detail varchar(20) ; -- 修改属性类型/字段
    3. alter table t_table1 change detail details varchar(20) ; -- 修改属性名/字段
    4. alter table t_table1 drop column details;-- 删除字段
    5. rename table t_table1 to table1;-- 修改表名
    6. drop table if exists test;-- 删除表

    三、多表操作

    1.多表设计

    一对多:部门与员工

    1. create table labor(
    2. id int unsigned primary key auto_increment,
    3. username varchar(20) not null,
    4. gender tinyint unsigned not null,
    5. departure_id int unsigned,
    6. create_time datetime not null) comment '员工表';
    7. create table departure(
    8. id int unsigned primary key auto_increment,
    9. name varchar(10) not null unique,
    10. create_time datetime not null
    11. ) comment '部门表';
    12. //对已建表添加联系/约束:一个部门对应多个员工
    13. alter table labor add constraint c1(约束名可任意取) foreign key(departure_id) references departure(id);
    14. //创建表时添加联系/约束
    15. create table managers(
    16. id int unsigned primary key auto_increment,
    17. name varchar(20),
    18. departure_id int unsigned,
    19. labor_id int unsigned,
    20. constraint m1 foreign key(departure_id) references departure(id)) comment '部门经理';
    21. //删除约束
    22. alter table managers drop constraint c1(约束名);

    一对一:用户与身份证-将单表进行拆分 

    1. create table labor(
    2. id int unsigned primary key auto_increment,
    3. username varchar(20) not null,
    4. gender tinyint unsigned not null,
    5. departure_id int unsigned,
    6. create_time datetime not null) comment '员工表';
    7. create table labor_card(
    8. id int unsigned primary key auto_increment,
    9. birthday date not null,
    10. idcard char(18) not null,
    11. labor_id int unsigned not null unique,//一定要加unique,否则不能实现一对一
    12. constraint l1 foreign key (labor_id) references labor(id)
    13. ) comment '员工身份信息表';

    多对多:学生与课程-借助中间表实现,分别关联两方主键

    1. create table stu(
    2. id int auto_increment primary key,
    3. name varchar(20),
    4. num varchar(10)
    5. ) comment '学生表';
    6. insert into stu(name,num) values('湘城','20210133'),('源思','20210522'),('砂峮','20211344'),('信肆','20212345');
    7. create table course(
    8. id int auto_increment primary key,
    9. name varchar(20)
    10. ) comment '课程表';
    11. insert into course(name) values('C'),('PHP'),('Java');
    12. create table stu_cour(
    13. id int auto_increment primary key,
    14. stu_id int not null,
    15. cour_id int not null,
    16. constraint s1 foreign key (stu_id) references stu(id),
    17. constraint s2 foreign key (cour_id) references course(id)
    18. ) comment '中间表';
    19. insert into stu_cour(stu_id,cour_id) values(1,2),(1,1),(2,2),(2,3);

    2.多表查询

    1. //查询所有
    2. select * from stu,course;-- 笛卡尔积:A、B所有组合情况 ,包括重复记录
    3. //内连接查询
    4. select * from stu,course where stu.id=course.id; -- 隐式内连接查询:消除重复记录
    5. select stu.name,course.name from stu,course where stu.id=course.id; -- 查询特定数据
    6. select s.name,c.name from stu s,course c where s.id=c.id; -- 给表起别名
    7. select * from stu (inner) join course on stu.id=course.id -- 显式内连接查询-inner可省略
    8. //外连接查询
    9. select * from stu left join course on stu.id=course.id; -- 左外连接:查询stu表的所有数据及其对应course数据
    10. select * from stu right join course on stu.id=course.id; -- 右外连接:查询course表的所有数据及其对应stu表的数据
    11. //子查询:嵌套查询
    12. select * from stu where num>(select num from stu where name='湘城'); -- 单行单列
    13. select * from course where id!=(select id from course where name='C')
    14. select * from stu where name='源思' or num=20210133 -- 多行单列查询
    15. select * from stu where num in (20210133,20210522)
    16. select * from (select * from stu where num>20210522) t1,course where t1.id=course.id -- 多行多列,放在from后作为虚拟表进行查询

  • 相关阅读:
    陈志泊主编《数据库原理及应用教程第4版微课版》的实验题目参考答案实验4
    任务调度xxljob的使用记录
    利用Python处理DAX多条件替换
    Mysql索引分类及其使用实例
    windows下的volatility取证分析与讲解
    【Spring Cloud实战】消费者直接调用提供者(案例)
    【Java】<泛型>,在编译阶段约束操作的数据结构,并进行检查。
    基于Algolia实现网站全文搜索
    ARMv7系统寄存器
    java-net-php-python-ssm大学生交互自助平台查重PPT计算机毕业设计程序
  • 原文地址:https://blog.csdn.net/2301_76371717/article/details/133607205