目录
SQL介绍——Structured Query Language
学习地址:https://www.liaoxuefeng.com/wiki/1177760294764384
学习地址:https://www.runoob.com/sql/sql-quickref.html
主流关系数据库:
- 商用数据库,例如:Oracle,SQL Server,DB2等;
- 开源数据库,例如:MySQL,PostgreSQL等;
- 桌面数据库,以微软Access为代表,适合桌面应用程序使用;
- 嵌入式数据库,以Sqlite为代表,适合手机应用和桌面程序。
SQL介绍——Structured Query Language
SQL是结构化查询语言的缩写,用来访问和操作数据库系统。SQL语句既可以查询数据库中的数据,也可以添加、更新和删除数据库中的数据,还可以对数据库进行管理和维护操作
SQL命令的五种分类
DDL:Data Definition Language(数据定义语言)
DDL允许用户定义数据(建表、删表、改表)——由DDL数据库管理员执行
create——创建
实体:学生 表名:s_student 属性:id,name,age,gender,dob,phone 要求名字默认为"新用户",性别默认为男性,年龄默认为20, 生日默认为2000年1月1日。 create table s_student( id number(7) name varchar2(20) default '新用户' age number(7) default 20 gender varcher2(20) default '男' dob date default ('2000-01-01','yyyy-mm-dd') phone char (11) );
drop——删除
drop table table_name
alter——修改
alter table table_name 操作; eg1:在表中新增一列 alter table test add birthday date; eg2:在表中删除某列 alter table test drop column birthday; eg3:给表中的列添加约束 alter table 表名 add [constraint 约束名] 约束类型(字段); alter table test add [constraint t_user_name_uk] unique(name) eg4:删除表中约束 alter table 表名 drop constraint 约束名; alter table test drop constraint user_name_un; eg5:修改表中某列类型 alter table 表名 modify 字段声明; alter table test modify (name varchar2(500)); eg6:使约束失效 alter table 表名 disable constraint 约束名 cascade; alter table t_user disable constraint user_id_pk cascade; eg7:使失效约束再次生效 alter table 表名 enable constraint 约束名; alter table t_user enable constraint user_id_pk;
truncate——截断(删除数据的效果)
1、truncate:是DDL操作语言,没有事务性,一旦删除不可恢复
2、truncate:直接删除表中全部数据,不进行where筛选
DML:Data Manipulation Language(数据操作语言)
DML为用户提供增、删、改数据的能力——应用程序对数据库的操作
insert——增加
insert into table (id,last_name) values (999,'zyj'); commit;
delete——删除
delete from table_name where id=1;
update——修改
update table_name set name="zyj" where id in (1,2,3);
DQL:Data Query Langauge(数据查询语言)
DQL允许用户查询数据
select 字段1,字段2,... from 表名 where 单值限定条件 group by 分组 having 多值限定条件 order by 排序方式 (id ASC/DESC)
DCL:Data Control Language(数据控制语言)
DCL控制用户的权限
grant——向用户赋予权限/角色
revoke——撤销用户的权限/角色
DTL: Data Transaction Language(数据事务语言)
commit——提交事务
rollback——回滚事务