SQL学习笔记(一)

SQL账号

- 老师的: -mysql -u guest -h 47.104.31.138 -p 密码: Guest.618
- 我的: -mysql -u root -p 密码:123456
- 云端数据库: 47.104.31.138 用户名:guest 密码:Guest.618   
- 登录: mysql -u root -p
- 链接本机: mysql -u root -h localhost -p

SQL —> 结构化查询语言

~ DDL(数据定义语言) ---> 创建删除各种对象 ---> create / drop / alter
~ DML(数据操作语言) ---> 插入、删除、修改数据 ---> insert / delete / update
~ DQL(数据查询语言) ---> 检索(查询)数据 ---> select  
~ DCL(数据控制语言) ---> 授予或者召回用户权限 ---> grant / revole
  • mysql 连接服务器 mysql -u root -p
SQL 是不区分大小写大的编程语言 —> create / CREATE
  1. 查看所有数据库

    show databases;

  2. 创建数据 create

    create database XX default charset utf8mb4;
    指定默认字符集为 utf8mb4

  3. 删除数据库 drop XX;

    drop database if exists school;

  4. 查看数据库的过程

    show create database school;

  5. 切换到指定的数据库

    use school;

  6. 显示数据中所有的表

    show tables;

  7. 创建二维表

    数据类型:

    • 整数: int / bigint / smallint / tinyint —> unsigned(无符号整数)
    • 小数:float / double / decimal
    • 时间类型:time / data / datetime / timestamp
    • 字符串: char(10) / varchar(20)
    • 大对象:longtext(字符) / longblob(二进制) —>单列可以方4G
create table tbl_student
 (
     stu_id integer not null comment '学号',
     stu_name varchar(20) not null comment '姓名',
     stu_sex boolean default 1 comment '性别',
     stu_birth date comment '出生日期',
     primary key(stu_id)    
 )engine=innodb comment '学生表';

 # 非空约束: not null 
 # default 默认值为1
 # 主键列(primary key): 能够唯一确定的一条记录的列  
 #  主键可以不加not null 要求 非空且不重复
  • 学生表<-----从属----->学院表

    (多)————————(一)

alter table tb_student add column cod_id int unsigned not null

alter table tb_student constraint fk_student_col_id foreign key (col_id) references tb_college(col_id);

  • alter table 语句用于在已有的表中添加、修改或删除列。
use school;

drop table if exists tb_student;

CREATE TABLE tbl_student (
    stu_id INTEGER NOT NULL COMMENT '学号',
    stu_name VARCHAR(20) NOT NULL COMMENT '姓名',
    stu_sex BOOLEAN DEFAULT 1 COMMENT '性别',
    stu_birth DATE COMMENT '出生日期',
    PRIMARY KEY (stu_id)
)  ENGINE=INNODB COMMENT '学生表';

-- 修改表
-- 添加一个列


alter table tbl_student add column stu_addr varchar(20) default '' comment '籍贯';

-- 删除一个列
alter table tbl_student drop column stu_addr;

-- 修改一个列(修改列的名字) change
alter table tbl_student change column stu_sex stu_gender boolean default 1 comment '性别';

-- 修改一个列(修改列的数据类型)modify
alter table tbl_student modify column stu_gender char(1) default '男';

-- 创建学院表tb_college
create table `tb_college`
(
 `col_id` int unsigned auto_increment comment '编号',
    `col_name` varchar(50) not null comment '名称',
    `col_intro` varchar(50) default '' comment '介绍',
    primary key(col_id)
)engine=InnoDB comment '学院表';

-- 修改学生表添加一个列来保护学生对学院的多对一关系
-- 多对一关系都是在多的一方添加一个列来维护

alter table tbl_student add column col_id int unsigned not null comment '学院编号';

-- 修改学生表添加一个外键约束,限制学生表中的学院编号必须参照学院表的学院编号
-- add constraint 约束 / foreign key 外键 / reference 参考
alter table tbl_student add constraint fk_student_col_id foreign key(col_id) references tb_college(col_id);


-- 老师表 老师属于学院
create table `tb_teachers`
(
 `teach_id` int unsigned auto_increment comment '编号',
    `teach_name` varchar(50) not null comment '名称',
    `teach_course_name` varchar(50) default '' comment '授课名称',
    primary key(teach_id)
)engine=InnoDB comment '老师表';

-- 课程表 老师可以开多个课程
create table `tb_course`
(
 `col_id` int unsigned auto_increment comment '编号',
    `col_name` varchar(50) not null comment '名称',
    `col_intro` varchar(50) default '' comment '介绍',
    primary key(col_id)
)engine=InnoDB comment '学院表';
-- 选课记录表 多对多关系
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值