MySQL------常用语句

来源:互联网 发布:怎么查看端口是否打开 编辑:程序博客网 时间:2024/06/10 20:19

1.创建表:
create table if not exists user(
user_id int unsigned auto_increment,
user_name varchar(100) not null,
user_pwd varchar(40) not null,
create_time date,
primary key(user_id)
) engine=innodb default charset=utf8;
注:
① if not exists:if not exists创建数据表,即使此表已经存在,也会执行成功。
② auto_increment:定义列为自增的属性,一般用于主键,数值会自动加1。
③ not null:非null设置。
④ primary key:关键字用于定义列为主键。 您可以使用多列来定义主键,列间以逗号分隔。
⑤ engine: 设置存储引擎。
⑥ charset:设置编码。

2.删除表:
drop table user;
注:整张表连带数据全部删除(谨慎执行此操作)。

3.添加数据:
insert into user (user_name,user_pwd,create_time)
values(‘root’,’123456’,now());
注:一定要注意字段的类型,尤其是字符串类型要加单引号或者双引号。
NOW():获取当前系统时间。

4.更新数据:
update user set user_name = ‘admin’ [where id = 1]
注:如果没有指定 where 子句,表中的所有记录的user_name字段都将改为’admin’。

5.删除数据:
① delete from user [where user_id = 1]
注:如果没有指定 where 子句,表中的所有记录将都被删除。
② truncate table user;
注:truncate可以删除表中所有数据。
③ 两种删除表中所有数据的差别: 效率上truncate比delete快,但truncate删除后不记录mysql日志,不可以恢复数据。delete的效果有点像将mysql表中所有记录一条一条删除到删完,而truncate相当于保留mysql表的结构,重新创建了这个表,所有的状态都相当于新表。

6.查询数据:
① select * from user [where user_id = 1 and user_name = ‘root’];
注:如果没有指定 where 字句,将查询出表中所有数据。

7.模糊查询:
select * from user where user_name like ‘%ad’;
注:”%”百分号字符在进行模糊查询是一定要加上;如果没有使用百分号 %, LIKE 子句与等号 = 的效果是一样的。

8.union 和 union all
① select * from user union select * from user2;
返回的结果会去掉重复的数据。
② select * from user union all select * from user2;
返回的结果是两个表中所有数据,不会去掉重复数据
③ union 和 union all两者在使用时,两边的select语句所查询的字段必须相同(包括顺序)。
eg:select user_name,user_pwd from user union select user_name,user_pwd from user2;

9.order by 排序:
select * from user order by create_time [asc/desc]
注:
① order by 默认asc升序排列,desc为降序排列;
② order by 进行多字段排列时,用”,”逗号隔开即可;
eg:select * from user order by id desc,create_time asc;

10.group by 分组:
统计每种user_type分别有多少条数据:
select user_type,count(*) from user group by user_type;
注:
① GROUP BY 语句根据一个或多个字段对结果集进行分组。
② 在分组的字段上我们可以使用 COUNT, SUM, AVG,等函数。

11.内连接:
select u1.*,c1.* from user u1 inner join project c1 on u1.user_id = c1.user_id;
注:
① 返回两张表内符合条件的所有数据。

12.左连接:
select u1.*,c1.* from user u1 left join project c1 on u1.user_id = c1.user_id;
注:
① 返回左表中的所有数据和右表中只符合条件的数据。

13.右连接:
select u1.*,c1.* from user u1 right join project c1 on u1.user_id = c1.user_id;
注:
① 返回右表中的所有数据和左表中只符合条件的数据。

14.null值处理:
select * from user where user_name is null;
① is null:当字段的值是 null,此运算符返回 true。
② is not null:当字段的值不是 null,此运算符返回 true。
③ 在 mysql 中,null 值与任何其它值的比较(即使是 null)永远返回 false,所以mysql 中处理 null 只能使用 is null 和 is not null 运算符。

15.修改表名:
alter table user rename to user1;

16.添加表字段:
alter table user add user_sex int default 0 [first/after user_name] ;
注:
① default:设置默认值。
② first:设置新添加字段在表中第一个字段处。
③ after:设置新添加字段在表中哪个字段之后。

17.修改字段名称及类型:
① 只修改字段类型:
alter table user modify user_sex varchar(2);
② 修改字段名称以及类型:
alter table user change user_sex user_sex2 int;

18.删除表中字段:
alter table user drop user_sex;
注:如果数据表中只剩余一个字段则无法使用DROP来删除字段。

19.修改和删除字段默认值:
① 修改字段默认值:
alter table user alter user_sex set default 0;
② 删除字段默认值:

20.添加和删除主键:
① 添加主键:
alter table user modify user_id int not null;
alter table user add primary key (user_id);
注:
主键只能作用在一个字段上,添加主键时,必须确保该主键默认不为空(not null);
② 删除主键:
alter table user drop primary key;

21.创建临时表:
create temporary table userlin(
user_id2 int not null,
user_name2 varchar(200),
create_time not null
);
注:
mysql在默认情况下,会在断开与数据库连接的时候,将临时表销毁。当然你也可以使用trop table命令来手动删除临时表。

原创粉丝点击