MySQL---建表添加语句,mysql---建语句


  • 创建学生表,表名student,根据需求,需要存储学号,姓名,性别、生日4列信息:

  • 向student表中添加学生信息,学号1,张三,男,1995-10-23


mysql> insert into student (sno,sname,sex,birth) values(1,'张三','男','1995-10-23');
Query OK, 1 row affected (0.00 sec)

mysql> select * from student;
+------+-------+------+------------+
| sno  | sname | sex  | birth      |
+------+-------+------+------------+
|    1 | 张三  | 男   | 1995-10-23 |
+------+-------+------+------------+
1 row in set (0.00 sec)
  • 向student表中添加学生信息,学号2,李四,女,不填写生日

mysql> insert into student (sno,sname,sex,birth) values(2,'李四','女','1995-10-23');
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+------+-------+------+------------+
| sno  | sname | sex  | birth      |
+------+-------+------+------------+
|    1 | 张三  | 男   | 1995-10-23 |
|    2 | 李四  | 女   | 1995-10-23 |
+------+-------+------+------------+
2 rows in set (0.00 sec)
  • SQL – 空值 null

  • DML-insert语句

向student表中一次添加2名学生信息


mysql> insert into student values(3,'王五','男','1996-05-12'),(4,'赵六',null,'1996-3-15');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from student;
+------+-------+------+------------+
| sno  | sname | sex  | birth      |
+------+-------+------+------------+
|    1 | 张三  | 男   | 1995-10-23 |
|    2 | 李四  | 女   | 1995-10-23 |
|    3 | 王五  | 男   | 1996-05-12 |
|    4 | 赵六  | NULL | 1996-03-15 |
+------+-------+------+------------+
4 rows in set (0.00 sec)
  • DML-update语句示例:

          把sno等于0的性别修改为‘女’


mysql>  update student set sex='女' where sno=0;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

update 表名 set 列1=列1值, 列2=列2值 where 列N=列N值;

        一般更新语句需要加上where子句已定位要修改的行,如果不加将会修改所有行相应的列;如果修改多个列对应的值,用“逗号”隔开各个列 。

  • DML-delete语句示例:

删除sno=0的行

mysql> delete from student where sno=2;

delete from 表名 where 列N=列N值;

有些数据库如oracle可以省略delete后面的from,mysql不可以;一般删除语句需要加上where子句已定位要删除的行,如果不加会删除整个表的所有行。

  • DDL建表语句CREATE TABLE其它用法

create table student1 as select * from student; (CTAS建表方式)

create table student2 as select * from student where gender='女';

create table student3 as select * from student where 1=2;

create table student4 like student; (MYSQL专有语法)

相关内容

    暂无相关文章