当前位置:网站首页>Simple operation of SQL server data table
Simple operation of SQL server data table
2022-07-29 08:21:00 【cloud_ yq】
1. Create table
create table Student information
(
-- Define the data format of the column of the table
Student number int not null,
full name char(20),
Gender char(4),
Age int,
class char(20),
departments char(30),
-- Add related constraints
constraint Gender check( Gender = ' male ' or Gender = ' Woman '), The data in the restricted gender column can only be male Woman
constraint Age check( Age between 0 and 50) The age limit can only be 0 To 50
)
2. Modify table
-- Add column
alter table Student information -- Specify the table to modify
add Place of instruction char(50) not null -- Add column Place of instruction
-- Modify the column
alter table Student information -- Specify the table to modify
alter column Place of instruction char(50) -- Modify the column ( Place of instruction ) Data format
-- Delete column
alter table Student information -- Specify the table to modify
drop column Place of instruction -- Delete column Place of instruction
-- Set the primary key of the data table
alter table Student information with nocheck
add constraint pk_num_id
primary key clustered( Student number )3. Data insertion
-- Add data to the created table
insert into Employee information ( Number , full name , Department number , Job number , Age , Current address )
values(1,' Zhang San ','001','1001',25,'A'),
(2,' Li Si ','002','2001',19,'S')
-- Copy table
insert into DataBack(tid,tname,tdid,tmid,tage,taddress) -- Choice and Employee information Tables with the same data format
select Number , full name , Department number , Job number , Age , Current address -- Select the corresponding column
from Employee information
-- Copy table
select Number , full name , Department number , Job number , Age , Current address
into new_datas
from Employee information
where Department number is not null and Job number is not null -- Copy Employee information Columns in the table where the department number and job number are not empty
4. Update of data in the table
-- Unconditional update
uodate Student information
set departments =' School of Internet of things engineering ' -- The Department column in the student information table has been changed to School of Internet of things engineering
-- Condition update
update Student information
set class =' class A '
where Student number < 14160150
-- Modify the table before n Data
update top(n) Student information
set class =' class A '5. Delete data from table
-- Delete all data in the table
delete from Employee information
-- Delete data in the table according to conditions
delete from Employee information
where Number <5
--truncate table Delete data in table
truncate table Student information -- Use truncate table Delete more than select It's a lot faster
6. Query the data in the table
-- Query all columns
select * from Student information -- Inquire about Student information All the columns in the table
-- Query partial Columns
select Student number , full name
from Student information -- Inquire about Student information In the table Student number , full name Two
-- Use alias when querying
select Student number as no, full name as name
from Student information -- Inquire about Student information In the table Student number , full name Two , And use no,name Express
-- Conditions of the query
select full name from Student information
where Student number between 1 and 4 -- Inquire about Student information In the table The student number is in 1 To 4 Name column between
-- Operator query
select * from Student information
where Gender = ' male ' or Student number > 2 and Age < 22 --or Represents or ,and Represents and And and The priority ratio or high
-- Fuzzy query
select * from Student information
where class like '% Connected to the Internet --' --% Indicates the location ( Front, middle and rear ) And the characters ( Variable quantity ),- Represents an unknown character
where class like ' The Internet of things [-]2' -- Represents a query The Internet of things -2 Class data [-] It is practical -
-- To repeat the record
selcet distinct( Number ), The unit price
from inventory -- take Number Put the same columns together
-- Before query n Bar record
select top 4 *
from Student information -- Before query 4 Bar record
-- Sort query information
select * from Student information
order by Student number
# 7. Link query
-- Multi table join query
select * from Student information , Class information
where Student information . class = Class information . class -- List all the data in the two tables that meet the constraints The result is shown in Fig. Fig3
-- Internal link query
select * from Student information inner join Class information
on Student information . class = Class information . class -- List the data in two tables that meet the constraints ( and where Restrictions are similar to ) The result is shown in Fig. Fig4
-- The left outer join
select * from Student information left join Class information
on Student information . class = Class information . class -- Show left table ( Student information ) All the information about , Fill in the blank on the right table The result is shown in Fig. Fig5
-- Right connection
select * from Student information right join Class information
on Student information . class = Class information . class -- Show right table ( Class information ) All the information about , Fill in the blank on the left table The result is shown in Fig. Fig6
-- Full outer join
select * from Student information full outer join Class information
on Student information . class = Class information . class -- Display all the information of the two tables , Fill in the blanks The result is shown in Fig. Fig7
-- Self connect query
select * from Class information as a1, Class information as a2
where a1. average >85 -- Self connection is a query that uses an alias to connect with itself The result is shown in Fig. Fig8
-- Cross query
select * from Student information cross join Class information
where Class information . average >85 -- Similar to self join query The result is shown in Fig. fig9
The experiment used two tables -- Student information (Fig1) And class information (Fig2)


Experimental results :Fig3-9







