当前位置:网站首页>Database addition, deletion, modification and query

Database addition, deletion, modification and query

2022-07-06 07:34:00 xinyubabyya

-- increase
-- grammar :insert into Table name values( The column value )

insert into  tb_stu(sname,ssex) values(' Green tea sister ',' Woman ')
insert into  tb_stu values(' Zhang San ',' male ',38,' Man Village ',' I like mushroom cooling in sister village ')
-- Batch insert
insert into  tb_stu
select ' Li Si ',' male ',28,' Man Village ',' I like mushroom cooling in sister village ' union
select ' Li Si 2',' male ',48,' Man Village ',' I like mushroom cooling in sister village ' union
select ' Li Si 3',' male ',58,' Man Village ',' I like mushroom cooling in sister village ' 
-- Inquire about
select * from  tb_stu
-- Query the information of female students
select * from  tb_stu where ssex=' Woman '
-- Look up the student number 、 full name 、 Age  
select sid,sname,sage from tb_stu
---- Look up the student number 、 full name 、 Age   Take the alias
select sid Student number ,sname as full name , Age =sage from tb_stu
-- Inquire about the information of students whose surname is Zheng who live in sister village
select * from tb_stu where sname like ' zheng %' and saddress=' Sister village '
-- Query the student information whose second word in the name is sister
select  * from  tb_stu  where sname like '_ The elder sister %'
-- Query the student information with sister characters in the address
select  * from  tb_stu where saddress  like '% Sister %'
-- Age 18 To the age of 38 Information about students between the ages
select  * from  tb_stu where sage>=18 and sage<=38
select * from tb_stu where sage between 18 and 38
-- The student ID is 1,3,5,7,8 Student information
select * from  tb_stu where sid=1 or sid=3 or sid=5 or sid=7 or sid=8
select  * from tb_stu where sid not in(1,3,5,7,8)
-- Arrange in descending order according to age  order by asc  desc
select * from tb_stu order by sage desc
-- Query the information of students with the top three student numbers
select top 3 * from tb_stu order by sid 
-- Query the information of students with the last three student numbers
select top 3 * from tb_stu order by sid  desc
-- Query half the data in the table
select top 50 percent * from tb_stu
-- modify
--update Table name set Name = Modified value where Conditions
-- Will age 18 The name of the year-old who lives in sister village is changed to " Little sister "
update tb_stu set sname =' Little sister ' where sage =18 and saddress=' Sister village '
-- Change the age of Wang Xiaomei to 38   Change the address to “ Sister village ”
update tb_stu set sage = 38 , saddress =' Sister village ' where sname=' Little sister Wang '
-- Increase the age of all 2 year
update tb_stu set sage = 12 where sage is null
update tb_stu set sage-=2  where sage =150
update tb_stu set sage+=2
select * from  tb_stu

-- Delete
-- grammar : delete  from Table name where Conditions
-- Delete student ID as 3 Student information
delete from tb_stu where sid =3
-- Delete the girls who live in the sister village again
delete tb_stu where saddress=' Sister village ' and ssex=' Woman '
-- Delete all information in the grade sheet
delete tb_score
-- Delete all data in the student table
delete tb_stu
-- Delete the student table structure
drop table tb_stu
drop table tb_score
-- Delete database
drop database db_t267

-- Add a message to the user table ( Zhao Mazi ,123456, male ,22, Super administrator )
insert into tb_users values(' Zhao Mazi ',123456,' male ',22,' Super administrator ')

-- Add a batch of room information in batch
--(1001,2,199,2,2,45, Full
--1002,3,399,2,0,45, The vacancy
--1003,2,199,2,1,45, Not fully occupied
--1004,4,599,4,0,45, The vacancy )
insert into tb_kefang
select 1001,2,199,2,2,45,' Full 'union
select 1002,3,399,2,0,45,' The vacancy ' union
select 1003,2,199,2,1,45,' Not fully occupied ' union
select 1004,4,599,4,0,45,' The vacancy ' 

