SQL语句的分类:
a,DDL 数据定义:用来定义数据库对象:对数据库或表的结构操作(增、删、改)。
创建表:create table 表名(字段名 类型 [其他],....);
create table user(stuid int,stuname varchar(10),age int);
修改:alter table 表名 add/modify/change/drop/rename to XXXX
alter table user drop useid;
alter table user add(useid int);
alter table user change money gongzi int;
allter table user RENAME to stu;
删除表:drop table 表名
drop table stu;
b,DML 数据操作:用来定义数据库记录(数据),对表的记录进行更新(增、删、改)。
添加:insert into 表名(字段名...) values(值....);
insert into stu(stuid,stuname,age)values(5,'田七',20);
删除:delete from 表名 where 字段名=值;
delete from stu where stuid=5;
修改:update 表名 set 字段名=新值.... where 字段名=值;
update stu set age=26 where stuname='赵六';
c,DCL 数据控制:用来定义访问权限和安全等级,对用户的创建和授权。
d,DQL 数据查询:用来查询记录(数据);主要是对表记录的查询。(**重点**)
1、条件查询
where、表达式、and/or、between and、in、is not、like
SELECT * FROM stu;
select stuid,stuname from stu;
select distinct age,stuid from stu;
select stuid as 学号 from stu as 学生 where age=20;
select * from stu where stuname like '张_';
“`
2、排序查询
order by、asc、desc
3、分组查询
group by、having
4、分页查询
limit
select * from stu limit 1,2;
主键的特点:非空、唯一、被引用。当表的某一列被指定为主键后,该列就不能为空,不能有重复值出现。
非空约束:因为某些列不能设置为NULL值,所以可以对列添加非空约束。
唯一约束:某些列不能设置重复的值,所以可以对列添加唯一约束。
外键:外键必须是另一张表(或本表)的主键的值,外键要引用主键;
外键可以重复;
外键可以为空;
一张表中可以有多个外键。
第一次写文章,不足之处欢迎指出。`