(二)sql基础语法总结

(二)sql基础语法总结

1. 创建表
create table 表名(列名 属性 约束)
create table table1(name char(11) parimary key not null)
2. 复制表
2.1 只复制表结构
create table 新表 like 已存在的表
create table copy_table like table
2.2 复制表结构和内容,且按照条件
create table 新表 select * from 已存在的表 where 限制条件
create table NewTable select * from OldTable where 条件
3. 删除表
drop table 表名
drop table table1
4. 插入语句
4.1 插入所以属性
insert into 表名 values (值,值,值,值…) 值个数和表列名个数一致
insert into orders values (3,current_timestamp,‘李’);
4.2 插入部分属性
Insert into 表名 (列名,列名) values (值,值) 注:列名和值需要匹配
insert into orders (O_ID,O_UNAME) values (5,‘Neusoft’);
4.3 批量插入
insert into 表名 (列名) values (属性值1),(属性值2)…
insert into orders2 (O_ID ,O_UNAME) VALUES (5,‘liyi’),(6,‘wangwu’)…
4.4 从相同表结构中复制插入现有数据中
insert into 表名 select * from 表名 where 条件
insert into copy_student select * from student where student.age>20
5. 更新操作
update 表名 set 列名=属性值 where 限制条件
update student set name=‘李四’ where sno=1
注意:当更新多个值的时,需要使用“,”来隔开,而不是用 and 。
UPDATE student set sex=‘男’ , name=‘李四’ WHERE sno=1

6. 删除操作
delete from 表名 where 条件
delete from student where sno=1
注意:truncate也是删除操作,清空数据和表结构,彻底删除,无法回滚。
delete 可以进行事务回滚,可以找回。
7. Alter操作
7.1 添加新的一列属性
alter table 表名 add column 列名 类型
alter table student add column hobby char(20)
7.2 删除某一列
alter table 表名 drop 列名
alter table student drop hobby
7.3 修改列的属性
alter table 表名 modify 列名 新的属性
alter table student modify id int(11)
7.4 修改列的名称
alter table 表名 change 旧的列名 新的列名 属性约束
alter table student change age stu_age int(2)
8. 添加约束
8.1 在表内添加外键
foreign key 字段 references 父表(唯一键)
foreign key sc_sno refences student(sno)
8.2 在表外添加外键
alter table 表名 add constraint 约束名 foreign key (外键字段) references 父表名(父表唯一键)
alter table student_course add constraint fk_sc_sno foreign key sc_sno references student(sno)
8.3 添加唯一约束
Alter table 表名 add constraint 约束名 约束(列名)
alter table student add constraint u_stu_phone unique(STU_PHONE);
9. 去重 关键字distinct
在需要去重的字段前加上关键字distinct即可。
注意:该字段必须放在第一个位置,也就是第一列。
select distinct sex,name from student
10. 多条件查询
当条件多余2个的时候,需要使用and或者or。当使用它们时,需要注意优先级。其中,and的优先级高于or 。
select * from student where sex=‘男’ and age >20 or age <18
意思是 (sex=‘男’ and age >20)或者 age <18 , 而不是 sex=‘男’ 或者 age >20 or age <18。如果不能够熟记于心,建议使用()来提高代码可读性。
11. in 关键词
in类似与枚举(enum),满足一系列里面的一个即可。
select name,sex from student where age in (18,19,20)
也可以使用or来代替。当让还有一个 [not in ] 意思与in 完全相反。
12. ordey by 排序
默认升序排序ASC,降序DESC。注意:(升序)空值默认排第一个,(降序)最后一个。位置相同,按照ASCll码排序。
select name ,age as ‘年龄’ from student order by 年龄 desc
13. like 模糊查询
使用通 “%” 和 “” 作为通配符。%代表任意位数(大于等于0),"" 代表只有一位。
select * from student where name like ‘%刘%’ order by age 包含 刘 的信息
select * from student where name like ‘刘_’ order by age 以 刘 开头的信息
当查询的信息中包含一些通配符(%,)的时候,需要使用转义(\)字符,一次只能转义一位。
select * from student where name like ‘_’
也可以使用escape自定义转义字符
select * from student where name like '@
’ escape @

感受:sql语句是比较多的,但也不是很复杂,其中有一些要注意的小细节不能疏忽,
比如说distinct必须放在第一列,这些得牢记于心,有时就是因为这些小问题,
我自己得卡半天,所以说平时需要多练习。
还有就是我总是记不清楚删除表是delete from 表名,
总会写成delete table 表名,得注意一下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值