MySQL增删查改语句(入门)
数据库定义语句:
- create:创建数据库及表对象
- drop:删除数据库及表对象
- alter:修改数据库及表对象
数据库控制语句:
- insert:向表中插入数据
- delete:将表中数据删除
- update:更改表中数据
- select:检索表中数据
数据库管理语句:
- grant:给用户授权
- revoke:回收用户权限
create
mysql> create table customers
-> (
-> cust_id int(4) primary key not null auto_increment, #创建表格,指定了主键,不允许为空,自增张
-> cust_name char(20) not null,
-> cust_sex char(5) not null,
-> cust_add char(20) not null default 'china', #默认值为'China'
-> cust_city char(20) not null default 'beijing'
-> );
alter:
1. MariaDB [test]> alter table test.customers
-> change column cust_sex sex char(2) null default 'M';
//修改表中字段名称及数据类型,change子句。
2. MariaDB [test]> alter table test.customers
-> alter column cust_city set default 'hangzhou';
//alter子句用来修改表中字段的默认值的。
3. MariaDB [test]> alter table test.customers
-> modify column cust_name char(20) first;
//modify子句用来修改表中字段的数据类型,first表示将这个字段设为第一列。
4. MariaDB [test]> alter table test.customers
-> drop column sex;
//drop子句用来删除表中字段(列,一个意思,别混淆)
MariaDB [test]> desc test.customers;
+--------------+-------------+------+-----+----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+----------+----------------+
| cust_name | char(20) | YES | | NULL | |
| cust_id | int(11) | NO | PRI | NULL | auto_increment |
| cust_add | varchar(50) | NO | | fd | |
| cust_city | char(10) | NO | | hangzhou | |
| cust_contact | char(50) | NO | | NULL | |
+--------------+-------------+------+-----+----------+----------------+
5. MariaDB [test]> alter table test.customers
-> rename to test.customers_backup;
MariaDB [test]> rename table test.customers_backup to test.customers;
//rename子句用来修改表的名称
6. MariaDB [test]> show tables from test;
//查看test库下表
7. MariaDB [test]> show columns from test.customers;
//查看该表的字段以及数据类型
insert
MariaDB [(none)]> insert into
-> test.customers
-> values
-> ('zhangsan',101,'qiao','BeiJing','Ali');
//向表中插入数据,千万要注意符合设定的约束即数据类型,字符要加引号。
MariaDB [(none)]> insert into test.customers
-> set cust_name='111',cust_add='WU';
//插入单个或多个指定字段的数据。
还有一个用法就是,insert……select,将从某个表对象查询的表插入到这个表对象中,需要注意的是,要注意数据类型一致性
delete
MariaDB [(none)]> delete from test.customers
-> where cust_name='lisi';
//删除满足条件的行记录
update
MariaDB [(none)]> update test.customers
-> set cust_add='ShanTou'
-> where cust_name='zhangsan';
select
MariaDB [(none)]> select cust_name from test.customers where cust_name='qiao1';
//查询指定字段,并满足where子句的条件。
MariaDB [(none)]> select cust_name,cust_add as dizhi from test.customers;
+-----------+-------+
| cust_name | dizhi |
+-----------+-------+
| qiao1 | BJ |
| qiao2 | CD |
+-----------+-------+
//as后起一个别名,并不改变表中数据
MariaDB [(none)]> select cust_name, case when cust_add='BJ' then 'BeiJing' else 'other city' end as dizhi from test.customers;
+-----------+------------+
| cust_name | dizhi |
+-----------+------------+
| qiao1 | BeiJing |
| qiao2 | other city |
+-----------+------------+
MariaDB [(none)]> select cust_name,cust_id+1000000000000000000000000000000 from test.customers;
+-----------+-----------------------------------------+
| cust_name | cust_id+1000000000000000000000000000000 |
+-----------+-----------------------------------------+
| qiao1 | 1000000000000000000000000000111 |
| qiao2 | 1000000000000000000000000000112 |
+-----------+-----------------------------------------+
MySQL增删查改语句(入门)的更多相关文章
- php mysql增删查改
php mysql增删查改代码段 $conn=mysql_connect('localhost','root','root'); //连接数据库代码 mysql_query("set na ...
- mysql 增删查改
非关系型数据库关系型数据库Oracle mysql sqlserver db2 Postgresql Sqlite access sqlserver 微软db2 ibm================ ...
- Mysql常用增删查改及入门(二)
常用:数据库常用就是DML:增删查改 1.增加数据: insert into 表名 values (值1,值2...); insert into 表名 (字段1,字段2) values (值1,值2) ...
- node.js+mysql增删查改
数据库和表: -- -- 数据库: `test` -- -- -------------------------------------------------------- -- -- 表的结构 ` ...
- 后端Spring Boot+前端Android交互+MySQL增删查改
2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)
1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...
- MySQL 增删查改 必知必会
MySQL 数据库中的基础操作 3.表的修改 对表的表名.字段.字段类型.字段长度.约束等进行修改. 3.1 表的名称修改 -- 语法: ALTER TABLE 库名.表名 RENAME TO 新表名 ...
- mysql数据库,数据表,数据的增删查改语句
查询mysql支持的引擎 show engines; 查询mysql支持的字符集 show character set; 设置mysql默认存储引擎 set default_storage_engin ...
- Hibernate增删查改语句
我用的数据库是MySQL,实体类叫Product create table Product ( proId integer not null auto_increment, proName varch ...
随机推荐
- mysql 1045
1.sudo gedit /etc/my.cnf 2.加入 skip-grant-tables 3.直接登录,输密码时回车 mysql -u root -p 4.修改密码 use mysql; upd ...
- 报错 react-hot-loader
./node_modules/element-react/dist/npm/es5/libs/animate/index.js Module not found: Can't resolve 'rea ...
- python中验证码连通域分割的方法详解
python中验证码连通域分割的方法详解 这篇文章主要给大家介绍了关于python中验证码连通域分割的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用python具有一定的参考学习价值,需 ...
- .Net MVC 标签页
目录 Bootstrap的标签页 适合.Net MVC的标签页 Bootstrap的标签页 下面是Bootstrap的标签页,挺好的,但是用的id,内容是固定的?我不知道怎么变成不同的视图来 < ...
- Tensorflow-逻辑斯蒂回归
1.交叉熵 逻辑斯蒂回归这个模型采用的是交叉熵,通俗点理解交叉熵 推荐一篇文章讲的很清楚: https://www.zhihu.com/question/41252833 因此,交叉熵越低,这个策略就 ...
- 对spring的简单了解
对spring的简单了解 什么是spring Spring是一个开源框架,是为了解决企业应用开发的复杂性而创建的,同时Spring也是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 特点 ...
- poj1228(稳定凸包+特判最后一条边)
题目链接:https://vjudge.net/problem/POJ-1228 题意:我是真的没看懂题意QAQ...搜了才知道.题目给了n个点,问这n个点确定的凸包是否能通过添加点来变成一个新的凸包 ...
- [转帖]LINUX下使用rinetd端口转发
LINUX下使用rinetd端口转发 https://www.iteye.com/blog/lvinie-1167701 . 本来想自己写一下 发现没必要. 并且原作者提供了pan.baidu.com ...
- vc编译器对 除法的优化
基本知识,7/2 和 6/2 在计算机中的商都为3.C语言的除法不等同于数学意义中的除法. C语言的除法.采用向零取整的方法. -______________0_______________+ 只有在 ...
- 剑指offer59:按之字形顺序打印二叉树:[[1], [3,2], [4,5,6,7]]
1 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 2 思路和方法 先给定一个二叉树的样式: ...