create database aaa
drop database aaa
--结构drop
--内容delete
--列名 数据类型 [完整性约束]
--1、列名,命名规则
--2、数据类型:text char(8000) nchar varchar nvarcha
int bigint smallint tinyint
money float,numeric(20,3) ,decimal(10,5)
datetime smalldatetime
binary(20),bit,imag
--3\约束:实体:primary key
参照:foreingn key
自己定义:not null
unique
check
default
create table emp
(eno char(10) primary key,
ename varchar(30) not null unique,
eage tinyint check(eage<100),
eworktime datetime
)
DDL:create drop
DML:insert
--1、insert into
insert into 表名(列名1,列名2,..)
values(值1,值2。。)
select * from stu
select * from sc
insert into
stu
values('006','puppy',21,'m','wlx')
insert into sc
values('002','c01',70)
--2 select:表、行、列
select 列
from 表
where 条件(行)
一、select后列的操作
1、所有列
select *
from stu
2、部分列
select sname,sno,ssex
from stu
3、重新命名列
select sname as name,sno,ssex
from stu
select sname as 姓名,sno 学号
from stu
4 、列的计算
一、字符类型的列的连接 +
select sno+sname as xuehaoxingming
from stu
select Rtrim(sno)+sname as xuehaoxingming
from stu
--字符串的操作
select '2009'+sno newsno
from stu
--常量列
select '2009'
from stu
二、数值类型的计算 + —— * /
select 2012-sage as birthyear
from stu
三、日期时间类型的计算,getdate(),year(),month,day()
select year(getdate())-sage birthyear
from stu
4、消除重复列
select distinct sdept
from stu
--where后的筛选条件,and or not
select *
from stu
where sname like '%j__'
1、比较>,<,>=,<=,<>,!=
2、闭区间 between and 的等价关系
not between and
3、模糊比较 like
作用范围:用于字符类型和日期时间类型,
凡是加引号的
%:代表任意多个字符
_:代表任意单个字符
4、空值的比较 is null / is not null
select *
from stu
where sdept is not null and sage>20
and sname like '_i%'
--from 后表的连接
--查询所有学生的姓名和成绩
select stu.sno
from (stu inner join sc on stu.sno=sc.sno)
select *
from stu,sc
where stu.sno=sc.sno
select 列
from 表
where 条件
SQL语句1——单表查询
最新推荐文章于 2023-10-17 12:46:20 发布