当前位置:网站首页>Database hyperphone (I)
Database hyperphone (I)
2022-07-27 17:34:00 【Moon in bubble】
Catalog
1. How to modify a data table
Use alter table command
(1)add
alter table < Table name >
add < Column definition >|< Integrity constraint definition >
Example :
(1) Add column :
alter table S
add sn int(6)
(2) Add default value constraint
alter table S
add constraint DF_S_T default(2) for time
(3) Add primary key constraint
alter table S
add constraint PK_S_Id primary key(id)
(4) Add a unique key constraint
alter table S
add constraint UQ_S_Name unique(id)
(5) Add foreign key constraints
alter table SC
add constraint FK_SC_S foreign key(sno) references S(sno)
6) increase check constraint
alter table SC
add constraint Score_Chk check(score between 0 and 100)
Be careful :
- New columns added in this way are automatically filled NULL value , Therefore, you cannot specify NOT NULL constraint .
(2)alter
alter table < Table name >
alter column< Name >< data type >[null|not null]
Example :
(1) Widen the column to 12 character
alter table S
alter column sn nvarchar(12)
(2) Change data type
alter table TbStudent
alter column stuGender nchar(1)
(3) Modify constraint type
alter table TbStudent alter column stuPhone char(11) not null
Be careful :
The column name cannot be changed ;
You cannot change the definition of a column with a null value to NOT NULL constraint ;
If there is data in the column , Then the width of the column cannot be reduced , Also cannot change its data type ;
It can only be modified NULL/NOT NULL constraint , Other types of constraints must be deleted before modification , Then add the modified constraint definition again .
(3)drop
alter table < Table name >
dorp aonstraint < Constraint name >
alter table < Table name >
drop column < Name >
Example :
(1) Delete constraints
alter table S
drop constraint UQ_S_Name
(2) Delete column
alter table S
drop column dept
2. distinguish count、count(*)
count(*): Count the number of tuples in the table . You can't go heavy , Calculation NULL value
count([distinct] < Name >): Count the number of column values . You can get rid of it , Don't count NULL
Be careful :
- In library functions , except COUNT(*) Outside , Other functions are ignored in the calculation NULL value .
(1) Ask how many departments there are in the school .
SELECT COUNT(DISTINCT Dept) AS DeptNum FROM S
(2) Count the number of column values with achievements .
SELECT COUNT (Score) FROM SC
The result in the above example is 0 Students are also counted , No results ( It is null ) Do not calculate .
(3) Find out that the selected courses of the computer department are less than 3 The name of the student of the door 、 The number of elective courses , Including students who don't choose courses . It is required to sort in descending order according to the number of courses selected , If the number of courses selected is the same , Sort by name in ascending order
select sn,count(cno) The number of elective courses
from S left outer join SC on S.sno = SC.sno
where dept=" Department of Computer Science "
group by S.sno
having count(cno)<3
order by count(cno) desc sn asc
(4)S The male students in the table are from each age group ( exceed 50 people ) How many people are there , Sort in ascending order by the number of people , The same number is in descending order of age
select <font color='red'>age </font>,count(*) The number of # Notice the age Don't forget
from S
where sex=' male '
group by age
having count( * )> 50
order by count(*),age desc
The first (3) Questions cannot be used count( * ), Among the students who did not choose courses , Use count(*) Still calculate the number of courses , While using count(cno), Directly count the number of course numbers of each group , The number of courses selected can be calculated correctly
Also note ,count and group by Continuous time ,count The number of statistics is group by After grouping, the number of data in each group , Not the number of groups .
3.SQL The order in which statements are executed
stay SQLServer in 
The picture is drawn for the online boss
4. distinguish where、having And clear group by It's using
- GROUP BY Clause is grouped by column Must be a column name that exists in the table , Out of commission AS Column alias assigned by clause .
- Appoint GROUP BY when , In the selection list All columns in any non aggregate expression Should be included in
- WHERE Clause used Screening FROM The row data generated by the data source specified in clause .
- GROUP BY Clauses are used to justify WHERE After Clause filtering Results data for Grouping .
- HAVING Clause is used for After grouping And then filter the statistical results .
- HAVING Clause can use Three elements : constant , Aggregate functions ,GROUP BY The column name specified in clause ( Juejian )
1. Query the number of students in each department of computer department and information management department .
The first one is :#( correct )
SELECT Sdept, COUNT(*) FROM Student
GROUP BY Sdept
HAVING Sdept in ( ' Department of Computer Science ', ' Information management department ')
The second kind :#( correct )
SELECT sdept, COUNT (*) FROM Student
WHERE Sdept in ( ' Department of Computer Science ', ' Information management department ')
GROUP BY Sdept
2. Query the age of each department is less than or equal to 20 The number of students .#( correct )
SELECT sdept,COUNT (*) FROM Student
WHERE Sage <= 20
GROUP BY Sdept
SELECT Sdept, COUNT(*) FROM Student# ( error )
GROUP BY Sdept
HAVING Sage <= 20
The second reason for the mistake is Sage Not in the group by It appears that
Be careful :
- Filter criteria that can be applied before the grouping operation , stay WHERE Clause is more effective .
- stay HAVING The filter conditions specified in clause should be those that must be applied after the grouping operation .
- Put all the filter conditions that should be carried out before grouping in WHERE Clause instead of HAVING clause .
5. Commonly used data manipulation language and data definition language syntax
Modify data sheet
Alter table < Table name >
Add < Column definition > |< Integrity constraints >
Alter table < Table name >
Alter colum < Name >< data type > [null | not null]
Alter table < Table name >
drop constraint < Constraint name >|colum< Name >
Delete base table
Drop table < Table name >
Add data
Insert into < Table name > [(< Name 1>[,< Name 2>])]
Values (< value >) | Subquery
Modifying data
Update < Table name >
Set < Name >=< expression >[,< Name >=< expression >]
[where < Conditions >]
Delete data
Delete
From < Table name >
[where < Conditions >]
Create view
Create view < View name >[( View column name )[, ( View column name )]
As Query statement
Color memory card :
边栏推荐
- 密集光流提取dense_flow理解
- 每条你收藏的资讯背后,都离不开TA
- Gradient ring progress bar
- 高精度定时器
- High cost, difficult to implement, slow to take effect, what about open source security?
- Lichuang EDA - layout and inspection of schematic diagram (III)
- Mobile page layout
- 【obs】x264_encoder_encode 编码输出pts dts和 framesize
- 三表联查1
- [SAML SSO solution] Shanghai daoning brings you SAML for asp NET/SAML for ASP. Net core download, trial, tutorial
猜你喜欢

大厂们终于无法忍受“加一秒”了,微软谷歌Meta等公司提议废除闰秒

Flex flex flex box layout 2

记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析

Day 7 summary & homework

诸神黄昏,“猫抖快”告别大V时代

Global string object (function type) +math object

High cost, difficult to implement, slow to take effect, what about open source security?

Swift QQ授权登录 坑集

Understand the basic properties of BOM and DOM

步 IE 后尘,Firefox 的衰落成必然?
随机推荐
An analysis of CPU explosion of a smart logistics WCS system in.Net
App Crash收集和分析
General process of background management system permission setting
Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
【SAML SSO解决方案】上海道宁为您带来SAML for ASP.NET/SAML for ASP.NET Core下载、试用、教程
How to extract tables from PDF through C /vb.net
通过 FileUploader 的初始化,了解 SAP UI5 应用的 StaticArea 初始化逻辑
Introduction to Alibaba eagle eye system
Behind every piece of information you collect, you can't live without TA
helm安装kubevela完整Makefile脚本内容
2021-06-18 automatic assembly error in SSM project
在CRA创建的项目中使用@并让其识别@路径并给出路径提示
Microsoft silently donated $10000 to curl, which was not notified until half a year later
DDD(领域驱动设计)分层架构
后台管理系统 权限设置大致流程
如何通过C#/VB.NET从PDF中提取表格
Understanding service governance in distributed development
Understand the basic properties of BOM and DOM
Explain the pile of binary trees in detail
Gree "not cool": the giant lawsuit ended and was reduced by large dealers. Is it too late for the new battlefield of air conditioning?