数据库与数据表的操作
选择数据库
Use db_user
查看数据表结构
Show columns from 数据表名
1.查看管理员表
2.查看“学生成绩表”
Describe 数据表名 这可以简写为 desc 数据表名;
修改数据表结构
Alter table 数据表名 add 字段 类型
例:altertable stu_info add 英语 float;
Desc stu_info;
二、数据表记录的更新操作
1.insert into 数据表名 values(**,**,……)
例:
(1)insert into user_info values(1,"yhd","yhd",1);
(2)
----我的操作--------
mysql> insertinto user_info values(1,yhd,yhd,1);
ERROR 1054(42S22): Unknown column 'yhd' in 'field list'
mysql> insertinto user_info values(1,"yhd","yhd",1);
Query OK, 1 row affected(0.00 sec)
mysql> insertinto stu_info values(1,"杨过",120,110,120);
ERROR 1366(HY000): Incorrect string value: 'xD1xEExB9xFD' for column '姓名' at row 1
mysql> insertinto stu_info values(1,"yhd",120,110,120);
Query OK, 1 rowaffected (0.00 sec)
【问题】为什么中文不可以呢?英文就可以
mysql> status
--------------
D:/wamp64/bin/mysql/mysql5.7.14/bin/mysql.exe Ver 14.14 Distrib 5.7.14, for Win64 (x86_64)
Connectionid: 2
Currentdatabase: db_user
Current user: root@localhost
SSL: Not in use
Usingdelimiter: ;
Serverversion: 5.7.14 MySQL CommunityServer (GPL)
Protocolversion: 10
Connection: localhost via TCP/IP
Servercharacterset: gbk
Db characterset: latin1
Client characterset: gbk
Conn. characterset: gbk
TCP port: 3306
Uptime: 47 min 25 sec
Threads: 1 Questions: 23 Slow queries: 0 Opens: 112 Flush tables: 1 Open tables: 103 Queries per second avg: 0.008
--------------
查看到用的gbk编码,是字节问题吗?
mysql> insertinto stu_info values(1,"试",20,10,20);
ERROR 1366(HY000): Incorrect string value: 'xCAxD4' for column '姓名' at row 1
还是不行哦!
mysql> altertable stu_info modify column 姓名 verchar(20);
ERROR 1064(42000): You have an error in your SQL syntax; check the manual thatcorresponds to your MySQL server version for the right syntax to use near'verchar(20)' at line 1
因为表中有数据,所以修改不成功,
要先清除数据
mysql> truncatetable stu_info;
Query OK, 0 rowsaffected (0.00 sec)
mysql> altertable stu_info modify column 姓名 char(20) character set gbk;
Query OK, 0 rowsaffected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
成功啦
mysql> insertinto stu_info values(1,"试",20,10,20);
Query OK, 1 rowaffected (0.00 sec)
mysql> insertinto stu_info values(1,"杨过",120,110,120);
Query OK, 1 rowaffected (0.00 sec)
mysql> insertinto stu_info values(1,"小龙女",120,120,120);
Query OK, 1 rowaffected (0.00 sec)
现在添加了三个数据了。
查询一下数据表中的数据:
好了,今天数据学习了数据添加,修改数据表结构(添加字段,修改类型)