-- Query user name 、 password
select uname ,upwd from tb_users
-- Query Andy Lau's password
select uname from tb_users where upwd=' Lau Andy '
-- Query age is greater than 28 Year old user
select * from tb_users where uage<=28
-- The age of inquiry is 38 To 50 Users between years old full name
select  * from  tb_users where uage>=38 and uage<=50
-- In descending order of age
select * from tb_users order by uage desc
-- Query the user surnamed Zhang
select * from tb_users where uname like ' Zhang %'
-- Query users whose gender is female
select * from tb_users where usex=' Woman '
-- The second word in the query name is ‘ Come on ’ Users of
select  * from  tb_users  where uname like '_ Come on %'
-- Check the information of full rooms
select * from tb_kefang where fkzrs=fkzrs
-- Query the vacant room information
select * from tb_kefang where fkzrs>=0 and fyzrs=0
-- Check the information of the room that is not full
select * from tb_kefang where fkzrs>fyzrs
-- Check the room price in 100 To 300 Room information between
select * from tb_kefang where fprice>=100 and fprice<=300
-- I live in 8301 Username
select dname from tb_dengji where dfid=8301
-- The guest room list is arranged in descending order according to the number of stays
select * from tb_kefang order by fcnt desc
-- Check the information of the rooms with the top three occupancy times
select top 3 * from tb_users order by uid 
-- The registration form is in ascending order of check-in time
select * from tb_dengji order by drz
-- There are wifi Room information
select * from tb_kefang where fmenu like 'wifi%'
-- Check the room information without TV
select * from tb_kefang where fmenu not like ' The TV %'

-- Change Andy Lau's password to 123456
update tb_users set upwd=123456 where uname=' Lau Andy '
-- Change the gender of Zhang San to male , And change the age to 88
update tb_users set usex=' male ', uage=88 where uname=' Zhang San '
-- Delete user No 3 User information for
delete from tb_users where uid =3
-- Delete all data in the user table
delete tb_users
-- Delete the user's table structure
drop table tb_users

One 、 Database table Introduction :

Number

Table name

English description

1

tb_type

Room type table

2

tb_kefang

Guest room list

3

tb_dengji

a registration form

4

tb_users

User table

Two 、 Data table structure :

1、tb_users User table

Field name

English description

Field type

Field size

Is it empty

Other

uid

The user id

int

false

Primary key , Identity column

uname

user name

varchar

20

False

upwd

password

varchar

16

False

The default value is 888888

usex

Gender

varchar

2

False

Value “ male ” or “ Woman ”

uage

Age

int

False

The value is greater than 0 Less than 150

usf

identity

varchar

20

False

2、tb_type Customer type table

Field name

English description

Field type

Field size

Is it empty

Other

tid

Type number

varchar

20

false

Primary key , Identity column

tname

Type name

varchar

20

False

tmenu

remarks

varchar

30

true

3、tb_kefang Guest room list

Field name

English description

Field type

Field size

Is it empty

Other

fid

Room number

int

false

Primary key ( It consists of floor and room number )

tid

Type number

int

false

Foreign keys ( Room type tid)

fprice

housing price

float

false

fkzrs

The number of people who can live

int

false

fyzrs

Number of residents

int

flase

The default value is 0

fcnt

Occupancy statistics

int

false

The default value is 0

fmenu

remarks

varchar

100

false

4、tb_dengji a registration form

Field name

English description

Field type

Field size

Is it empty

Other

did

Number

int

false

Primary key

dname

Customer name

varchar

20

false

dfid

Room number

int

false

Foreign keys ( Guest room list fid)

dsfz

ID number

varchar

18

false

dyj

The deposit

float

false

drz

Check in time

datetime

false

Default to current date

dtf

check-out time

datetime

true

-- Computer operation 1

--2、 Query all users
select * from tb_users
--3、 Query all records in the room type table
select * from tb_type

--4、 Check the information of all rooms
select * from tb_kefang

--5、 View registration form
select * from tb_dengji

