天天动画片 > 八卦谈 > 常用SQL语句大全(值得收藏)

常用SQL语句大全(值得收藏)

八卦谈 佚名 2022-10-22 15:42:18

登录到MySQL:mysql -h 主机名 -u 用户名 -p-h : 该命令用于指定客户端所要登录的MySQL主机名, 登录当前机器该参数可以省略;-u : 所要登录的用户名;-p : 告诉服务器将会使用一个密码来登录, 如果所要登录的用户名密码为空, 可以忽略此选项。

img

创建一个数据库:create database 数据库名 [其他选项];提示: 可以使用 show databases; 命令查看已经创建了哪些数据库。

img
img

一、显示数据库

show databases;  (结尾要加分号)

img

二、选择所要操作的数据库:

use test;

img

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 "-");

img

三、往表中添加数据

nsert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:insert into students values(NULL, "王刚", "男", 20, "13811371377");

img

四、查询表中的数据

select 语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:select 列名称 from 表名称 [查询条件];例如:要查询 students 表中所有学生的名字和年龄, 输入语句 select name, age from students;也可以使用通配符 * 查询表中所有的内容, 语句: select * from students;

img

按特定条件查询:where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex="女";1.查询年龄在21岁以上的所有人信息: select * from students where age >= 21;2.查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";3.查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

img

五、更新表中的数据update 语句可用来修改表中的数据, 基本的使用形式为:update 表名称 set 列名称=新值 where 更新条件;1.将id为2的手机号改为默认的"-": update students set tel=default where id=2;2.将所有人的年龄增加1: update students set age=age+1;3.将手机号为 13288097888 的姓名改为 "张伟鹏", 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";

img

六、删除表中的数据delete 语句用于删除表中的数据, 基本用法为:delete from 表名称 where 删除条件;1.删除id为2的行: delete from students where id=2;2.删除所有年龄小于21岁的数据: delete from students where age=22;3.删除表中的所有数据: delete from students;

img

七、创建后表的修改alter table 语句用于创建后对表的修改, 基础用法如下:

添加列基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];1.在表的最后追加列 address: alter table students add address char(60);2.在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

img
img

修改列基本形式: alter table 表名 change 列名称 列新名称 新数据类型;1.将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";2.将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;

img

删除列基本形式: alter table 表名 drop 列名称;示例:删除 birthday 列: alter table students drop birthday;

img

八、重命名表基本形式: alter table 表名 rename 新表名;示例:重命名 students 表为 workmates: alter table students rename workmates;

img

删除整张表基本形式: drop table 表名;示例: 删除 workmates 表: drop table workmates;

img

删除整个数据库基本形式: drop database 数据库名;示例: 删除 samp_db 数据库: drop database samp_db;

img

最后:新建一个数据库用户和密码,并且设置所有权限。create user 'lxk'@'localhost' identified by 'lxk';grant all privileges on lxk.* to 'lxk'@'localhost';flush privileges;

具体怎么解释,我也不是很清楚,但就是这么用的。跟上我的个人理解吧。

上面就是新建了个用户,@标识本地的数据库,identified by 后面的跟的就是密码了。

grant单词就是授权的意思,all,全部,privileges,特权的意思。lxk.*估计就是lxk用户下的所有的数据库吧,

CREATE USER 'username@host' [IDENTIFIED BY 'PASSWORD'] 其中密码是可选项;说明:该方法创建出来的用户只有连接数据库的权限,需要后续继续授权;注意:用户与@后主机地址是一体的,用一个分号连接,否则会报错.ERROR 1396 (HY000): Operation CREATE USER failed for 'remote'@'%'

使用例子:CREATE USER 'john@localhost' IDENTIFIED BY "123";

grant all privileges on lxk.* to 'lxk'@'localhost';给主机为localhost的用户lxk分配可对数据库lxk所有表进行所有操作的权限,并设定口令为123。

完成用户的创建后,请记得刷新系统权限表;flush privileges;

以下是我参考的链接:MySQL创建用户的三种方法http://blog.csdn.net/huaishu/article/details/50540814MYSQL问题解决方案:Access denied for user 'root'@'localhost' (using password:YES)http://blog.csdn.net/skywalker_leo/article/details/47274441

img
img

**改密码的方法,在我的MySQL分类的文章的MySQL安装篇有介绍。

补充,MySQL数据库的数据类型。**如下,具体详细的,看我的jdbc篇的,jdbc全部概念篇,里面讲了MySQL数据库的各种数据类型的详解。MySQL有三大类数据类型, 分别为数字、日期\时间、字符串, 这三大类中又更细致的划分了许多子类型:

数字类型整数: tinyint、smallint、mediumint、int、bigint浮点数: float、double、real、decimal日期和时间: date、time、datetime、timestamp、year字符串类型字符串: char、varchar文本: tinytext、text、mediumtext、longtext二进制(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob


本文标题:常用SQL语句大全(值得收藏) - 八卦谈
本文地址:www.ttdhp.com/article/4863.html

天天动画片声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
扫码关注我们