MySQL基本增删改查语句练习

MySQL基本增删改查语句练习

#创建数据库:
create database zhangsan character set gbk; # 为了便于在命令提示符下显示中文, 在创建时通过 character set gbk 将数据库字符编码指定为 gbk

#要对一个数据库进行操作, 必须先选择该数据库
C:\Users\Administrator>mysql -u root -p #登录MySQL环境
Enter password: ********

mysql> mysql -D zhangsan -u root -p #登录创建好的zhangsan数据库
-> use zhangsan
-> ^C

mysql> use zhangsan #登录后使用zhangsan这个数据库
Database changed #有该提示表示可以成功使用zhangsan这个数据库

#创建数据库表:
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 “-”
);

create table students
(
id int unsigned not null auto_increment primary key,
name char(20)not null,
sex char(4)not null,
result int(10) not null,
age tinyint unsigned not null,
tel char(20) null default “-”
);

#插入数据库
mysql> insert into students values(NULL,“王刚”,“男”,20,“12345678”);
Query OK, 1 row affected (0.53 sec)

mysql> select name,age from students;
±-------±----+
| name | age |
±-------±----+
| 王刚 | 20 |
±-------±----+
1 row in set (0.30 sec)

查询数据库:
mysql> select * from students;
±—±-------±----±----±---------+
| id | name | sex | age | tel |
±—±-------±----±----±---------+
| 1 | 王刚 | 男 | 20 | 12345678 |
±—±-------±----±----±---------+
1 row in set (0.00 sec)

插入数据库:
mysql> insert into students values(NULL,“钟无艳”,“女”,100,“987654321”);
Query OK, 1 row affected (0.35 sec)

mysql> select * from students;
±—±----------±----±----±----------+
| id | name | sex | age | tel |
±—±----------±----±----±----------+
| 1 | 王刚 | 男 | 20 | 12345678 |
| 2 | 钟无艳 | 女 | 100 | 987654321 |
±—±----------±----±----±----------+
2 rows in set (0.00 sec)

查询数据库:
mysql> select * from students where sex=“女”;
±—±----------±----±----±----------+
| id | name | sex | age | tel |
±—±----------±----±----±----------+
| 2 | 钟无艳 | 女 | 100 | 987654321 |
±—±----------±----±----±----------+
1 row in set (0.28 sec)

修改数据库:
mysql> update students set tel = 123 where id = 2;
Query OK, 1 row affected (0.36 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from students;
±—±----------±----±----±---------+
| id | name | sex | age | tel |
±—±----------±----±----±---------+
| 1 | 王刚 | 男 | 20 | 12345678 |
| 2 | 钟无艳 | 女 | 100 | 123 |
±—±----------±----±----±---------+
2 rows in set (0.00 sec)

删除数据库:
mysql> delete from students where id=1;
Query OK, 1 row affected (0.35 sec)

mysql> select * from students;
±—±----------±----±----±-----+
| id | name | sex | age | tel |
±—±----------±----±----±-----+
| 2 | 钟无艳 | 女 | 100 | 123 |
±—±----------±----±----±-----+
1 row in set (0.00 sec)

修改数据库:
mysql> update students set id = 1 where id = 1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

mysql> select * from students;
±—±----------±----±----±-----+
| id | name | sex | age | tel |
±—±----------±----±----±-----+
| 2 | 钟无艳 | 女 | 100 | 123 |
±—±----------±----±----±-----+
1 row in set (0.00 sec)

mysql> update students set id = 1 where id = 2;
Query OK, 1 row affected (0.40 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from students;
±—±----------±----±----±-----+
| id | name | sex | age | tel |
±—±----------±----±----±-----+
| 1 | 钟无艳 | 女 | 100 | 123 |
±—±----------±----±----±-----+
1 row in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/weixin_43184774/article/details/82819519