Database basic operations: CRUD operations and inter-table

Software used: SQL Server Management Studio

The first step, create a table. Here named T1. And fill in a few data on the inside. Figure:
T1
Here Insert Picture Description
. A query
to query all:

select  * from T1;

Query by the conditions:

select * from T1 where name = ’老黄’;

Here is a man named Huang's query.
A column query: select name from T1;
herein indicates that the query name column of this table.

Second, add
a new row of data:

insert into T1(ID,name,phone,userid) values (‘6’,’老李’,’708’,’8’);

Herein represents a new ID = 6, name = Lao Li, phone = 708, userid = 8 data.

Three Delete
to delete the entire line statement:

delete from T1 where ID=1;

Here is the means to delete the entire line statement ID 1
do bulk deletion: delete from T1 where ID> 3 ;
here means to delete all the data ID is greater than 3

four. Modify the
values you want to modify modify table:

update T1  set  name=’老花’ where ID=1;

Here is the statement said it would ID 1 in the name Huang changed presbyopia. If you have to continue to change the statement we can continue to add set + to change the column names and values ​​behind the old flowers.

Fives. Contingency table query
First, let's create two tables T1, T2.
T1
Here Insert Picture Description
T2
Here Insert Picture Description
different note userid table T1 and T2 table of ID

1. The outer query

Left query:

select * from T1 left join T2 on T1.userid=T2.ID

Results are as follows:
Here Insert Picture Description
Right inquiry:

select * from T1 right join T2 on T1.userid=T2.ID

Here Insert Picture Description
Full query:

select * from T1 full join T2 on T1.userid=T2.ID

Here Insert Picture Description
2. intra-query

Only two appearances match the query data:

select * from T1 inner join T2 on T1.userid=T2.ID

Here Insert Picture Description
six. Advanced Search

T1
Here Insert Picture Description
T2
Here Insert Picture Description
I am here to set up two tables, one table T1 is a table T2. Now when the query requires the added row of data table T1. T2 is a table of gender (UserSex), how to do?
Take a look at the statement:

select * ,(select UserSex from T2 where T1.ID=T2.ID)as 'Sex' from T1;

Results are as follows:
Here Insert Picture Description
VI. Get ID

public int AddDetailList(MODEL.DetailList Mymodel)
        {

           if (Mymodel != null)
            {
                object i=DBHelper.ExecuteScalar(@"set NOCOUNT ON; insert into DetailList (VoucherID,ActivityID,CardNum,IsExchange,IsUse,Flag,IsDuiHuan) values ('" + Mymodel.VoucherID + "','" + Mymodel.ActivityID + "','" + Mymodel.CardNum + "','0201','0501','0','0901') ;SELECT SCOPE_IDENTITY() AS id FROM DetailList; SET NOCOUNT OFF;");
                if (i != null && i.ToString() != "" && i.ToString() != "0")
                {
                    return Convert.ToInt32(i.ToString());
                }
                else 
                {
                    return 0;
                }

           }
            else
            {
                return 0;
            }
        }

Get ID table structure as follows:
Here Insert Picture Description
and insert the data and the data ID obtained

SET NOCOUNT ON ;
 INSERT INTO MyGift (Gift,EndTime,Score,Picture,Flag) VALUES ('LIP','2018-09-09 00:12:00',20,'IMGURL','0');
 SELECT TOP 1 ID FROM (SELECT  SCOPE_IDENTITY() AS ID FROM MyGift) A;
 SET NOCOUNT OFF;

Guess you like

Origin blog.csdn.net/weixin_44003632/article/details/86637107