当前位置:网站首页>数据库之存储
数据库之存储
2022-08-07 08:58:00 【weixin_51808099】
学生表:Student (Sno, Sname, Ssex , Sage, Sdept)
学号,姓名,性别,年龄,所在系 Sno为主键课程表:Course (Cno, Cname,)
课程号,课程名 Cno为主键学生选课表:SC (Sno, Cno, Score)
学号,课程号,成绩 Sno,Cno为主键
1.用SQL语句创建学生表student,定义主键,姓名不能重名,性别只能输入男或女,所在系的默认值是 “计算机”。
mysql> create table student (sno varchar(20) unique primaryy key,sname varchar(20),ssex enum("男","女"),sage int(10),sdept varchar(20) default "计算机");
2.修改student 表中年龄(age)字段属性,数据类型由int 改变为smallint。
mysql> alter table student modify sage smallint;
mysql> desc student;
+-------+-------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------+------+-----+-----------+-------+
| sno | varchar(20) | NO | PRI | NULL | |
| sname | varchar(20) | YES | | NULL | |
| ssex | enum('男','女') | YES | | NULL | |
| sage | smallint | YES | | NULL | |
| sdept | varchar(20) | YES | | 计算机 | |
+-------+-------------------+------+-----+-----------+-------+
5 rows in set (0.00 sec)
3.为SC表建立按学号(sno)和课程号(cno)组合的升序的主键索引,索引名为SC_INDEX 。
mysql> create table sc(sno varchar(20),cno varchar(20),grade int(10),primary key(sno,cno),index sc_index(sno,cno asc));
4.创建一视图 stu_info,查询全体学生的姓名,性别,课程名,成绩。
mysql> create view stu_info as select sname,ssex,sdept,grade from sc,student where sc.sno=student.sno;
mysql> desc stu_info;
+-------+-------------------+------+-----+-----------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------------+------+-----+-----------+-------+
| sname | varchar(20) | YES | | NULL | |
| ssex | enum('男','女') | YES | | NULL | |
| sdept | varchar(20) | YES | | 计算机 | |
| grade | int | YES | | NULL | |
+-------+-------------------+------+-----+-----------+-------+
4 rows in set (0.00 sec)
创建表并插入数据
字段名 数据类型 主键 外键 非空 唯一 自增
id int 是 否 是 是 否
name VARCHAR(50) 否 否 是 否 否
glass VARCHAR(50) 否 否 是 否 否
sch 表内容
id name glass
1 xiaommg glass 1
2 xiaojun glass 2
mysql> create table sch (id int primary key unique,name varchar(50) unique not null,glass varchar(50) unique not null);
mysql> desc sch;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(50) | NO | UNI | NULL | |
| glass | varchar(50) | NO | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> insert into sch values(1,"xiaommg","glass 1");
mysql> insert into sch values(2,"xiaojun","glass 2");
1、创建一个可以统计表格内记录条数的存储函数 ,函数名为count_sch()
mysql> delimiter $$
mysql> create function count_schh()
returns int
deterministiic
begin declare a int default 0;
select count(*) into a frrom sch;
return a;
end$
创建一个存储过程avg_sai,有3个参数,分别是deptno,job,接收平均工资,功能查询emp表dept为30,job为销售员的平均工资。
create procedure avg_sal(in p1 int,in p2 varchar(50),out avg_salary float)
begin
select avg(sal) into avg_salary from emp
where deptno =p1 and job = p2;
end/
call avg_sal(30,'销售员',@a)/
select @a/
边栏推荐
- 背包理论之01背包(滚动数组)
- LeetCode #100. 相同的树
- JVM:(五)运行时数据区之虚拟机栈
- (一)UPF之电源网络(Supply_port、Supply_net、Supply_set)
- 随笔-那些快乐的日子
- The PG function generates the corresponding table creation statement according to the table name
- Swap sort (bubble sort, quick sort)
- RestTemplate
- The principle and source code of redis - transaction mechanism
- Scala——While和do..While循环控制
猜你喜欢

ABP 6.0.0-rc.1的新特性

SRM系统是什么?有什么作用?企业如何应用SRM系统?

Principle of redis and source - redis data types of coding formats and data structures, a list, dict, SDS zskiplist, intset, ziplist, quicklist, listpack, rax, stream

【正点原子STM32连载】第五章 STM32基础知识入门 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1

In-depth analysis of Spark SQL illustrates the execution process and application scenarios of five Join strategies

ABP 6.0.0-rc.1的新特性

STM32出现波特率不对问题,我们要设定的波特率是9600,而实际的波特率竟然是14400,这是为什么呢?

window.requestAnimationFrame Web3D rendering frame rate control

LVS+Keepalived高可用群集部署

【Go】(1)go语言开发环境配置
随机推荐
ABP 6.0.0-rc.1的新特性
Insertion sort (direct, binary)
隐式类型转换问题(c语言)
What do you think of Douyin's China Video Partner Program?
jenkins配置自动打包
ABP 6.0.0-rc.1的新特性
E-commerce data warehouse notes 1 (data warehouse concept, project requirements and architecture design, data generation module)
双向链表的增删查改
Lecture: 494. Goals and
Binary search (search interval is left closed and right open)
The principle and source code of redis - transaction mechanism
C 学生管理系统_读取文件中得学生信息
【正点原子STM32连载】第七章 认识HAL库 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
字符串的相关操作以及小案例
LeetCode #94.二叉树的中序遍历
[Go] (1) Go language development environment configuration
选择排序(简单选择排序和堆排序)
一种API写法
今日睡眠质量记录74分
数据库连接池commons-pool源码分析