SQL中用新資料填充表

2021-06-18 09:48:59 字數 2075 閱讀 4686

一、插入記錄操作

語法:insert into 表名 (列名) values (列值);  

敘述:若在輸入記錄時,每個欄位均有內容,可省略表名後的欄位名。如果表名後面沒寫欄位名,則預設是向所有的字段新增值。

注意事項:1. 插入的值必須和列一一對應,各資料項用逗號分隔;

2. 插入值的資料型別、精度和小數字數必須和對應列的資料型別、精度和小數字數相一致;

3. 如果在設計表的時候就指定了某列不允許為空,則必須插入資料;

4. 如果某列的某個字段沒有值,可以插入 null 值;

5. 具有預設值的列,可以使用default(預設)關鍵字來代替插入的數值;

6. 字串和日期值資料必須用單引號括起來,數值型資料不用括起;

7. 所有標點符號一律在英文半形狀態下輸入。

例1:向 people 表中新增一條記錄:insert into people(name,age)values("zhangsan",20);

例2:建立乙個** teams 來記錄籃球隊:create table teams(team_id integer(2) not null,name  varchar(20) not null );

生成籃球隊的記錄:insert into teams values (1,'string music');

insert into teams values (2,'hackers');

insert into teams values (3,'sharp shooters');

insert into teams values (4,'hammer time');

例3:建立一張學生資訊表,往表中插入資料:create table students( scode  int   not null  auto_increment, sname  varchar(20)  not null,

saddress varchar(20)  default'未知', sgrade  int, semail  varchar(20), s***  bit, primary key(scode) );

insert into students (sname,saddress,sgrade,semail,s***)values ('張青裁','上海松江',96,'[email protected]', 0) ;

insert into students (sname,saddress,sgrade,semail,s***)values ('李曉明','北京海淀',75,'[email protected]', 1) ;

insert into students (sname,saddress,sgrade,semail,s***)values ('王晶晶', default, 83, '[email protected]', 1) ;

二、插入多行資料

insert into 表名 (列名) values (列值), (列值), (列值) …… ;

例4:往學生資訊表中插入多行資料:

insert  students (sname,sgrade,s***) values

('測試女生1',75,0),

('測試女生2',77,0),

('測試女生3',83,0),

('測試男生1',81,1), 

('測試女生4',90,0),

('測試男生2',94,1),

('測試女生5',51,0),

('測試男生3',53,1);

三、從另乙個表插入資料

insert into 表名 (列名)

select (列名) from 源表名;

這裡,select 是執行查詢的主要命令,下一章我們會詳細介紹;from 是查詢中的乙個子句,用於指定要查詢的表的名稱。        

例1:把學生資訊表中的部分資料插入通訊錄表裡:

insert into tongxunlu (姓名, 位址, 電子郵件) 

select sname, saddress, semail from students;

例2:把 teams 表裡的資料插入 teams_tmp 表裡:

insert into teams_tmp

select * from teams;

mysql中用查詢結果建立新錶

在mysql中可以把已經存在的表直接通過命令複製為另乙個表 方法1 create table mmm select from bbb 注意 這條命令要求mmm這個表在資料庫中不存在 這條命令可以建立新錶mmm並且bbb中的表結構以及資料和mmm完全一樣mysql insert,也可以匯出部分字段 c...

Oracle中用sql語句建立和管理表

create table schema.tablename column datatype default expr constaint desc tablename轉殖整個表 create table emp as select from scott.emp 轉殖表結構 create table ...

在ACCESS中用SQL匯入匯出資料

hkey local machine software microsofe jet 4.0 isam formats 下有多種格式,在access中可以使用這些格式匯入匯出 select into filname from text database e developer filname.txt ...