MySQL第四课

database

CREATE TABLE biao(
    name VARCHAR(20) PRIMARY KEY,
    age INT(11) NOT NULL,
    sex CHAR(11)DEFAULT 1,
    love VARCHAR(11)
    );
Query OK, 0 rows affected

此时name设置了主键约束,age设置了非空约束,sex默认值为一

查看: DESC biao;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | NO   | PRI | NULL    |       |
| age   | int(11)     | NO   |     | NULL    |       |
| sex   | char(11)    | YES  |     | 1       |       |
| love  | varchar(11) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set

添加数据:

1.

INSERT INTO biao (name,age,sex,love) VALUES ("张三",41,"男","e");
Query OK, 1 row affected

查看:SELECT*FROM biao;
+------+-----+-----+------+
| name | age | sex | love |
+------+-----+-----+------+
| 张三 |  41 | 男  | e    |
+------+-----+-----+------+
1 row in set

2.

INSERT INTO biao  VALUES ("张",40,"男","em");
Query OK, 1 row affected

SELECT*FROM biao;
+------+-----+-----+------+
| name | age | sex | love |
+------+-----+-----+------+
| 张三 |  41 | 男  | e    |
| 张   |  40 | 男  | em   |
+------+-----+-----+------+
2 rows in set

3.

 INSERT INTO biao (sex) VALUES ("男");
Query OK, 1 row affected

SELECT*FROM biao;
+------+-----+-----+------+
| name | age | sex | love |
+------+-----+-----+------+
| 张三 |  41 | 男  | e    |
| 张   |  40 | 男  | em   |
|      |   0 | 男  | NULL |
+------+-----+-----+------+
3 rows in set

4.

INSERT INTO biao(name,age,sex,love) VALUES("李四",39,"女","ex"),("王五",38,"男","van"),("甲",20,"男","emm");
Query OK, 3 rows affected
Records: 3  Duplicates: 0  Warnings: 0

SELECT*FROM biao;
+------+-----+-----+------+
| name | age | sex | love |
+------+-----+-----+------+
| 张三 |  41 | 男  | e    |
| 张   |  40 | 男  | em   |
|      |   0 | 男  | NULL |
| 李四 |  39 | 女  | ex   |
| 王五 |  38 | 男  | van  |
| 甲   |  20 | 男  | emm  |
+------+-----+-----+------+
6 rows in set

 

以上是 MySQL第四课 的全部内容, 来源链接: utcz.com/z/531371.html

回到顶部