当前位置:网站首页>Basic SQL operations
Basic SQL operations
2022-07-24 10:06:00 【xingxg.】
Catalog
form
create database test
-- Create student table Student
create table Student(Sno char(12) primary key,
Sname char(8) unique,
Age smallint,
Sex char(2))
-- Create a student course selection table Sc
create table Sc(Sno char(12),
Cno char(4),
Grade smallint,
Primary key(Sno,Cno))
-- Create a curriculum Course
create table Course(Cno char(6),
Cname char(20) not null,
Teacher char(8))
-- Delete table Course
drop table Course
-- stay SC In the table , Establish foreign code constraints ,
--Sno It's the outside code , The reference table is Student(sno);
alter table Sc
add foreign key(Sno) references Student(Sno)
-- Add column
alter table Student
add Sdept char(8) not null
-- Delete column
alter table Student
drop column Age
-- Modify the column
alter table Student
alter column Sex char(4) not null
-- Add constraints
alter table Student
add unique(Sex)
-- Delete constraints
alter table Student
drop constraint UQ__Student__CA1E3C800519C6AF
-- stay student Create about attributes on the table Sno Unique index of
create unique index Stusno on Student(Sno)
-- Insert data into table
insert
into Student
values ('1', ' Zhang Tian ', ' male ', ' Computer ')
-- see Student List the information
select * from Student
-- If in student In the table to delete ’202001020101’ This record
-- Delete column 、 surface use drop , Delete tuples ( Specific information ) use delete
delete
from Student
where Sno = '202001020101'
-- to Student Table to add “ Admission time ” Column , The data type is date
alter table Student
add S_entrance date
-- Change the data type of age from character type to integer type
alter table Student
alter column Sage int
-- Add constraints
-- Add the constraint that the student number must take a unique value
alter table Student
add unique(Sno)View :
Treat the view as a table .
-- Create a view , Count the number of courses selected by each student , The student number is required to be output 、 The number of elective courses -- See the table , Easy to insert data select * from Student select * from Sc; go -- View name List name create view Num_course(Sno,Num_course) as select Sno,COUNT(*) from Sc group by Sno go -- Before students take courses , There must be a course , This is the function of outer code !!! -- Insert courses and two students' elective schedules insert into Course values ('1','os',' Teacher wang '), ('2','cs',' Miss Liu ') -- 2 Student No insert into Sc values ('202001020102','1',80),('202001020102','2',90) -- 3 Student No insert into Sc values ('202001020103','1',95) -- View view select * from Num_course -- Create a student course selection view , The student number is required to be output 、 full name 、 Course name go create view Course_Student( Student number , full name , Course name ) as select Student.Sno,Sname,Cname from Student,Sc,Course where Student.Sno = Sc.Sno and Sc.Cno = Course.Cno go
边栏推荐
- Uniapp calendar component
- MySQL 数据库 JDBC编程
- [STM32 learning] (17) STM32 realizes LCD12864 display serial implementation
- Implementation principle of acid in MySQL
- Spark Learning: using RDD API to implement inverted index
- 多表查询之子查询_单行单列情况
- Ask you to build a small program server
- Countdownlatch and join [concurrent programming]
- [STM32 learning] (22) STM32 realizes 360 degree rotary encoder
- The most complete solution for distributed transactions
猜你喜欢

Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack

缓冲区的概念真的理解么?带你揭开缓冲区的面纱~
![[C language] implementation of three versions of address book small project (including source code)](/img/3b/926001332ec05378de4c35dc28ed55.png)
[C language] implementation of three versions of address book small project (including source code)

What is the cloud native mid platform business architecture?

Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?

Dynamic programming -- a collection of stock problems

CRC Coding in C language

ASI-20220222-Implicit PendingIntent

How does ribbon get the default zoneawareloadbalancer?

An article takes you to understand the operation of C language files in simple terms
随机推荐
聚集日志服务器
JS 84*148=b6a8 how many decimal places can you make both sides equal
Embedded development: Tools - optimizing firmware using DRT
Is CITIC Securities a safe and reliable securities firm? How to open an account?
How to solve command 'xxx GCC' not found, but can be installed with:??
Getting started with identityserver4
Arduino drive Lora module master node
Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
Server load and CPU performance tuning
Raspberry Pie: serial port login does not display print information
error: field ‘XXX’ declared as a function
[STM32 learning] (4) press the key to control the flow light
Knapsack problem of dynamic programming -- three lectures on knapsack (01 knapsack, complete knapsack, multiple knapsack)
Home raiding II (leetcode-213)
Write a simple memo using localstorage
JMeter setting default startup Chinese
Uniapp calendar component
Raspberry Pie: the serial port has been unable to read the information sent by the upper computer
Wechat applet
AttributeError: module ‘sipbuild. api‘ has no attribute ‘prepare_ metadata_ for_ build_ wheel‘