栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

MySQL基本代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MySQL基本代码

数据库相关的SQL

1.查询所有数据库

格式:show databases;

2.创建数据库

格式:create database 数据库名 charset=utf8/gbk;

举例 create database db1;
        create database db2 charset=utf8;
        create database db3 charset=gbk;
        show databases;

3.查询数据库

格式:show create database 数据库名;

举例:show create database db1;

4.删除数据库

格式:drop database 数据库名;

举例:drop database db3;
           show databases;

5.使用数据库

使用完数据库之后再执行相关的数据SQL 否则会报错:No database selected

格式:use 数据库名;

举例:use db1;

表相关的SQL

执行表相关的SQL必须使用了某个数据库否则会报错
1.创建表

格式:create table 表名(字段1名 类型, 字段2名 类型) charset=utf8/gbk;

举例:create table person(name varchar(50),age int);

           create table car(name varchar(50),type varchar(5),price int);

           create table student(name varchar(50),age int,chinese int,math int,english int)charset=gbk;

2.查询所有表

格式:show tables;

3.查询表信息

格式:show create table 表名;

举例:show create table person;
           show create table student;

4.查询表字段信息

格式:desc 表名;

5.删除表

格式:drop table 表名;

举例:drop table car;

           show tables;

6.修改表名

格式:rename table 原名 to 新名;

7.添加表字段

最后面添加格式:alter table 表名 add 字段名 类型;

最前面添加格式:alter table 表明 add 字段名 类型 first;

在xxx字段的后面添加:alter table 表名 add 字段名 类型 after xxx;

举例:creater database db4;

            use db4;

             creater table teacher(name varchar(20));

             alter table teacher add age int; 

              alter table teacher add id int first;

               alter table teacher add salary int after name;

8.删除表字段

格式:alter table 表名 drop 字段名;

           alter table teacher change age salary int;

数据相关SQL

执行数据相关的SQL语句必须使用了某个数据库并且已经创建好了保存数据的表

create database db5 charset=utf8;

use db5;

create table person(name varchar(50),age int);

1.插入数据

全表格插入格式:insert into 表名 values(值1,值2);

指定字段插入格式:insert into 表名(字段1名,字段2名) values (值1,值2);

举例:insert into person values("tom",30);

           insert into person(name) values("jerry");

批量插入:insert into person values("liubei",20),("guanyu",18),("zhangfei",15);

                insert into person(name) values('libai'),('liucangsong');

插入中文:insert into person values("刘德华",17);

如果执行上面的SQL语句报一下错误,执行 set names gbk; 之后再次执行

2.查询数据

格式:select 字段信息 from 表名 where 条件;

举例:select name from person;

select name,age from person;

select * from person;

select * from person where age>20;

select age from person where name='tom';

select name from person where age=15; 

3.修改数据

格式:update 表名 set 字段名=值,字段名=值 where 条件;

举例:update person set age=50 where name="刘备";

           update person set name=" liubei" where age=20;

4.删除数据

格式:delete from 表名 where 条件;

举例:delete from person where name="刘德华";

           delete from person where age<20;

delete from person where age is null;

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1036800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号