--6、 View the user names and passwords of all users in the user table
select uname as user name ,upwd as password from tb_users;
--7、 Check the status of the guest room ( That is, only query the room number and status of the room table )
select fid as Room number ,fyzrs as Number of residents from tb_kefang;

--8、 Ward round : Check the registration form for all people who live in 101 The customer
select * from tb_kefang  where fid=101

--9、 Find all the empty rooms in the guest room table
select * from tb_kefang where fyzrs = 0;

--10、 Query all check-in statistics greater than 100 Room number of times
select * from tb_kefang where fcnt > 100

--11、 Inquire how many days Sheraton stayed in the hotel

--12、 Check all the full rooms , But the field name is required to use Chinese alias
select * from tb_type
select * from tb_kefang where tid=100 and fkzrs-fyzrs=0

--13、 Query all 2018 year 5 Customers who check in in this month
select * from tb_dengji where MONTH(drz)=5

--14、 The guest room list is in descending order according to the number of occupancy
select * from tb_kefang order by fcnt desc

--15、 The customer list is in descending order according to the number of occupancy , But only the top three
select top 3 * from tb_kefang order by fcnt desc;

--16、 The registration form is arranged in ascending order according to the check-in time
select * from tb_dengji order by drz asc;

--17、 The registration form is in ascending order of room number and descending order of check-in time
select * from tb_dengji order by dfid asc,drz desc;

--18、 Query Andy Lau's login password
select uname,upwd from tb_users where uname = ' Lau Andy '

--19、 Check all rooms with TV ( Tips : use like)
select fid from tb_kefang where fmenu like '% TV %'


-- Computer operation 2
--1、 Check all customers surnamed Zhang ( Tips :left())
select * from tb_users where LEFT(uname,1) = ' Zhang '

--2、 Check out all the check-out records ( Tips : is not null)
select * from tb_dengji where dtf is not null 

--3、 Query the names of all customers , Not including last name (substring())
select SUBSTRING(uname,2,LEN(uname)) from tb_users

--4、 Change the names of all customers into similar “ Zhang x ” Re display , That is, only the last name , Name is replaced by
select uid,REPLACE(uname,SUBSTRING(uname,2,LEN(uname)),' So-and-so ')as uname,upwd,usex,uage,usf from tb_users;

--5、 Check the registration form ,4 Monthly check-in customers (month( ))
select * from tb_dengji where MONTH(drz)=4

--6、 Check all check-in times less than 2007 Registration record of
select * from tb_dengji where YEAR(drz)<=2018

--7、 Check the occupancy of all the rooms on the first floor in the customer table (convert, left)
select * from tb_kefang where LEFT(fid,1)=1

--8、 Check the deposit at 200 To 500 Between customers
select * from tb_dengji where dyj between 200 and 500

--9、 Check the occupancy of deluxe rooms
select * from tb_type
select * from tb_kefang where tid=103

--10、 Check all maintenance rooms
select * from tb_kefang where fmenu like '% maintenance %'

--11、 Sort by house price
select * from tb_kefang order by fprice 

--12、 Check all rooms where one person can stay
select * from tb_kefang  where fkzrs-fyzrs = 1;

--13、 Inquiry can also live in three people
select * from tb_type
select * from tb_kefang where tid=102 and fkzrs-fyzrs>0

--14、 Check all available rooms , That is, under filled rooms and empty rooms
select * from tb_kefang where  fkzrs-fyzrs>0


