数据库sql语句各常用操作

时间:2021-11-27 00:00:16

直接用select重复的值会被选取多次,这个问题可以用distinct解决:

select distinct company from orders,这样就只会列出一次

以逆字母顺序显示公司名称,并以数字顺序显示顺序号:

select company,ordernumber from orders order by company desc,ordernumber asc

insert into persons values('gates','bill','xuanwumen 10','beijing')
在指定的列中插入数据:

insert into persons(lastname,address) values('wilson','champs-elysees')

update person set firstname = 'fred' where lastname = 'wilson'

update person set address = 'zhongshan 23',city = 'nanjing' where lastname='wilson'

delete from person where lastname = 'wilson'
删除表的所有行,但是表的结构,属性和索引都是完整的:
delete from person

delete * from person

truncate table person删除表中的所有数据

select * from persons limit 5
select * from persons where rownum <= 5
select top 2 * from persons
选取前50%:
select top 50 percent * from persons

select * from persons where city like 'n%'
选取不包含‘lon'的人:
select * from persons where city not like '%lon%'
选取名字的第一个字符之后是’eorge'的人:
select * from persons where firstname like '_eorge'
名字以c开头,然后是任意字符,然后是r,然后是任意字符,然后是er
select * from persons where firstname like 'c_r_er'
居住城市以A或L或N开头的人:
select * from persons where city like '[ALN]%'
不以ALN开头的人:
select * from persons where city like '[!ALN%]'

选取姓氏为adams和carter的人:

select * from persons where lastname in ('adams','carter')

以字母顺序介于‘adams’和'carter'之间的人:
select * from persons where lastname between 'adams' and 'carter'
选取范围以外的人:

select * from persons where lastname not between 'adams' and 'carter'

select persons.lastname,orders.orderno from persons
inner join orders
on persons.id_p = ordres.id_p group by persons.lastname



UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。
union用于合并两个或多个select的结果集:
union选取不同的值,允许重复的话使用union all:
select e_name from employees_china
union (all)

select e_name from employees_usa

into:从一个表中选择某些数据插入另一个表中,也可以用于备份:
select lastname into persons_backup from persons
向另一个数据库中拷贝集:
select * into persons in 'backup.mdb' from persons
select person.lastname,orders.orderno into persons_order_backup
from person inner join orders on persons.id_p = orders.id_p

create database my_db


varchar(size):可变长度的字符串
char(size):固定长度的字符串
主键必须包含唯一的值,不能包含null值,每个表只能有一个主键,且为persons创建

与orders连接的外键:

create table persons(id_p int not null,
                     lastname varchar(255),
                     city varchar(255)
                      primary key(id_p)
                      foreign key(id_p) references orders(id_p))
create table persons(id_p int primary key,
                     lastname varchar(255),
                     city varchar(255))
check用于限制列中的值的范围:
create table persons(id_p int not null check(id_p>0),
                     lastname varchar(255),
                     city varchar(255))

DEFAULT 约束用于向列中插入默认值。如果没有规定其他的值,那么会将默认值添加到所有的新记录。


alter column city drop default
alter table persons
add unique(id_p)
alter table persons
drop index uc_personid
alter table persons
drop constraint uc_personid
alter table persons
add primary key(id_p)
为多个列定义peimary key约束:
alter table persons

add constraint pk_personid primary key (id_p,lastname))

create index personindex on person(lastname)
create index personindex on person(lastname desc)

create index personindex on person(lastname,firstname)



alter table table_name drop index index_name

drop table table_name

drop database db_name

添加列:
alter table persons
add birthday date
删除列:
alter teble table_name
drop column column_name
改变列的数据类型:
alter table persons
alter column birthday year

drop column birthday

where无法与合计函数一起使用,需要使用having:
select customer,sum(orderprice) from ordres
group by customer having sum(orderprice)<2000
ucase把字段的值转换成大写(lcase):
select ucase(lastname) as lastname from persons


select now() from table_name

以上均参考自:http://www.w3school.com.cn/sql/sql_func_mid.asp