当前位置:Gxlcms > 数据库问题 > 三.SQL语句

三.SQL语句

时间:2021-07-01 10:21:17 帮助过:18人阅读

20), sage TINYINT, sgender ENUM(m,f), cometime DATETIME);

11.数据类型

int: 整数 -231 ~ 231 -1

varchar:字符类型 (变长)

char: 字符类型 (定长)

tinyint: 整数 -128 ~ 128

enum: 枚举类型

datetime: 时间类型 年月日时分秒

12.创建表加其他属性

mysql> create table student(
sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT ‘学号’,
sname VARCHAR(20) NOT NULL COMMENT ‘学生姓名’,
sage TINYINT UNSIGNED COMMENT ‘学生年龄’,
sgender ENUM(m,f)  NOT NULL DEFAULT ‘m’ COMMENT ‘学生性别’,
cometime DATETIME NOT NULL COMMENT ‘入学时间’)chatset utf8 engine innodb;
#查看建表语句
mysql> show create table student;
#查看表
mysql> show tables;
#查看表中列的定义信息
mysql> desc student;

13.数据类型

not null: 非空

primary key: 主键(唯一且非空的)

auto_increment: 自增(此列必须是:primary key或者unique key)

unique key: 单独的唯一的

default: 默认值

unsigned: 非负数

comment: 注释

14.删除表

mysql> drop table student;

15.修改表

#修改表名
mysql> alter table student rename stu;
#添加列和列定义
mysql> alter table stu add age int;
#添加多个列
mysql> alter table stu add test varchar(20),add qq int;
#指定位置进行添加列(表首)
mysql> alter table stu add classid varchar(20) first;
#指定位置进行添加列(指定列)
mysql> alter table stu add phone int after age;
#删除指定的列及定义
mysql> alter table stu drop qq;
#修改列及定义(列属性)
mysql> alter table stu modify sid varchar(20);
#修改列及定义(列名及属性)
mysql> alter table stu change phone telphone char(20);

三.DCL(数据控制语言)

1.grant

#授权root@10.0.0.51用户所有权限(非炒鸡管理员)
mysql> grant all on *.* to root@10.0.0.51 identified by oldboy123;
#怎么去授权一个炒鸡管理员呢?
mysql> grant all on *.* to root@10.0.0.51 identified by oldboy123 with grant option;
#其他参数(扩展)
max_queries_per_hour:一个用户每小时可发出的查询数量
max_updates_per_hour:一个用户每小时可发出的更新数量
max_connetions_per_hour:一个用户每小时可连接到服务器的次数
max_user_connetions:允许同时连接数量

2.revoke

#收回select权限
mysql> revoke select on *.* from root@10.0.0.51;
#查看权限
mysql> show grants for root@10.0.0.51;

四.DML(数据操作语言)

1.insert

#基础用法,插入数据
mysql> insert into stu values(linux01,1,NOW(),zhangsan,20,m,NOW(),110,123456);
#规范用法,插入数据
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values(linux01,1,NOW(),zhangsan,20,m,NOW(),110,123456);
#插入多条数据
mysql> insert into stu(classid,birth.sname,sage,sgender,comtime,telnum,qq) values(linux01,1,NOW(),zhangsan,20,m,NOW(),110,123456),
(linux02,2,NOW(),zhangsi,21,f,NOW(),111,1234567);

2.update

#不规范
mysql> update student set sgender=f;
#规范update修改
mysql> update student set sgender=f where sid=1;
#如果非要全表修改
mysql> update student set sgender=f where 1=1;

3.delete

#不规范
mysql> delete from student;
#规范删除(危险)
mysql> delete from student where sid=3;
#DDL删除表
mysql> truncate table student;

4.伪删除

1)额外添加一个状态列

mysql> alter table student add status enum(1,0) default 1;

2)使用update

mysql> update student set status=0 where sid=1;

3)应用查询存在的数据

mysql> select * from student where status=1;

五.DQL(数据查询语言)

1.select:基础用法

#常用用法
mysql> select countrycode,district from city;
#查询单列
mysql> select countrycode from city;
#行级查询
mysql> select countrycode,district from city limit 2;
mysql> select id,countrycode,district from city limit 2,2;
#条件查询
mysql> select name,population from city where countrycode=CHN;
#多条件查询
mysql> select name,population from city where countrycode=CHN and district=heilongjiang;
#模糊查询
mysql> select name,population,countrycode from city where countrycode like %H% limit 10;
#排序查询(顺序)
mysql> select id,name,population,countrycode from city order by countrycode limit 10;
#排序查询(倒叙)
mysql> select id,name,population,countrycode from city order by countrycode desc limit 10;
#范围查询(>,<,>=,<=,<>)
mysql> select * from city where population>=1410000;
#范围查询OR语句
mysql> select * from city where countrycode=CHN or countrycode=USA;
#范围查询IN语句
mysql> select * from city where countrycode in (CHN,USA);

 

三.SQL语句

标签:sele   key   zhang   插入数据   gbk   expand   bsp   from   shutdown   

人气教程排行