-- Homework three :
--1、 Create student information table with script , Include column names :              
 --StudentNo( Student number ),
        --       LoginPwd( password ),
       --        StudentName( The student's name ),
       --        Sex( Student gender ),
       --        GradeId( grade ),
       --        Phone( Phone number ),
      --         Address( Address ),
      --        BornDate( Birthday ),
     --          Email( mailbox )
create table tb_stu(
    StudentNo int primary key, 
    LoginPwd int not null,
    StudentName varchar(20) not null,
    Ssex varchar(4) not null, 
    GradeId varchar(20) not null,
    Phone int not null,
    SAddress varchar(max) not null,
    BornDate date not null,
    Email varchar(200)
)

--2、 Write a script to insert 5 Analog data
insert into tb_stu
select 1,123,' Lei Jiaxin ',' male ',' Third grade ',686886,' LongQin Bay community Yanlian ',20021010,'[email protected]'
select 2,345,' Lei Xinyu ',' Woman ',' Third grade ',886668,' LongQin Bay community Yanlian ',20020118,'[email protected]'
select 3,567,' Zhang San ',' male ',' In grade one ',525722,' Qingshui, LongQin Bay community ',19980510,'[email protected]'
select 4,789,' Li Si ',' Woman ',' second grade ',812532,' Qingshui, LongQin Bay community ',20001124,'[email protected]'
select 5,890,' Wang Wu ',' male ',' Third grade ',278633,' LongQin Bay community Yanlian ',19990421,'[email protected]'

--3、 Create student address book , Containing fields :
--SName,Phone,Address,Email, And insert data
create table tb_tel(
      sname varchar(10),
      phone varchar(10),
      saddress varchar(20),
      email varchar(20)
     
      ) 

insert into tb_tel 
select ' Lei Jiaxin ','520',' LongQin Bay ','[email protected]'  union
select ' Lei Xinyu ','521',' LongQin Bay ','[email protected]'  union
select ' Lei kaichao ','666',' LongQin Bay ','[email protected]'

--4、 Write chart of accounts , Containing fields :cno( Number ),SubjectName( Subject name ),ClassHour( Class hours ),GradeId( grade )
create table tb_subject(
      cno int,
      SubjectName varchar(10),
      ClassHour varchar(10),
      GradeId int
     
      ) 
insert into tb_subject
select  1,'lxy',' mathematics ',18  union
select  2,'ljx',' English ',11  union
select  3,'lkc',' Chinese language and literature ',11
--5、 Set up a grade sheet , Containing fields :StudentNo,SubjectId( Account No ),StudentResult( Student achievement ),
--ExamDate( Date of establishment )

-- And insert multiple pieces of data at the same time
create table tb_score(
 StudentNo int,
      SubjectId int,
      StudentResult int,
      ExamDate date
     
      ) 

insert into tb_score 
select  ' mathematics ',88,20201222 union
select  ' English ',54,20210326  union
select  ' Chinese language and literature ',90,20220110
--6、 Change the gender of the student named Zhang Yang in the student information form to male
update tb_stu set sex=' male ' where StudentName=' Publicize '
--7、 Establish constraints in the student information table , The gender can only be male or female
 

product surface (Production

product Number

product name

production date

P#

Pname

Pdate

1

product A

2020/01/02

2

product B

2019/04/02

3

product C

2020/03/02

sales Personnel list (Sales

sales Personnel number

sales Name of person

S#

Sname

1

Zhao Yi

2

The king 2

3

Zhang San

4

Li Si

Sales volume surface (Deal

sales Personnel number

product Number

sales The amount

S#

P#

Volume

1

1

20

1

2

36

1

3

55

2

2

24

2

3

56

2

1

44

3

2

11

3

3

77

4

1

36

4

2

91

4

3

22

-- problem :
--1、 Query Zhang San 19 Annual sales ( Use left join keyword ).
select * from tb_sales
left join tb_deal on tb_sales.s#=tb_deal.s#
left join tb_production on tb_deal.p#=tb_production.p#
where Sname=' Zhang San ' and Pdate like '2019%';
--2、 Query the sales volume of products with production date in the first quarter .
select
t2.p#,t2.pname, SUM(volume) Total sales ,t2.pdate
from tb_deal t1
left join tb_production t2 on t1.p#=t2.p#
where t2.Pdate like '2020%'
GROUP BY t2.p#,t2.pname,t2.pdate;
--3、 Query the sales volume of the products with the highest sales volume ( Use group by keyword ).
select a.p#,a.pname,max(a.sum1)
from(
select
t1.p#,t1.pname,sum(volume) as sum1
from tb_production t1
left join tb_deal t2 on t1.p#=t2.p#
group by t1.p#
) a;

原网站

版权声明
本文为[xinyubabyya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131916453176.html