数据库SQL语句操作
2021/7/26 2:09:44
本文主要是介绍数据库SQL语句操作,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
–数据库连接:
1、mysql管理人默认为root,没有设置密码则直接登录 mysql -h host -u root -p 不用输入密码按回车自动进入 2、如果想设置mysql密码 mysqladmin -u root password 123456 3、如果你的root现在有密码了(123456),那么修改密码为abcdef的命令是: mysqladmin -u root -p password abcdef
– 数据库操作
-- 链接数据库 mysql -uroot -p123456 -- 退出数据库 exit/quit/ctrl + d -- 显示版本 select version(); -- 显示时间 select now(); -- 查看所有数据库 show databases; -- 创建数据库 -- create database 数据库名 charset=utf8; create database python; create database python charset=utf8; --查看当前使用数据库 select database(); -- 使用数据库 -- user 数据库名 use python; -- 删除数据库 -- drop database 数据库名; drop database python;
– 数据表操作
--desc 数据表名字; 查看数据表结构 desc xxxxx; --查看当前数据库中所有表 show tables; --创建表 --auto_increment表示自动增长 --not null 表示不能为空 --primary key 表示主键 --default 默认值 -- create table 表名(字段 类型 约束[, 字段 类型 约束]); -- 创建classes的表(id, name) create table x1(id int, name varchar(30)); create table x2( id int primary key not null auto_increment, name varchar(30) ); create table x3( id int unsigned not null auto_increment primary key, name varchar(30), age tinyint unsigned default 0, high decimal(5,2), gender enum("男", "女", "保密") default "保密", cls_id int unsigned ); -- mysql> desc x3; -- +--------+------------------------+------+-----+---------+----------------+ -- | Field | Type | Null | Key | Default | Extra | -- +--------+------------------------+------+-----+---------+----------------+ -- | id | int unsigned | NO | PRI | NULL | auto_increment | -- | nema | varchar(30) | YES | | NULL | | -- | age | tinyint unsigned | YES | | 0 | | -- | high | decimal(5,2) | YES | | NULL | | -- | gender | enum('男','女','保密') | YES | | 保密 | | -- | cls_id | int unsigned | YES | | NULL | | -- +--------+------------------------+------+-----+---------+----------------+ -- 6 rows in set (0.00 sec) -- 修改表-添加字段 -- alter table 表名 add 列名 类型; alter table x3 add birthday datetime; -- 修改表-添加字段:不重命名 -- alter table 表名 modify 列名 类型及约束; alter table x3 modify birthday date; -- 修改表-添加字段:重命名 -- alter table 表名 change 原名 新名 类型及约束; alter table x3 change birthday11 birthday22 date default "1995-05-04"; -- 修改表-删除字段 -- alter table 表名 drop 列名; 删除一般少用 alter table x3 drop birthday22; --插入数据 insert into x3 values(0, "老王", 18, 188.88, "男", 0); select * from x3; -- mysql> select * from x3; -- +----+------+------+--------+--------+--------+ -- | id | nema | age | high | gender | cls_id | -- +----+------+------+--------+--------+--------+ -- | 1 | 老王 | 18 | 188.88 | 男 | 0 | -- +----+------+------+--------+--------+--------+ -- 1 row in set (0.00 sec) -- drop table 表名 删除表 drop database 数据库; drop table 数据表; -- delete from 表名 清空表 -- truncate from 表名 清空表 --查询表的创建语句 --show create table 表名字;
–数据增删改查
--增加 --全列插入 --insert [into] 表名 values(); --主键任意添值占位 -- +----------+------------------------+------+-----+---------+----------------+ -- | Field | Type | Null | Key | Default | Extra | -- +----------+------------------------+------+-----+---------+----------------+ -- | id | int unsigned | NO | PRI | NULL | auto_increment | -- | name | varchar(30) | YES | | NULL | | -- | age | tinyint unsigned | YES | | 0 | | -- | gender | enum('男','女','保密') | YES | | 保密 | | -- | cls_id | int unsigned | YES | | NULL | | -- | birthday | date | YES | | NULL | | -- +----------+------------------------+------+-----+---------+----------------+ insert into x3 values(0, "小王", 20, "男", 1,"1995-06-06"); select * from x3; --insert into x3 values(0, "小王", 20, "1", 1,"1995-06-06"); -- 枚举中数据对应索引从1开始 +----+------+------+--------+--------+------------+ | id | name | age | gender | cls_id | birthday | +----+------+------+--------+--------+------------+ | 1 | 老王 | 18 | 男 | 0 | NULL | | 2 | 小王 | 20 | 男 | 1 | 1995-06-06 | +----+------+------+--------+--------+------------+ --部分插入 --insert [into] 表名 (列1...) values(值1...); insert into x3 (name, age) values ("大王", 20); select * from x3; +----+------+------+--------+--------+------------+ | id | name | age | gender | cls_id | birthday | +----+------+------+--------+--------+------------+ | 1 | 老王 | 18 | 男 | 0 | NULL | | 2 | 小王 | 20 | 男 | 1 | 1995-06-06 | | 3 | 大王 | 20 | 保密 | NULL | NULL | +----+------+------+--------+--------+------------+ --多行插入 insert into x3 (name, age) values ("大王", 20), ("老李", 20); --修改 --update 表名字 set 列1=value1,列2=value2... where 条件 update x3 set name="老王new",age=19 where id=1; update x3 set age=19 where id=1; --查询 --全查 where条件 select * from x3; --where条件 select * from x3 where age=19; --查询指定列 --select 列1,列2 from 表名; select name,age from x3; --可以使用as为列或表指定名 --select 字段 [as 别名],字段 [as 别名] from x3; select name as 姓名,age as 年龄 from x3;
这篇关于数据库SQL语句操作的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-04TiDB 资源管控的对撞测试以及最佳实践架构
- 2024-07-03万字长文聊聊Web3的组成架构
- 2024-07-02springboot项目无法注册到nacos-icode9专业技术文章分享
- 2024-06-26结对编程到底难不难?答案在这里
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现