添加数据和简单查询

修改表名

alter table 表名 rename to 新表名

修改列名

alter table 表名 change 列名 新列名 char;

 删除列表:

alter table 表名 drop  column 列名

添加列名:

alter table 表名 add column 列名 varchar(20)

修改列属性:

alter table 表名 change 列名 列名 char(40)     alter table 表名 modify 列名 char(10) 

1.注释语法:--,#
2.后缀是.sql的文件是数据库查询文件
3.保存查询
4.在数据库里面 列有个名字叫字段 行有个名字叫记录

CRUD操作:
create 创建(添加)
read 读取
update 修改
delete 删除

1、添加数据
insert into 表名 values('p009','张三',1,'n001','2016-8-30 12:9:8') ;
给特定的列添加数据
insert into 表名 (code,name) values('p010','李四');
自增长列的处理
insert into 表名 values('','p001','数据','T001','数据',1);

insert into 表名 values(值)

2、删除数据
删除所有数据
delete from 表名
删除特定的数据
delete from 表名 where 列名='p001'

delete from 表名 where 条件

3、修改数据
修改所有数据
update 表名 set 列名='徐业鹏'
修改特定数据
update 表名 set name='吕永乐' where code='p002'
修改多列
update Info set name='吕永乐',sex=1 where code='p003'

update 表名 set 要修改的内容 where 条件

4、读取数据
(1)简单读取,查询所有列(*) 所有行(没有加条件)
select * from Info
(2)读取特定列
select code,name from Info
(3)条件查询
select * from Info where code='p003'
(4)多条件查询
select * from Info where code='p003' or nation='n002' #或的关系
select * from Info where sex=0 and nation='n002' #与的关系
(5)关键字查询(模糊查询)
查所有包含奥迪的汽车
select * from car where name like '%奥迪%'; #百分号%代表任意多个字符
查以'皇冠'开头的所有汽车
select * from car where name like '皇冠%';
查询汽车名称中第二个字符是'马'的
select * from car where name like '_马%'; #下划线_代表任意一个字符
(6)排序查询
select * from car order by powers #默认升序排列
select * from car order by powers desc #升序asc 降序 desc
先按brand升序排,再按照price降序排
select * from car order by brand,price desc

查询存在一个表而不在另一个表中的数据记录:

方法一(仅适用单个字段)
使用 not in ,容易理解,效率低

select A.ID from A where A.ID not in (select ID from B)

方法二(适用多个字段匹配)
使用 left join...on... , "B.ID isnull" 表示左连接之后在B.ID 字段为 null的记录

select A.ID from A left join B on A.ID=B.ID where B.ID is null 

方法三(适用多个字段匹配)

select * from B where (select count(1) as num from A where A.ID = B.ID) = 0

方法四(适用多个字段匹配)

select * from A where not exists(select 1 from B where A.ID=B.ID)

原文地址:https://www.cnblogs.com/gaojunshan/p/5967013.html