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

超全SQL语句大全,值得收藏(常用)
最新回答
姊‘妝濃孒

2026-04-07 17:33:17

在MySQL中,执行登录命令:mysql -h 主机名 -u 用户名 -p ,其中主机名表示客户端登录的MySQL主机名,如未指定则默认为当前机器;-u用于指定登录的用户名;-p表示使用密码登录,若用户名密码为空可省略此参数。

创建数据库使用命令:create database 数据库名 [其他选项]。可通过运行show databases;来查看已创建的数据库列表。

一、显示数据库信息

使用命令:show databases;。

二、选择操作数据库

通过命令:use 数据库名; 来切换数据库。

创建表: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 "-" );

插入数据:使用insert语句如:insert into students values(NULL, "王刚", "男", 20, "13811371377");

查询数据:使用select语句,如:select name, age from students; 或 select * from students; 使用通配符 * 可查询所有列。

条件查询:使用where关键字,如:select * from students where sex="女"; 查询特定条件数据。

更新数据:使用update语句如:update students set tel=default where id=2; 更新指定列数据。

删除数据:使用delete语句如:delete from students where id=2; 删除指定数据。

表修改:使用alter table语句添加、修改、删除列,如:alter table students add address char(60); 或alter table students drop birthday;。

重命名表:使用alter table表名 rename 新表名;。

删除表:使用drop table 表名;。

删除数据库:使用drop database 数据库名;。

创建用户:使用命令CREATE USER 'lxk'@'localhost' identified by 'lxk';,并授权grant all privileges on lxk. to 'lxk'@'localhost';,刷新系统权限表flush privileges;。

MySQL数据类型包括数字类型(如tinyint、float)、日期/时间类型(如date、datetime)、字符串类型(如char、text),提供丰富存储需求。