数据库基本命令操作
----------------1----------------------
创库
create database haha;
使用库
use haha;
创表1
create table t1 (id int);
查看所有表
show tables;
插入数据
insert into t1 values(1);
查询所有数据
select * from t1;
删除表
drop table t1;
----------------2----------------------
创表2(两列id 和 name(16长度))
create table t2 (id int,name varchar(16));
查看表结构
desc t2;
插入数据
insert into t2 values(1,"zhangsan");
查询所有数据
select * from t2;
----------------3---------------------
修改表名
ALTER TABLE t2 RENAME t3;
查看所有表
show tables;
增加一列
ALTER TABLE t3 ADD address varchar (50);
查看表结构
desc t3;
插入数据
insert into t3 values(1,"wangwu","beijing");
查询所有数据
select * from t3;
----------------4---------------------
在ID列前增加一列(idd int)
ALTER TABLE t3 ADD idd int FIRST;
在ID列后增加一列(sex char)
ALTER TABLE t3 ADD sex char after id;
查看表结构
desc t3;
---------------5-----------------------
删除idd列
alter table t3 drop 字段名;
查看表结构:
desc t3;
---------------6------------------------
查看表结构
desc t3;
修改name列的列名为mingzi
ALTER TABLE t3 CHANGE name mingzi varchar(16) ;
查看表结构
desc t3;
---------------7--------------------------
把t3 复制 t4
create table t4 select * from t3;
查看t3 和t4 的内容
select * from t3;
select * from t4;
--------------结束-------------------------
--------------8-------------------------
8.列的类型-整数型测试:tinyint,int
创表
mysql> create table test1(
tinyint_test tinyint,
int_test int
);
插入两列合法值
mysql> insert into test1 values (111,111);
Query OK, 1 row affected (0.09 sec)
插入tinyint非法值
错误的示例:
mysql> insert into test1(tinyint_test) values(128);
ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1
正确的示例:
mysql> insert into test1(tinyint_test) values(127);
插入int合法值
mysql> insert into test1(int_test) values(2147483647);
Query OK, 1 row affected (0.05 sec)
插入int的非法值
mysql> insert into test1(int_test) values(2147483648);
ERROR 1264 (22003): Out of range value for column 'int_test' at row 1
验证那些值插入成功?
select * from test1;
--------------9-------------------------
--------------9-------------------------
9.数值列的符号概念。
创表
create table test2(
tinyint_test tinyint unsigned,
int_test int unsigned
);
插入tinyint unsigned 合法值
insert into test2(tinyint_test) values(255);
插入int unsigned 合法值
mysql> insert into test1(int_test) values(2147483648);
插入两列的非法数值 负数。
mysql> insert into test2 values(-20,-20);
ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1
--------------10-------------------------
10.掌握整数型 零填充约束。
创表
create table t2 (
id1 int zerofill,
id2 int(6) zerofill
);
插入值
mysql> insert into t2 values(2,2);
Query OK, 1 row affected (0.01 sec)
查询值
mysql> select * from t2;
+------------+--------+
| id1 | id2 |
+------------+--------+
| 0000000002 | 000002 |
+------------+--------+
1 row in set (0.00 sec)
--------------11-------------------------
--------------11-------------------------
11.掌握小数型数据类型
创表
create table test4(float_test float(5,2));
插入数值
insert into test4 values (10.2), (70.243), (70.246);
查询结果
mysql> select * from test4;
+------------+
| float_test |
+------------+
| 10.20 |
| 70.24 |
| 70.25 |
+------------+
3 rows in set (0.01 sec)
------------12--------------------