首页 > 代码库 > 基础sql语句

基础sql语句

sfdf

 

---------------------------------------------------// 创建数据库create database databaseName;e.g:create database sample;// 显示数据库状态status;// 使用数据库use databaseName;// 删除整个数据库drop database databaseName;---------------------------------------------------// 创建数据库表create table tableName(cols);e.g: create table students(id int unsigned not null auto_increment primary key,name char(8) not null,sex char(4) not null,age tinyint unsigned not null,tel char(13) null default "-");// 重命名表alter table tableName rename new_tableName;e.g:alter table students rename workmates;// 删除表drop table tableName;// 删除表中的数据delete from tableName where [condition];e.g:delete from students where id=2;// 显示表show tables;// 显示表结构describe tableName;e.g:describe students;---------------------------------------------------// 插入表数据insert into tableName values(cols);e.g: insert into students values(NULL, "王刚", "男", 20, "13811371377");// 查询表数据select colName1, colName2 from tableName where [conditions];e.g: select name, age from students where age>18;// 更新表数据update tableName set colName=new_value where [condition];e.g:update students set age=23 where id=2;// 修改表结构// 添加列alter table tableName add colName col_dataType [after colName];e.g:alter table students add birthday date after age;e.g:alter table students add address char(60);// 修改列alter table tableName change colName new_colName col_dataType;e.g:alter table students change name name char(16) not null;// 删除列alter table tableName drop colName;e.g:alter table students drop birthday;---------------------------------------------------// 日期操作:date_add和date_sub.mysql> select * from students;+----+--------+-----+-----+------------+-------------+--------------+| id | name | sex | age | birthday | tel | address |+----+--------+-----+-----+------------+-------------+--------------+| 1 | 杨家将 || 26 | 2016-07-28 | 12345678901 | 喝粥大学 || 2 | 刘姥姥 || 25 | 2016-06-03 | 12121212121 | 吃饭大学 |+----+--------+-----+-----+------------+-------------+--------------+// 查询以当前时间为原点,以birthday为查询标的,向前向后延伸150days的所有记录.select name from students where date_sub(now(), interval 150 day) <=birthday;// 查询以2016-07-01为原点,以birthday为查询标的,向前向后延伸20days的所有记录.select name from students where date_sub(2016-07-01, interval 20 day) <=birthday;// 以2016-07-23为原点,以birthday为查询标的,向前向后延伸30days的所有记录Records, 查询不在Records记录中的其他记录.select name from students where date_sub(2016-07-01, interval 30 day) >birthday;// 以2016-06-01增加30days所得到的时间点为OriginDay,查询在OriginDay时间之后的所有记录.select name from students where date_add(2016-06-01, interval 30 day) <=birthday;// 以2016-06-01增加30days所得到的时间点为OriginDay,查询在OriginDay时间之前的所有记录.select name from students where date_add(2016-06-01, interval 30 day) > birthday;// 查询两个日期之间的记录.select * from students where birthday between 2016-06-11 and 2016-07-29;---------------------------------------------------

 

 

sdf

 

 

 

 

sdf

基础sql语句