SQL基本操作语句

1.创建一个数据库

使用 create database yourDatabaseName 语句可完成对数据库的创建, 创建命令的格式如下:
create database 数据库名 [其他选项];

例如我们需要创建一个名为 samp_db 的数据库, 在命令行下执行以下命令:

create database samp_db character set gbk;

为了便于在命令提示符下显示中文, 在创建时通过 character set gbk 将数据库字符编码指定为 gbk。创建成功时会得到 Query OK, 1 row affected(0.02 sec) 的响应。

在创建数据库成功后,需要选择一个具体的数据库来进行后续的操作,如果自己的数据库较多,可以选择show databases; 来查看所有的数据库。当找到自己所需的数据库时,使用 use samp_db; 来选择刚才创建的数据库。

2.创建数据库表

使用 create table 语句可完成对表的创建, create table 的常见形式:
create table 表名称;

这里以创建 students 表为例, 表中将存放 学号(id)、姓名(name)、性别(sex)、年龄(age)、联系电话(tel) 这些内容:

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

括号内声明了5列内容, id、name、sex、age、tel为每列的名称, 后面跟的是数据类型描述, 列与列的描述之间用逗号(,)隔开;(最后一个列特殊,如上面的语句 tel char(13) null default "-" 后面不能加(,),其余的每个属性描述后都用(,)隔开)。
这里我将以 “id int unsigned not null auto_increment primary key” 行来进行语义介绍:

“id” 为列的名称;
“int” 指定该列的类型为 int(取值范围为 -8388608到8388607), 在后面我们又用 “unsigned” 加以修饰, 表示该类型为无符号型, 此时该列的取值范围为 0到16777215;
“not null” 说明该列的值不能为空, 必须要填, 如果不指定该属性, 默认可为空;
“auto_increment” 需在整数列中使用, 其作用是在插入数据时若该列为 NULL, MySQL将自动产生一个比现存值更大的唯一标识符值。在每张表中仅能有一个这样的值且所在列必须为索引列。
“primary key” 表示该列是表的主键, 本列的值 必须唯一,不能为空,不能重复, MySQL将自动索引该列。
与数据库一样,当一个库中有多个表时,可以使用show tables;来查看所有的表。
3.向表中插入数据

insert 语句可以用来将一行或多行数据插到数据库表中, 使用的一般形式如下:
insert [into] 表名 [(列名1, 列名2, 列名3, …)] values (值1, 值2, 值3, …);

其中 [] 内的内容是可选的, 例如, 要给 samp_db 数据库中的 students 表插入一条记录, 执行语句:

insert into students values(NULL, "王刚", "男", 20, "13811371377");

按回车键确认后若提示 Query Ok, 1 row affected (0.05 sec) 表示数据插入成功。 若插入失败请检查是否已选择需要操作的数据库。

有时我们只需要插入部分数据, 或者不按照列的顺序进行插入, 可以使用这样的形式进行插入:

insert into students (name, sex, age) values("孙丽华", "女", 21);

4.查询表中的数据

select 语句常用来根据一定的查询规则到数据库中获取数据, 其基本的用法为:
select 列名称 from 表名称 [查询条件];

例如要查询 students 表中所有学生的名字和年龄, 输入语句 select name, age from students; 执行结果如下:

mysql> select name, age from students;
+——–+—–+
| name | age |
+——–+—–+
| 王刚 | 20 |
| 孙丽华 | 21 |
| 王永恒 | 23 |
| 郑俊杰 | 19 |
| 陈芳 | 22 |
| 张伟朋 | 21 |
+——–+—–+
6 rows in set (0.00 sec)

也可以使用通配符 * 查询表中所有的内容, 语句: select * from students;

按特定条件查询:
where 关键词用于指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;

以查询所有性别为女的信息为例, 输入查询语句: select * from students where sex=”女”;

where 子句不仅仅支持 “where 列名 = 值” 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。

eg:
查询年龄在21岁以上的所有人信息: select * from students where age > 21;

查询名字中带有 “王” 字的所有人信息: select * from students where name like "%王%";

查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;

查询满足总分大于200分的学生的个数:select count(*) from students where english+math+chinese >200;

查询时给结果起个别名:select sum(chinese) 语文总分,sum(english) 英语总分 from students;

5.更新表中的数据

update 语句可用来修改表中的数据, 基本的使用形式为:
update 表名称 set 列名称=新值 where 更新条件;

eg:
将id为5的手机号改为默认的”-“: update students set tel=default where id=5;

将所有人的年龄增加1: update students set age=age+1;

将手机号为 13288097888 的姓名改为 “张伟鹏”, 年龄改为 19: update students set name="张伟鹏", age=19 where tel="13288097888";

6.删除表中的数据

delete 语句用于删除表中的数据, 基本用法为:
delete from 表名称 where 删除条件;

eg:
删除id为2的行: delete from students where id=2;

删除所有年龄小于21岁的数据: delete from students where age<20;

删除表中的所有数据: delete from students;(这样的操作要小心哦!因为你很有可能就这样失去你的工作)

7.创建表后的修改

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

添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];

eg:
在表的最后追加列 address: alter table students add address char(60);

在名为 age 的列后插入列 birthday: alter table students add birthday date after age;

修改列
基本形式: alter table 表名 change 列名称 列新名称 新数据类型;

eg:
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";
将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;

删除列
基本形式: alter table 表名 drop 列名称;

eg:
删除 birthday 列: alter table students drop birthday;

重命名表
基本形式: alter table 表名 rename 新表名;(或rename 表名 to 新表名)

eg:
重命名 students 表为 workmates: alter table students rename workmates;
Rename students to workmates;

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

eg: 删除 workmates 表: drop table workmates;

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

eg: 删除 samp_db 数据库: drop database samp_db;

注意: MySQL语句以分号(;)作为语句的结束, 若在语句结尾不添加分号时, 命令提示符会以 -> 提示你继续输入(有个别特例, 但加分号是一定不会错的);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值