The detailed operation of the SQL statement (1)

SQL basic statement

  • create table

create table person(
	id int(10),
	lastname varchar(24),
	firstname varchar(24),
	address varchar(24),
	city varchar(24),
	primary key(id)
)

Insert picture description here

  • insert

insert into person values(1, 'Gates', 'Bill', 'Xuanwumen', 'Beijing');
insert into person values(2, 'Dell', 'Smith', 'Heping', 'Tianjin');
insert into person values(3, 'Tom', 'David', 'Haidian', 'Beijing');
insert into person(id, lastname, firstname) values(4, 'Will', 'John');

Insert picture description here

  • select

select * from person;
select lastname, firstname from peerson;

Insert picture description here
Insert picture description here

  • distinct

insert into person values(5, 'Han', 'David', 'Zhongyuan', 'Zhengzhou');
select firstname from person;
select distinct firstname from person;

Insert picture description here

  • where

select * from person where city = 'Beijing';

Insert picture description here
Insert picture description here

  • and&or

select * from person where address = 'Haidian' and city = 'Beijing';
select * from person where address = 'Heping' or firstname = 'David';

Insert picture description here

  • order by

select * from person order by lastname asc;
select * from person order by firstname desc;
select * from person order by city asc, address desc;

Insert picture description here
In the above result, there are two equal cities (Beijing). Only this time, when there is the same value in the first column, the second column is sorted in descending order. This is also the case if some values ​​in the first column are nulls.

  • update

update person set lastname = 'Bob' where id = 3;

Insert picture description here

  • delete

delete from person where id = 4;

Insert picture description here

delete from person
delete * from person
# 效果一样,但是带*的执行比不带*的快,可以试一下

Guess you like

Origin blog.csdn.net/qq_42647711/article/details/109126818