数据库系统原理学习笔记1

我的博客链接

删除表 需要重新create

drop table r;

删除表中所有元组 可以重新插入

delete from r;             

前者更强力一点

mysql> select name from user;
+-------+
| name  |
+-------+
| zty   |
| qwe   |
| asdas |
| qwe   |
| qweqe |
+-------+
5 rows in set

mysql> select distinct name from user;               关键字distinct去重
+-------+
| name  |
+-------+
| zty   |
| qwe   |
| asdas |
| qweqe |
+-------+
4 rows in set

SQL中的字符串匹配:
% 匹配任意字符串
_ 匹配任意一个字符
用 like 来表达
eg:

select * from user where address like '%shanxi%';

还可以用escape定义转义字符
eg:

select * from user where address like 'ab\%cd%' escape '\';  匹配所有以'ab%cd'开头的字符串

order by 子句 对查询结果排序,默认为升序,
desc 表示降序
asc 表示升序
eg:

select * from user
order by money desc,   #对工资降序排列
name asc;                   #工资相同的 按名字升序排列

where 子句后 还可以加 between…and… and 等
eg:

select * from user where money between 1000 and 2000;
select * from user where address='qwe' and id='111';

集合运算

union 并
union all 保留所有重复
intersect 交
except 差

聚集函数:
以值的一个集合为输入,返回单个值 的函数

avg 平均值
max
min
sum 求和
count 计数

eg:

mysql> select * from user;
+----+-------+----------+
| id | name  | password |
+----+-------+----------+
|  1 | zty   | 123q     |
|  4 | qwe   | 456dfgd  |
|  5 | asdas | asdasd   |
|  6 | qwe   | dfg      |
|  7 | qweqe | vbcv     |
+----+-------+----------+
5 rows in set
mysql> select avg(id) as avg_id from user;      #as子句 将返回值 赋给 avg_id
+--------+
| avg_id |
+--------+
| 4.6000 |
+--------+
1 row in set

数据库修改:
删除:(整行),不能删除单个属性上的值

mysql> delete from user
    -> where name='zty';
Database changed

插入:

mysql> insert into user
    -> values(1,'zty','123456','a','2000','g');
Database changed

更新:

mysql> update user
    -> set salary = salary * 1.1;
mysql> update user
    ->set password='098765'
    -> where name='zty';

mysql> alter table user                         #增加一列
    -> add birthday varchar(30);
Database changed
Records: 5  Duplicates: 0  Warnings: 0

mysql> alter table user                         #删除一列
    -> drop column birthday;
Database changed
Records: 5  Duplicates: 0  Warnings: 0

SQL语言包括数据定义语言(DDL)和数据操纵语言(DML)

posted @ 2016-07-29 22:54  aallennty  阅读(180)  评论(0编辑  收藏  举报