--创建一个数据库
create database TX
--创建dept表
create table dept
(deptno int primary key,--该表的主键
dname nvarchar(30),
loc nvarchar(30)
)
--创建emp表
create table emp(
empno int primary key,--该表的主键
ename nvarchar(30),
job nvarchar(30),
mgr int,
hiredate datetime,
sal numeric(10,2),
deptno int foreign key references dept(deptno)--外键
)
--针对外键,请注意:
--①外键只能指向主键
--②外键和主键的数据类型要一致
--③emp表外键的所有值,dept表的主键必须有否则不能匹配
--然后进行其他操作
查询列操作
select 字段名1,字段名2 from 表名 where 条件
取消某字段相同的行--distinct
select distinct 字段名1,字段名2 from 表名 where 条件
--该句含义:选择出某条件下,找出某表中字段名为1和2的两列,并且这两列相同的行只显示一行代表
时间的选择
select *from emp where hiredate>'1999-2-2'--emp表名,hiredate字段名,注意时间的书写格式
选择关键字 and、between(含有等于边界)、or的使用
操作符like(模糊查询)
in的使用
order by的使用 asc升序(默认,可不写),desc降序
复杂查询实例: