标签:
数据库、表、字段、记录:增、删、改、查。分类进行学习,重点是查询。
/* 数据库、表、字段、记录:增、删、改、查 */ -- 1.数据库:增删改查 create datebase if not exists jkxy; drop database if exists jkxy; rename database jkxy to jikexueyuan; --这条语句官方说不能用了 show databases; use jikexueyuan; /* 1、CREATE DATABASE new_db_name; 创建新的数据库 2、RENAME TABLE db_name.table1 TO new_db_name.table1; 对所有的表进行重命名 3、DROP DATABASE db_name; 删除原来的数据库 */ -- 2.表:增删改查 create table if not exists user(); drop table if exists user; alter table user rename users; --as可以省略不要 show tables; desc users; --3.字段:增删改查 alter table user add tpass char(32) not null default ‘‘ after userpass; alter table user drop tpass; alter table change userpass tpass varchar(50) not null default ‘‘; alter table modify tpass char(32) not null default ‘‘; desc user; alter table user auto_increment = 1; --设置自增值为1 alter table user add index username_index(username); --为字段添加索引 alter table user drop index username_index; alter table user add unique(tel); --还剩主键不知道怎么添加和删除 alter table user drop index tel; --alter table user add unique tel_unique(tel); index和unique删除都是用drop index show indexes from user; --4.记录:增删改查 insert into user(username, pwd, email) values(‘user1‘, md5(123456), ‘user1@jkxy.com‘), (‘user2‘, md(123456), ‘user2@jkxy.com‘); delete from user where id = 2; update user set username = ‘user111‘, email = ‘user111@jkxy.com‘ where id = 1; select username from user where id > 1; --5.重点:查询 select username from user where id > 2; select username as ‘用户名‘ from user where id > 2; select distinct email from user; --只针对一个字段 select * from user where username like ‘%user%‘; --内容包含user的记录 select * from user order by score DESC; select * from user order by score DESC limit 2; --limit 0, 2; select * from user order by score DESC limit 2,3; --下标从0开始,从第2+1条开始,取三条 select count(id) num from user where id > 2; --计算id的条数,num是一个别名 select avg(score) num from user; --计算平均值,还有函数:max() min() sum()
标签:
原文地址:http://www.cnblogs.com/htmlphp/p/5767856.html