SELECT * FROM DUAL
这是最基本语句,具体的要看你的要求了。
2. sql数据库有哪几种查询方式
--查询数据库里所有表名和字段名的语句
--SQL 查询所有表名:
SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'
SELECT * FROM INFORMATION_SCHEMA.TABLES
--查询表的所有字段名:
SELECT NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID(' 表名' )
SELECT * FROM INFORMATION_SCHEMA.TABLES
SELECT * FROM INFORMATION_SCHEMA.VIEWS
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
---------以下为其他数据库查询表----------
--ORACLE 查看所有表名:
SELECT TABLE_NAME FROM USER_TABLES
--ACCESS 查看所有表名:
SELECT NAME FROM MSYSOBJECTS WHERE TYPE=1 AND FLAGS=0
--MSYSOBJECTS 是系统对象,默认情况是隐藏的。通过工具、选项、视图、显示、系统对象可以使之显示出来。
3. 如何根据sql数据库表中数据进行查询
sql server 查询一个表的所有信息:查询语法为:select * from 表名 语法中的”*“代表所有。
实际操作举例:某数据中有一表叫”user_user“,表中存储了一些用户信息;
1、查询”user_user“表中所有人员信息且所有字段,sql语句:select * from user_user
2、查询”user_use“表中所有人员信息且部分字段。sql语句:select user_show_id,user_name,pinyin,password from user_user
3、条件查询”user_user“表,以user_name为查询条件查询所有信息。sql语句:select * from user_user where user_name='李丹'
4、模糊查询”user_user“表,以user_name为模糊查询条件查询所有信息。sql语句:select * from user_user where user_name like '%张%'
4. 关于数据库SQL语句的查询
select
deptclass_three,count(*)
ty_grade_num
from
table
where
deptclass_two='校机关'
and
ty_grade='正厅级'
group
by
deptclass_three
q求出结果是校机关--正厅级中每个存在的deptclass_three所对应的人数,应该一个人一行吧。
5. SQL数据库 查询方法
--SQL 查询所有表名:
SELECT NAME FROM SYSOBJECTS WHERE TYPE='U'
SELECT * FROM INFORMATION_SCHEMA.TABLES
--查询表的所有字段名:
SELECT NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID(' 表名' )
SELECT * FROM INFORMATION_SCHEMA.TABLES
SELECT * FROM INFORMATION_SCHEMA.VIEWS
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
---------以下为其他数据库查询表----------
--ORACLE 查看所有表名:
SELECT TABLE_NAME FROM USER_TABLES
--ACCESS 查看所有表名:
SELECT NAME FROM MSYSOBJECTS WHERE TYPE=1 AND FLAGS=0
--MSYSOBJECTS 是系统对象,默认情况是隐藏的。通过工具、选项、视图、显示、系统对象可以使之显示出来。
6. 如何在sql数据库中查询数据
貌似不能查把 因为有可能你要查的值在几个表中都有
7. SQL数据库的、外键和查询
增加外键
创建表的时候增加外键:在所有的表字段之后,使用foreign key(外键字段) references 外部表(主键字段)
在新增表之后增加外键:修改表结构,使用alter table 表名 add [constraint 外键名字] foreign key(外键字段) references 父表(主键字段);
修改外键&删除外键
alter table 表名 drop foreign key 外键名;
外键条件
外键要存在,首先必须保证表的存储引擎是innodb
列类型必须与父表的主键类型一致
一张表中的外键名字不能重复
增加外键的字段数据已经存在,必须保证数据与父表主键要求对应
外键约束
有三种约束模式
district:严格模式(默认的)
cascade:级联模式
set null:置空模式
语法:foreign key(外键字段) references 父表(主键字段) on delete 模式 on update 模式;
联合查询
基本语法:
select 语句1
union [union 选项]
select 语句2……
union 选项
all:保留所有,不管重复
distinct:去重,默认的
子查询(sub query)
按位置分类
from子查询
where子查询
exists子查询
按结果分类
标量子查询
列子查询
行子查询
表子查询
子查询
列子查询
=any等价于in; -- 其中一个即可
any等价于some; -- 二者是一样的
=all为全部
-- 创建外键
create table my_foreign1(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id',
-- 增加外键
foreign key(c_id)references
my_class(id)
)charset utf8;
-- 创建表
create table my_foreign2(
idint primary key auto_increment,
name varchar (20)not null comment
'学生姓名',
c_idint comment'班级id' -- 普通字段
)charset utf8;
-- 增加外键
alter table my_foreign2add
-- 指定外键的名字
constraint student_class_1 -- 可以指定多个外键 但是名字不能相同
-- 指定外键的字段
foreign key(c_id)
-- 引用父表主键
references my_class(id);
-- 删除外键
alter table my_foreign1drop
foreign key my_foreign1_ibfk_1; -- my_foreign1_ibfk_1 通过外键的名字来删
-- 插入数据;外键字段在父表不存在
insert into my_foreign2values (
null,'郭富城',4); -- 没有4号班级
insert into my_foreign2values (
null,'项羽',1);
insert into my_foreign2values (
null,'刘邦',2);
insert into my_foreign2values (
null,'韩信',3);
-- 更新父表的记录
update my_classset id=4 where id=1; -- 失败;id=1记录已经被学生引用
update my_foreign2set c_id=2 where id=4; -- 更新
update my_classset id=4 where id=3; -- 可以;没有学生引用此班级
-- mysql中添加外键约束遇到一下情况:
-- cannot add foreign key constraint
-- 出现这个问题的原因是,外键的使用:
-- 1. 外键字段不能为该表的主键;
-- 2. 外键字段参考字段必须为参考表的主键
-- 插入数据
insert into my_foreign1values (
null,'马超','3'
);
-- 增加外键
alter table my_foreign1add
foreign key(c_id)references
my_class(id); -- 失败;因为没有3号班了
-- 创建外键,指定模式;删除置空;更新级联
create table my_foreign3(
idint primary key auto_increment,
name varchar (20)not null,
c_idint,
-- 增加外键
foreign key (c_id)
-- 引用表
references my_class(id)
-- 指定删除模式
on delete set null
-- 指定更新模式
on update cascade
)charset utf8;
-- 插入数据
insert into my_foreign3values (
null,'刘备',1),
(null,'曹操',1),
(null,'孙权',1),
(null,'祝贺量',2),
(null,'周瑜',2);
-- 解除My_foreign2表的外键
alter table my_foreign2drop
foreign key student_class_1;
-- 更新父表主键
update my_classset id=3 where id=1;
-- 删除父表主键
delete from my_classwhere id=2;
-- 联合查询
select * from my_class
union -- 默认去重
select * from my_class;
select * from my_class
union all -- 不去重
select * from my_class;
select id,c_name,roomfrom my_class
union all -- 不去重
select name,number,idfrom my_student;
-- 需求;男生升序;女生降序(年龄)
(select * from my_student
where sex='男'
order by ageasc limit9999999)
union
(select * from my_student
where sex='女'
order by agedesc limit9999999);
select * from my_studentwhere
c_id=(
-- 标量子查询
select idfrom my_classwhere
c_name='python1903');-- id一定只有一个值(一行一列)
insert into my_classvalues (1,
'python1907','B407');
-- 列子查询
select * from my_studentwhere
c_idin(select idfrom my_class);
-- any,some,all
select * from my_studentwhere
c_id=any(select idfrom my_class);
select * from my_studentwhere
c_id=some(select idfrom my_class);
select * from my_studentwhere
c_id=all(select idfrom my_class);
select * from my_studentwhere
c_id!=any(select idfrom my_class); -- 所有结果(null除外)
select * from my_studentwhere
c_id!=some(select idfrom my_class); -- 所有结果(null除外)
select * from my_studentwhere
c_id!=all(select idfrom my_class); -- 所有2号班级(null除外)
select * from my_studentwhere
age=(select max(age)from
my_student)
and
height=(select max(height))from
my_student);
-- 行子查询
select * from my_student
-- (age,height)称之内为行元素
where (age,height)=(select max(
age),max(height)from my_student);
update my_studentset height=188
where name='王五';
select * from my_studentorder by
agedesc,heightdesc limit1;
select * from my_studentorder by
heightdesc;
-- 表子查询
select * from my_studentgroup by
c_idorder by heightdesc; -- 每个班选出第一个学生再按身高排序
select * from (select * from
my_studentorder by heightdesc)
as studentgroup by student.c_id;
8. SQL数据库,如何查询数据库内含有某一列(某字段,如name)的所有表
SQL数据库,查询包含列(字段,如名称)的数据库中的所有表的步骤如下:需要准备的材料是:计算机,sql finder。
1,首先,打开sql查询器并连接到相应的数据连接,例如测试库。
9. SQL数据库查询
实现方法:
SQL> create table a (
2 bm char(4), --编码
3 mc varchar2(20) --名称
4 )
5 /
表已建立:
SQL> insert into a values('1111','1111');
SQL> insert into a values('1112','1111');
SQL> insert into a values('1113','1111');
SQL> insert into a values('1114','1111');
SQL> insert into a values('1115','1111');
SQL> create table b as select * from a where 1=2;
表已建立:
SQL> insert into b values('1111','1111');
SQL> insert into b values('1112','1111');
SQL> insert into b values('1113','1111');
SQL> insert into b values('1114','1111');
SQL> commit;
完全提交:
SQL> select * from a;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111
1115 1111
SQL> select * from b;
BM MC
---- --------------------
1111 1111
1112 1111
1113 1111
1114 1111
方法一
exists子句:
SQL> delete from a where exists (select 'X' from b where a.bm=b.bm and a.mc=b.mc);
删除4个记录。
where条件:如果两个表中都拥有相同字段的主键(primary key),则只需比较两个主键就可以了。
方法二
in子句:
SQL> delete from a where (bm,mc) in (select bm,mc from b);
删除4个记录.
SQL> select * from a;
BM MC
---- --------------------
1115 1111
实际测试结论
在表不是很大时,用in子句速度还可以忍受,而如果记录量很多时(十万条以上),in子句速度很慢。