#8 The joint query
--UNION Statement query
select Number , name , Number , Warehousing date
from inventory
union
select Number , name , Number , Warehousing date
from Cargo information -- Merge the same parts of the two tables , Different parts show Pictured Fig3
--INTERSECT Statement query
select Number , name , Number , Warehousing date
from inventory
intersect
select Number , name , Number , Warehousing date
from Cargo information -- Show the parts of the two tables where the query columns are exactly the same Pictured Fig4
--EXCEPT Statement query
select Number , name , Number , Warehousing date
from inventory
except
select Number , name , Number , Warehousing date
from Cargo information -- Show the parts with different query columns in the two tables Pictured Fig5The experiment used two tables -- Cargo information (Fig1) And merchandise inventory (Fig2)


The experimental results are shown in the figure Fig3-5



#9. Subquery
--SELECT Sentence query
select
avg( Number ) as Average inventory quantity ,
(select avg( Number ) from Cargo information ) as Average quantity of goods ,
max( Number ) as Maximum quantity in stock ,
(select avg( Number ) from Cargo information ) as Maximum quantity of goods
from inventory -- Goods information query is embedded in commodity inventory query
--IN Statement query
select
Number , name , Number
from Cargo information
where Number in (select Number from inventory where Number <300)
-- Find out that the quantity in the goods information is equal to the quantity in the goods inventory and less than 300 The number of columns
--EXISTS Statement query
select
Number , name , Number
from Cargo information as a
where exists (select name from inventory where name =a. name )
--ANY Statement query
select
Number , name , Number
from Cargo information
where Number = any(select Number from inventory ) -- Display the data of the quantity in the goods information in the quantity set in the goods inventory
--
select
Number , name , Number
from Cargo information
where Number > all(select Number from inventory ) -- Display the data that the quantity in the goods information is greater than all the quantities in the commodity inventory
#10. Crosstab query
-- Use CASE Statement to realize crosstab query
select
(case name when ' rice ' then Number end) as The quantity of rice purchased each time ,
(case name when ' millet ' then Number end) as Each purchase quantity of Xiaomi ,
(case name when ' soybean ' then Number end) as The quantity of soybeans purchased each time
from inventory -- Search each row of names and extract the number of corresponding names to form a new table result Fig1
-- Use PIVOT Statement to realize crosstab query
select Warehousing date ,
a.[ rice ] as ' rice ',
a.[ millet ] as ' millet ',
a.[ soybean ] as ' soybean '
from inventory
pivot(sum( Number ) for name in([ rice ],[ millet ],[ soybean ])) as a
order by Warehousing date -- The result is shown in Fig. Fig2
The experiment uses a table inventory ( And the 9 The stanzas are the same )
Result chart :


边栏推荐
- Chapter contents of the romance of the Three Kingdoms
- 阿里巴巴政委体系-第一章、政委建在连队上
- Cluster usage specification
- Unity Shader学习(六)实现雷达扫描效果
- 阿里巴巴政委体系-第三章、阿里政委与文化对接
- DC motor control system based on DAC0832
- Deep learning (1): prediction of bank customer loss
- Domestic application of ft232 replacing gp232rl usb-rs232 converter chip
- Privacy is more secure in the era of digital RMB
- STM32 MDK (keil5) contents mismatch error summary
猜你喜欢

数字人民币时代隐私更安全

Clion+opencv+aruco+cmake configuration

Reading papers on false news detection (4): a novel self-learning semi supervised deep learning network to detect fake news on

Unity多人联机框架Mirro学习记录(一)

The first week of postgraduate freshman training: deep learning and pytorch Foundation

Inclination sensor accuracy calibration test

Tb6600+stm32f407 test

Deep learning (1): prediction of bank customer loss

110道 MySQL面试题及答案 (持续更新)
![[academic related] why can't many domestic scholars' AI papers be reproduced?](/img/1a/7b162741aa7ef09538355001bf45e7.png)
[academic related] why can't many domestic scholars' AI papers be reproduced?
随机推荐
(视频+图文)机器学习入门系列-第5章 机器学习实践
AES 双向加密解密工具
ML.NET相关资源整理
【Transformer】SegFormer:Simple and Efficient Design for Semantic Segmentation with Transformers
Ws2812b color lamp driver based on f407zgt6
分段分页以及段页结合
2.4G band wireless transceiver chip si24r1 summary answer
Simplefoc parameter adjustment 1-torque control
RPC和REST
[beauty of software engineering - column notes] 28 | what is the core competitiveness of software engineers? (next)
Collation of ml.net related resources
深度学习(1):银行客户流失预测
ROS tutorial (Xavier)
ROS common instructions
Low power Bluetooth 5.0 chip nrf52832-qfaa
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
Noise monitoring and sensing system
110道 MySQL面试题及答案 (持续更新)
[beauty of software engineering - column notes] 29 | automated testing: how to kill bugs in the cradle?
Simple calculator wechat applet project source code